[JAVA] Object-oriented programming learned from Dragon Ball

1. 1. Purpose

2. Overview

The world of Dragon Ball is composed of various warriors such as Goku, Android Cell, and Majin Buu. This time, we will design and implement a small case while focusing on those who are different from ordinary people.

3. 3. UML design

UML diagram (class diagram)

Summary of frequently used items

image.png

Access modifier (UML diagram)

image.png

4. Design-Implementation

Warrior features: Fighting function → Treat as an interface

uploading-0

image.png

Fighter.java


interface Fighter {
	void fight();
}

Warriors possess the above functions and have the following characteristics

image.png

DragonballWarrior.java


public class DragonballWarrior implements Fighter {
	private String name;
	private int property;
	private int HP;

	@Override
	public void fight() {
	}

	public void getSkill() {
	}

	public void doSkill() {
	}
}

Goku can be treated as a subclass of warriors Summarize the common items of warriors and further subdivide

  • Saiyan (Super Saiyan under certain conditions / Monkey when looking at the moon)

image.png

Saiyan.java


public class Saiyan extends DragonballWarrior {
	/** [1:normal / 2:monkey / 3:super-saiyan] */
	private int status;

	public void changeStatus(Saiyan saiyan) {
		this.status = saiyan.status;
	}
}

I want to design a Tenkaichi Budokai that fights warriors

  • Field

Tenkaichi Budokai-Thinking about the has-a relationship of warriors Define superclass constructors and getters / setters

image.png

DragonballWarrior.java


public class DragonballWarrior implements Fighter {
	private String name;
	private int property;
	private int HP;

	/** default constractor */
	DragonballWarrior() {
	}

	DragonballWarrior(String name, int property, int HP) {
		this.name = name;
		this.property = property;
		this.HP = HP;
	}

	@Override
	public void fight() {
	}

	public void getSkill() {
	}

	public void doSkill() {
	}

	// S - getter,setter
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getProperty() {
		return property;
	}

	public void setProperty(int property) {
		this.property = property;
	}

	public int getHP() {
		return HP;
	}

	public void setHP(int hP) {
		HP = hP;
	}
	// E - getter,setter
}

Saiyan.java


public class Saiyan extends DragonballWarrior {
	/** [1:normal / 2:monkey / 3:super-saiyan] */
	private int status = 0;

	public Saiyan(String name, int property, int HP) {
		super(name, property, HP);
	}

	public void changeStatus(Saiyan saiyan) {
		this.status = saiyan.status;
	}

	@Override
	public void doSkill() {
		if (super.getName().equals("Goku")) {
			System.out.println("kamehame-ha");
		}
	}

	@Override
	public void fight() {
		if (super.getName().equals("Goku")) {
			System.out.println("ora-Goku!");
		}
	}

Saiyan.java


public class MartialArtist extends DragonballWarrior {
	public MartialArtist(String name, int property, int HP) {
		super(name, property, HP);
	}

	@Override
	public void doSkill() {
		if (super.getName().equals("Kuririn")) {
			System.out.println("kien-zan");
		}
	}

	@Override
	public void fight() {
		if (super.getName().equals("Kuririn")) {
			System.out.println("oreha-Kuririn!");
		}
	}
}

BattleTournament.java


public class BattleTournament {
	private List<DragonballWarrior> dwlist;

	public static void main(String args[]) {
		List<DragonballWarrior> dwlist = new ArrayList<>();
		dwlist.add(new Saiyan("Goku", 50000, 200));
		dwlist.add(new Saiyan("Vegeta", 40000, 250));
		dwlist.add(new MartialArtist("Kuririn", 3000, 80));
		dwlist.add(new MartialArtist("Kamesennin", 3000, 80));
		for (DragonballWarrior dw : dwlist) {
			dw.fight();
			dw.doSkill();
		}
	}
}

5. Execution result

Execution result.java


ora-Goku!
kamehame-ha
oreha-Kuririn!
kien-zan

It ’s full of stuff, I was able to confirm that polymorphism is working

→ Someday

6. the end

3 months tester → I thought I had to go to basic design and learn design patterns Perfect cell is singleton class design, technique is interface or abstract class and composition relationship

Now I would like to summarize anonymous classes-> generics-> lambda expressions and functional interfaces-> Stream API.

Recommended Posts

Object-oriented programming learned from Dragon Ball
Object-oriented programming
Object-oriented programming terminology
What is object-oriented programming? ~ Beginners ~
Object-oriented design learned from "Thorough capture Java SE 11 Silver problem collection"