I studied the State pattern and the Strategy pattern

Everyone. This is the last day of the year. About a month ago, I declared on my blog that I would post to Qiita again within the year, and I finally posted it at the very end of the year: sweat_smile:

I usually study C # mainly, but most of the design pattern books were JAVA, so I tried my best to write in JAVA.

As an inexperienced person, I finally learned about "design patterns" this year.

The design pattern shocked me just like Vegeta's Big Bang attack. I didn't really understand the difference between State and Strategy, so I will summarize it myself. sorry if that's not right.

State is a state that moves externally or internally. Strategy is a strategy that externally decides which strategy to take

I felt like this.

So, this time, I bought these two patterns with a person named Kyosuke Shindo. Please note that it may be a little longer.

First, about the program.

There is only one person, Kyosuke Shindo. Kyosuke Shindo chooses whether to work normally or hard. (Strategy) Then, you will gain experience points from your work, and Kyosuke Shindo will be ranked higher depending on the experience points. (State)

It's a silly program.

First, write from Main to be executed.

Main.java



package Kyosuke;

public class Main {
	
	public static void main(String[] args) {
		Human kyosuke = new Kyosuke();
		//Work and get 1 experience point
		kyosuke.DoWork();
		//Work hard and get 3 experience points
		kyosuke.ChangeWorkStyle(new HardWork());
		kyosuke.DoWork();
		//After all I work normally
		kyosuke.ChangeWorkStyle(new NomalWork());
		kyosuke.DoWork();
		kyosuke.DoWork();
		kyosuke.DoWork();
		//I'm serious from today w
		kyosuke.ChangeWorkStyle(new HardWork());
		kyosuke.DoWork();
		kyosuke.DoWork();
		kyosuke.DoWork();
		kyosuke.DoWork();
		kyosuke.DoWork();
		kyosuke.DoWork();
		
	}
}



Rank is now B rank
I got 1 experience point because I worked normally
I got 3 experience points because I worked hard
I got 1 experience point because I worked normally
I got 1 experience point because I worked normally
I got 1 experience point because I worked normally
I got 3 experience points because I worked hard
Rank is now A rank
I got 3 experience points because I worked hard
I got 3 experience points because I worked hard
I got 3 experience points because I worked hard
I got 3 experience points because I worked hard
I can't stop w
I got 3 experience points because I worked hard
I can't stop w

The first rank is B rank. And work is also normal. I work hard on the way and the rank goes up, and finally I can stop

Let's take a look at a person named Kyosuke Shindo.

Human.java



package Kyosuke;

interface Human {
	void DoWork();
	void SetRank(Rank newRank);
	void ChangeWorkStyle(Work style);
	int GetExperiencepoint();
}


Kyosuke.java



package Kyosuke;

public class Kyosuke implements Human{
	private Rank rank;
	private Work work;
	private int experiencepoint;

	public Kyosuke() {
		this.rank = new BRank(this);
		this.work = new NomalWork();
		//Experience value 1 at first
		this.experiencepoint = 1;
	}
	
	@Override
	public void DoWork() {
		int point = work.DoWork();
		this.experiencepoint += point;
		//Check Rank here
		rank.CheckNextRank();
	}

	@Override
	public void SetRank(Rank newRank) {
		this.rank = newRank;
	}

	@Override
	public void ChangeWorkStyle(Work style) {
		this.work = style;
	}

	@Override
	public int GetExperiencepoint() {
		return this.experiencepoint;
	}
}


It has a Rank that represents the current rank, a Work that represents a job (meaning unknown), and an experience point.

Initialize all of these in the constructor. Actions that actually work, actions that change ranks, actions that change work styles, actions that return experience points Override these.

Now, I will write about work style.

Work.java


package Kyosuke;

public abstract class Work {
	public abstract int DoWork();
}

NomalWork.java


package Kyosuke;
public class NomalWork extends Work{
	private final int experiencePoint =1;
	@Override
	public int DoWork() {
		System.out.println("I got 1 experience point because I worked normally");
		return experiencePoint;
	}
}

