Strategy Pattern
This commit is contained in:
14
design-pattern/src/Strategy/AObj.java
Normal file
14
design-pattern/src/Strategy/AObj.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package Strategy;
|
||||
|
||||
public class AObj {
|
||||
Ainterface ainterface;
|
||||
|
||||
public AObj() {
|
||||
ainterface = new AinterfaceImpl();
|
||||
}
|
||||
|
||||
public void funcAA() {
|
||||
ainterface.funcA();
|
||||
ainterface.funcA();
|
||||
}
|
||||
}
|
||||
7
design-pattern/src/Strategy/Ainterface.java
Normal file
7
design-pattern/src/Strategy/Ainterface.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package Strategy;
|
||||
|
||||
public interface Ainterface {
|
||||
|
||||
// 기능 선언
|
||||
void funcA();
|
||||
}
|
||||
8
design-pattern/src/Strategy/AinterfaceImpl.java
Normal file
8
design-pattern/src/Strategy/AinterfaceImpl.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package Strategy;
|
||||
|
||||
public class AinterfaceImpl implements Ainterface{
|
||||
@Override
|
||||
public void funcA() {
|
||||
System.out.println("A function");
|
||||
}
|
||||
}
|
||||
8
design-pattern/src/Strategy/Ax.java
Normal file
8
design-pattern/src/Strategy/Ax.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package Strategy;
|
||||
|
||||
public class Ax implements Weapon {
|
||||
@Override
|
||||
public void attack() {
|
||||
System.out.println("도끼 공격 훙훙");
|
||||
}
|
||||
}
|
||||
20
design-pattern/src/Strategy/GameCharacter.java
Normal file
20
design-pattern/src/Strategy/GameCharacter.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package Strategy;
|
||||
|
||||
public class GameCharacter {
|
||||
// 접근점
|
||||
private Weapon weapon;
|
||||
|
||||
// 교환 가능
|
||||
public void setWeapon(Weapon weapon) {
|
||||
this.weapon = weapon;
|
||||
}
|
||||
|
||||
public void attack() {
|
||||
// 델리게이트
|
||||
if (weapon == null ) {
|
||||
System.out.println("맨손공격 퍽퍽");
|
||||
} else {
|
||||
weapon.attack();
|
||||
}
|
||||
}
|
||||
}
|
||||
8
design-pattern/src/Strategy/Gun.java
Normal file
8
design-pattern/src/Strategy/Gun.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package Strategy;
|
||||
|
||||
public class Gun implements Weapon {
|
||||
@Override
|
||||
public void attack() {
|
||||
System.out.println("총 쏘기 빵야빵야");
|
||||
}
|
||||
}
|
||||
23
design-pattern/src/Strategy/Main.java
Normal file
23
design-pattern/src/Strategy/Main.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package Strategy;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// Ainterface ainterface = new AinterfaceImpl();
|
||||
// ainterface.funcA();
|
||||
//
|
||||
// AObj aObj = new AObj();
|
||||
// aObj.funcAA();
|
||||
GameCharacter gameCharacter = new GameCharacter();
|
||||
|
||||
gameCharacter.attack();
|
||||
|
||||
gameCharacter.setWeapon(new Gun());
|
||||
gameCharacter.attack();
|
||||
|
||||
gameCharacter.setWeapon(new Sword());
|
||||
gameCharacter.attack();
|
||||
|
||||
gameCharacter.setWeapon(new Ax());
|
||||
gameCharacter.attack();
|
||||
}
|
||||
}
|
||||
30
design-pattern/src/Strategy/Strategy
Normal file
30
design-pattern/src/Strategy/Strategy
Normal file
@@ -0,0 +1,30 @@
|
||||
인터페이스
|
||||
- 기능에 대한 선언과 구현 분리
|
||||
Ainterface : 선언 , AinterfaceImpl : 구현
|
||||
|
||||
- 기능을 사용할 수 있는 통로
|
||||
Ainterface 타입으로 AimterfaceImple 객체를 생성 및 메소드 사용
|
||||
|
||||
델리게이트
|
||||
- 위임하다
|
||||
AObj 에서 funcAA를 사용하기위해 AinterfaceImpl 객체를 호출, 메소드 사용
|
||||
- 어떤 객체가 어떤 기능을 수행할 때 그 객체 이외의 다른 객체의 기능을 사용
|
||||
|
||||
Strategy pattern
|
||||
- 여러 알고리즘을 하나의 추상적인 접근점을 만들어 접근 점에서 서로 교환 가능하도록 하는 패턴
|
||||
|
||||
요구사항
|
||||
- 신작 게임에서 케릭터의 무기를 구현
|
||||
- 무기 종류는 총, 검 두가지
|
||||
|
||||
-> Weapon interface 생성
|
||||
-> Weapon을 상속 받는 Gun, Sword 클래스 생성
|
||||
-> GameCharacter 클래스에서 attack 메소드를 사용하기 위해
|
||||
Weapon 객체를 호출 하여 사용( attack 메소드 직접 구현 X )
|
||||
-> Main 클래스에서 GameCharacter 객체를 생성하고 사용
|
||||
|
||||
추가사항
|
||||
- 무기 도끼 추가
|
||||
|
||||
-> 다른 코드들을 변경할 필요 없이
|
||||
Weapon을 상속받은 Ax클래스만 추가하여 사용가능 해진다.
|
||||
8
design-pattern/src/Strategy/Sword.java
Normal file
8
design-pattern/src/Strategy/Sword.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package Strategy;
|
||||
|
||||
public class Sword implements Weapon {
|
||||
@Override
|
||||
public void attack() {
|
||||
System.out.println("검 공격 서걱서걱");
|
||||
}
|
||||
}
|
||||
6
design-pattern/src/Strategy/Weapon.java
Normal file
6
design-pattern/src/Strategy/Weapon.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package Strategy;
|
||||
|
||||
public interface Weapon {
|
||||
|
||||
void attack();
|
||||
}
|
||||
Reference in New Issue
Block a user