We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strategy Pattern은 객체들의 행위를 클래스로 만들어서 캡슐화한 뒤, 행위의 변경이나 수정이 필요할 때 동적으로 행위를 바꿀 수 있도록 하는 디자인 패턴입니다.
public abstract class Robot { private String name; public Robot(String name) { this.name = name; } public String getName() { return name; } // 추상 메서드 public abstract void attack(); public abstract void move(); } public class TaekwonV extends Robot { public TaekwonV(String name) { super(name); } public void attack() { System.out.println("I have Missile."); } public void move() { System.out.println("I can only walk."); } } public class Atom extends Robot { public Atom(String name) { super(name); } public void attack() { System.out.println("I have strong punch."); } public void move() { System.out.println("I can fly."); } }
public class Client { public static void main(String[] args) { Robot taekwonV = new TaekwonV("TaekwonV"); Robot atom = new Atom("Atom"); System.out.println("My name is " + taekwonV.getName()); taekwonV.move(); taekwonV.attack(); System.out.println() System.out.println("My name is " + atom.getName()); atom.move(); atom.attack(); } }
public class Atom extends Robot { public Atom(String name) { super(name); } public void attack() { System.out.println("I have strong punch."); } //public void move() { System.out.println("I can fly."); } public void move() { System.out.println("I can only walk."); } // 수정 }
move()를 수정하려면 기존 코드의 내용을 수정해야 하므로 OCP에 위배됩니다.
상속을 통한 메소드 overriding 구현은 중복 코드가 발생하기 쉽습니다.
TaekwonV와 Atom의 move() 메서드 내용이 중복됩니다.
새로운 방식으로 수정하려면 중복 코드를 모두 수정해야 합니다.
public abstract class Robot { private String name; private AttackStrategy attackStrategy; private MovingStrategy movingStrategy; public Robot(String name) { this.name = name; } public String getName() { return name; } public void attack() { attackStrategy.attack(); } public void move() { movingStrategy.move(); } // setter 메서드 public void setAttackStrategy(AttackStrategy attackStrategy) { this.attackStrategy = attackStrategy; } public void setMovingStrategy(MovingStrategy movingStrategy) { this.movingStrategy = movingStrategy; } }
public class TaekwonV extends Robot { public TaekwonV(String name) { super(name); } } public class Atom extends Robot { public Atom(String name) { super(name); } }
// 인터페이스 interface AttackStrategy { public void attack(); } // 구체적인 클래스 public class MissileStrategy implements AttackStrategy { public void attack() { System.out.println("I have Missile."); } } public class PunchStrategy implements AttackStrategy { public void attack() { System.out.println("I have strong punch."); } }
// 인터페이스 interface MovingStrategy { public void move(); } // 구체적인 클래스 public class FlyingStrategy implements MovingStrategy { public void move() { System.out.println("I can fly."); } } public class WalkingStrategy implements MovingStrategy { public void move() { System.out.println("I can only walk."); } }
public class Client { public static void main(String[] args) { Robot taekwonV = new TaekwonV("TaekwonV"); Robot atom = new Atom("Atom"); /* 수정된 부분: 전략 변경 방법 */ taekwonV.setMovingStrategy(new WalkingStrategy()); taekwonV.setAttackStrategy(new MissileStrategy()); atom.setMovingStrategy(new FlyingStrategy()); atom.setAttackStrategy(new PunchStrategy()); /* 아래부터는 동일 */ System.out.println("My name is " + taekwonV.getName()); taekwonV.move(); taekwonV.attack(); System.out.println() System.out.println("My name is " + atom.getName()); atom.move(); atom.attack(); } }
Spring framework 에서 oauth2 를 이용하여 google, facebook, 등 로그인을 사용하는 예제
스프링 부트 어플리케이션의 전략 패턴
https://gmlwjd9405.github.io/2018/07/06/strategy-pattern.html
https://victorydntmd.tistory.com/292
The text was updated successfully, but these errors were encountered:
psi1104
No branches or pull requests
Strategy Pattern
Strategy Pattern은 객체들의 행위를 클래스로 만들어서 캡슐화한 뒤, 행위의 변경이나 수정이 필요할 때 동적으로 행위를 바꿀 수 있도록 하는 디자인 패턴입니다.
Strategy Pattern 사용 예
Atom이 움직이는 방법을 수정한다면?
move()를 수정하려면 기존 코드의 내용을 수정해야 하므로 OCP에 위배됩니다.
상속을 통한 메소드 overriding 구현은 중복 코드가 발생하기 쉽습니다.
TaekwonV와 Atom의 move() 메서드 내용이 중복됩니다.
새로운 방식으로 수정하려면 중복 코드를 모두 수정해야 합니다.
해결 방법
장점 및 단점
실제 사용 예시
Spring framework 에서 oauth2 를 이용하여 google, facebook, 등 로그인을 사용하는 예제
스프링 부트 어플리케이션의 전략 패턴
Reference
https://gmlwjd9405.github.io/2018/07/06/strategy-pattern.html
https://victorydntmd.tistory.com/292
The text was updated successfully, but these errors were encountered: