기타/Java

[Design Pattern]Adapter Pattern

stonesy 2021. 11. 16. 13:53
728x90
public interface Duck {
    public void quack();
    public void fly();
}
public interface Turkey {
    public void gobble();
    public void fly();
}
public class MallardDuck implements Duck{
    public void quack(){
        System.out.println("Quack");
    }
    public void fly(){
        System.out.println("I'm flying");
    }
}
public class WildTurkey implements Turkey{
    public void gobble(){
        System.out.println("Gobble gobble");
    }
    public void fly(){
        System.out.println("I'm flying a short distance");
    }
}
public class TurkeyAdapter implements Duck{
    Turkey turkey;

    public TurkeyAdapter(Turkey turkey){
        this.turkey=turkey;
    }
    public void quack(){
        turkey.gobble();
    }
    public void fly(){
        for(int i=0;i<5;i++){
            turkey.fly();
        }
    }
}
public class DuckTestDrive {
    public static void min(String[] args){
        MallardDuck duck = new MallardDuck();

        WildTurkey turkey = new WildTurkey();
        Duck turkeyAdapter = new TurkeyAdapter(turkey);
        turkey.fly();

        System.out.println("\nThe Duck says...");
        testDuck(duck);

        System.out.println("\nThe TurkeyAdapter says...");
        testDuck(turkeyAdapter);
    }

    static void testDuck(Duck duck){
        duck.quack();
        duck.fly();
    }
}
728x90

'기타 > Java' 카테고리의 다른 글

JAVA(1)  (0) 2024.01.20
[디자인패턴]Iterator Pattern  (0) 2021.12.02
[Design Pattern]Command Pattern  (0) 2021.11.16
[Java]Design Pattern-Singleton  (0) 2021.11.03
[Java]Design Pattern-Decorator Pattern  (0) 2021.10.28