HardWork.java


package Kyosuke;
public class HardWork extends Work{
	private final int experiencePoint = 3;
	@Override
	public int DoWork() {
		System.out.println("I got 3 experience points because I worked hard");
		return experiencePoint;
	}
}

Well, if you simply work, it will return your experience points. If you don't use this pattern, you will need if statements and switch statements. Specific processing can be easily switched by implementing it in a subclass.

Rank.java


package Kyosuke;

public abstract class Rank {
	protected int experinencepoint;
	protected int nextLeve;
	protected Human human;
	
	public Rank(Human human){
		this.human = human;
	}

	protected void SetExperinencepoint(){
		this.experinencepoint =human.GetExperiencepoint();
	}

	protected abstract void CheckNextRank();
}

And Rank. This is the State. The experience value of Kyosuke Shindo is expressed in the state of rank. The constructor receives Kyosuke Shindo.

BRank.java


package Kyosuke;

public class BRank extends Rank{
	public BRank(Human human){
		super(human);	
		this.nextLeve = 8;
		System.out.println("Rank is now B rank");
	}
	@Override
	protected void CheckNextRank() {
		//First, check the experience points
		SetExperinencepoint();
		//Level up if experience points exceed the next level
		if (experinencepoint > nextLeve){
			human.SetRank(new ARank(this.human));
		}
	}
	
}

Override the required methods in the Rank subclass. In the constructor, call the constructor of the parent class and set Kyosuke Shindo. And set nextLeve that you need 8 experience points until the next level (what is Nextrebe)

The point is the CheckNextRank method. The experience value of Kyosuke Shindo is read, and the state is changed accordingly. In particular SetExperinencepoint(); If the experience value is read with and the condition of the next level set in the constructor is exceeded, the rank is raised to A rank. (Changes the state)

On the Main method side, it is not involved in this change of state at all. Everything is moving internally.

ARank.java


package Kyosuke;

public class ARank extends Rank{
	public ARank(Human human){
		super(human);
		nextLeve = 20;
		System.out.println("Rank is now A rank");
	}
	
	@Override
	protected void CheckNextRank() {
		//First, check the experience points
		SetExperinencepoint();
		//Level up if experience points exceed the next level
		if (experinencepoint > nextLeve){
			System.out.println("I can't stop w");
		}
	}
	
}


And ARank also receives Kyosuke Shindo in the constructor. The processing content is the same as BRank. If you want to return to BRank without a counter stop, or if you want to go to a higher level, you can hand over Kyosuke Shindo in the same way.

I've put it together very easily, but that's it.

The design pattern was magical. He is also such a teacher who makes me think that I should learn more and more.

Well, suddenly Next year will be an unemployed start: severe:

Anyway, I will practice and do my best so that I can eat by programming.

Recommended Posts

I studied the State pattern and the Strategy pattern
I studied the constructor (java)
Strategy pattern
Strategy Pattern
What a Strategy pattern Factory, not a State
I tried to implement the Iterator pattern
[Rails] I studied the difference between new method, save method, build method and create method.
[Java] Strategy pattern
I studied for 3 weeks and passed Java Bronze
Design pattern ~ State ~
Design pattern ~ Strategy ~
I compared the characteristics of Java and .NET
[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I learned about the difference between resources and resources
I summarized the types and basics of Java exceptions
[Rails] I investigated the difference between redirect_to and render.
Difference between i ++ and ++ i
I changed the Ruby version and now I can't bundle install
[Java] Change the process according to the situation with the Strategy pattern
I didn't understand the behavior of Java Scanner and .nextLine ().
I want to bring Tomcat to the server and start the application
I want to call a method and count the number
I tried JAX-RS and made a note of the procedure
Explain the benefits of the State pattern with a movie rating
I want to transition to the same screen in the saved state
Understand the Iterator pattern by comparing Java and JavaScript code
[Java] I thought about the merits and uses of "interface"