设计模式之工厂方法模式(二)
为了解决简单工厂不满足OCP的问题,又提出工厂方法模式一概念,下面的例子:
interface Car{ public void run(); public void stop();}class Ford implements Car{ public void run(){ System.out.println("Ford run。。。"); } public void stop(){ System.out.println("Ford stop。。。。"); }}class Toyota implements Car{ public void run(){ System.out.println("Toyota run。。。"); } public void stop(){ System.out.println("Toyota stop。。。。"); }}interface factory{Car getCar();}class ToyotaFactory implements factory{public Car getCar() {return new Toyota();}}class FordFactory implements factory{public Car getCar() {return new Ford();}}