qiuzeliang
4/22/2018 - 7:01 AM

工厂方法模式

抽象工厂定义一个用于创建对象的接口,让子类决定类的实例化

public class PeachFactory implements Factory {
    @Override
    public Fruit get(){
        return new Peach();
    }
}
public class Peach implements Fruit {
    @Override
    public void eat(){
        System.out.println("eat peach");
    }
}
public interface Fruit{
    void eat();
}
public interface Factory{
    Fruit get();
}
public class AppleFactory implements Factory {
    @Override
    public Fruit get() {
        return new Apple();
    }
}
public class Apple implements Fruit {
    @Override
    public void eat(){
        System.out.println("eat apple");
    }
}