适配器模式

2016-05-20  本文已影响9人  keith666

Intent

Structure

Adapter by keith
  1. 代码:
public class Adapter {
    public static void main(String[] args) {
        Dog dog = new Dog();
        Flyable flyable = new FlyAdapter(dog);
        flyable.fly();
    }
}
//dog can't fly
class Dog {
    void running() {
        System.out.print("Dog is running");
    }
}
interface Flyable {
    void fly();
}
// adapter convert dog to be a flyable object
class FlyAdapter implements Flyable {
    Dog dog;

    public FlyAdapter(Dog dog) {
        this.dog = dog;
    }

    @Override
    public void fly() {
        dog.running();
    }
}
  1. Output
Dog is running

Refenrence

  1. Design Patterns
  2. 设计模式
上一篇下一篇

猜你喜欢

热点阅读