ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 12일 차 디자인패턴을 이용한 프로그래밍
    삼성SDS_멀티캠퍼스/Java 2015. 9. 22. 14:11
    반응형


    public interface Move {


    public void move();

    }



    public class FastMove implements Move{


    @Override

    public void move() {

    // TODO Auto-generated method stub

    System.out.println("빠른 이동");

    }


    }


    public class SlowMove implements Move{

    @Override
    public void move() {
    // TODO Auto-generated method stub
    System.out.println("느린 이동");
    }

    }









    -------------------------------------------------------------------------------------------



    public interface Shotting {

    public void shotting();
    }

    public class GoodShotting implements Shotting{

    @Override
    public void shotting() {
    // TODO Auto-generated method stub
    System.out.println("좋은 명중률");
    }

    }

    public class BadShotting 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 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 Ranger extends Army {

    public Ranger(String name) {
    super(name);
    System.out.println("레인저");
    }

    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 Test {

    public static void main(String[] args) {

    Shotting bs = new BadShotting();
    Shotting gs = new GoodShotting();
    Move fm = new FastMove();
    Move sm = new SlowMove();

    Signaller sig = new Signaller("통신병");
    sig.setShotting(bs);
    sig.setMove(fm);
    sig.shotting();
    sig.move();

    System.out.println();

    Riflemen rif = new Riflemen("소총병");
    rif.setShotting(gs);
    rif.setMove(sm);
    rif.shotting();
    rif.move();

    System.out.println();

    Officer off = new Officer("장교");
    off.setShotting(bs);
    off.setMove(sm);
    off.shotting();
    off.move();

    System.out.println();
    Ranger ran = new Ranger("레인저");
    ran.setShotting(gs);
    ran.setMove(fm);
    ran.shotting();
    ran.move();
    }
    }





    반응형
Designed by Tistory.