design patterns : strategy

This commit is contained in:
haerong22
2021-12-22 01:31:00 +09:00
parent 3e176ea078
commit bb620d4b9f
8 changed files with 109 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
package strategy.after;
public class BlueLightRedLight {
public void blueLight(Speed speed) {
speed.blueLight();
}
public void redLight(Speed speed) {
speed.redLight();
}
}

View File

@@ -0,0 +1,10 @@
package strategy.after;
public class Client {
public static void main(String[] args) {
BlueLightRedLight game = new BlueLightRedLight();
game.blueLight(new Normal());
game.redLight(new Faster());
}
}

View File

@@ -0,0 +1,13 @@
package strategy.after;
public class Faster implements Speed {
@Override
public void blueLight() {
System.out.println("무궁화꽃이");
}
@Override
public void redLight() {
System.out.println("피었습니다.");
}
}

View File

@@ -0,0 +1,13 @@
package strategy.after;
public class Fastest implements Speed {
@Override
public void blueLight() {
System.out.println("무광꼬치");
}
@Override
public void redLight() {
System.out.println("피어씀다");
}
}

View File

@@ -0,0 +1,13 @@
package strategy.after;
public class Normal implements Speed {
@Override
public void blueLight() {
System.out.println("무 궁 화 꽃 이");
}
@Override
public void redLight() {
System.out.println("피 었 습 니 다.");
}
}

View File

@@ -0,0 +1,7 @@
package strategy.after;
public interface Speed {
void blueLight();
void redLight();
}

View File

@@ -0,0 +1,31 @@
package strategy.before;
public class BlueLightRedLight {
private int speed;
public BlueLightRedLight(int speed) {
this.speed = speed;
}
public void blueLight() {
if (speed == 1) {
System.out.println("무 궁 화 꽃 이");
} else if (speed == 2) {
System.out.println("무궁화꽃이");
} else {
System.out.println("무광꼬치");
}
}
public void redLight() {
if (speed == 1) {
System.out.println("피 었 습 니 다.");
} else if (speed == 2) {
System.out.println("피었습니다.");
} else {
System.out.println("피어씀다");
}
}
}

View File

@@ -0,0 +1,10 @@
package strategy.before;
public class Client {
public static void main(String[] args) {
BlueLightRedLight blueLightRedLight = new BlueLightRedLight(3);
blueLightRedLight.blueLight();
blueLightRedLight.redLight();
}
}