JAVA 장식 자 모드 단순 예시
1314 단어 프로 그래 밍 입문-JAVA
interface Worker{
public void doWork();
}
class Plumber implements Worker{
public void doWork(){
System.out.println(" ");
}
}
class Carpenter implements Worker{
public void doWork(){
System.out.println(" ");
}
}
class AWorker implements Worker{
private Worker worker;
public AWorker(Worker worker){
this.worker=worker;
}
public void doWork(){
System.out.println(" , A ");
worker.doWork();
}
}
class BWorker implements Worker{
private Worker worker;
public BWorker(Worker worker){
this.worker=worker;
}
public void doWork(){
System.out.println(" , B ");
worker.doWork();
}
}
class Test{
public static void main(String args[]){
Plumber plumber=new Plumber();
AWorker aWorker1=new AWorker(plumber);
aWorker1.doWork();
Carpenter carpenter=new Carpenter();
BWorker bWorker1=new BWorker(carpenter);
bWorker1.doWork();
}
}
실행 결과:
안녕하세요.
수도관 을 수리 하 다
안녕하세요. B 사 직원 입 니 다.
문과 창문 을 수리 하 다