-
12일 차 디자인패턴을 이용하기 - 첨부파일포함카테고리 없음 2015. 9. 22. 14:50반응형
public interface Move {
public void move();
}
public class SlowMove implements Move{@Overridepublic void move() {// TODO Auto-generated method stubSystem.out.println("느린 이동");}}public class FastMove implements Move{@Overridepublic void move() {// TODO Auto-generated method stubSystem.out.println("빠른 이동");}}public interface Shotting {
public void shotting();
}
public class BadShotting implements Shotting{
@Override
public void shotting() {
// TODO Auto-generated method stub
System.out.println("좋지 않은 명중률");
}
}
public class GoodShotting implements Shotting{
@Override
public void shotting() {
// TODO Auto-generated method stub
System.out.println("좋은 명중률");
}
}
public class Army {
protected String name;
protected Shotting shotting;
protected Move move;
public Army(String name) {
this.name = name;
}
public void shotting(){
shotting.shotting();
}
public void move(){
move.move();
}
public void setShotting(Shotting shotting){
this.shotting=shotting;
}
public void setMove(Move move){
this.move=move;
}
}
public class Signaller extends Army{
public Signaller(String name) {
super(name);
System.out.println(name);
}
public void shotting(){
shotting.shotting();
}
public void move(){
move.move();
}
}
public class Riflemen extends Army {public Riflemen(String name) {super(name);System.out.println(name);}public void shotting(){shotting.shotting();}public void move(){move.move();}}public class Ranger extends Army {public Ranger(String name) {super(name);System.out.println("레인저");}public void shotting() {shotting.shotting();}public void move() {move.move();}}public class Officer extends Army{public Officer(String name) {super(name);System.out.println(name);}public void shotting(){shotting.shotting();}public void move(){move.move();}}public class Test {
public static void main(String[] args) {
Shotting bs = new BadShotting();
Shotting gs = new GoodShotting();
Move fm = new FastMove();
Move sm = new SlowMove();
Army sig = new Signaller("통신병");
sig.setShotting(bs);
sig.setMove(fm);
sig.shotting();
sig.move();
System.out.println();
Army rif = new Riflemen("소총병");
rif.setShotting(gs);
rif.setMove(sm);
rif.shotting();
rif.move();
System.out.println();
Army off = new Officer("장교");
off.setShotting(bs);
off.setMove(sm);
off.shotting();
off.move();
System.out.println();
Army ran = new Ranger("레인저");
ran.setShotting(gs);
ran.setMove(fm);
ran.shotting();
ran.move();
}
}
디자인패턴을 이용하면 내용의 변경이 있을 때, 손쉽게 변경이 가능하다
첨부한 파일중 OOP4, OOP5, OOP6(현재 올린 글의 내용과 똑같음)을 보면
OOP4와 OOP5는 재사용에서 매우 떨어진다는 것을 확인할 수 있을 것이다.
첨부파일 사용법은 압축을 풀고
Eclipse -> File -> Import -> General -> Existing Projects into Workspace -> Select root directory 에서 Browser -> 압축 푼 폴더 클릭하면 됩니다
반응형