티스토리 뷰

Java

Mediator Pattern

techbard 2016.07.18 19:11



# IMediator.java


public interface IMediator {

    

    public abstract void play();

    public abstract void eat();

    public abstract void talk();

}



# CommonUnit.java


public abstract class CommonUnit {

    IMediator mediator;


    public void setMediator(IMediator mediator) {

this.mediator = mediator;

    }

    

    public abstract void run();

}    



# Unit01.java


public class Unit01 extends CommonUnit {

    

    @Override

    public void run() {

mediator.eat();

mediator.play();

    }

}



# Unit02.java


public class Unit02 extends CommonUnit {


    @Override

    public void run() {

mediator.talk();

mediator.eat();

    }

}



# ConcreteMediator.java


public class ConcreteMediator implements IMediator {


    @Override

    public void play() {

System.out.println("playing...");

    }


    @Override

    public void eat() {

System.out.println("eating...");

    }


    @Override

    public void talk() {

System.out.println("talking...");

    }

}



# Main.java


public class Main {


    public static void main(String[] args) {

Unit01 unit01 = new Unit01();

ConcreteMediator concreteMediator = new ConcreteMediator();

unit01.setMediator(concreteMediator);

unit01.run();

System.out.println("====================");

Unit02 unit02 = new Unit02();

unit02.setMediator(concreteMediator);

unit02.run();

    }

}



# 결과


eating...

playing...

====================

talking...

eating...


댓글
댓글쓰기 폼
공지사항
Total
394,931
Today
2
Yesterday
28
«   2018/11   »
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  
글 보관함