标签:err over factory 工厂 imp ret factor 定义 汽车
工厂方法-(Factory Method) : 定义一个用于创建产品的接口,由子类决定生产什么产品
/** * 定义一个交通工具 */ public interface Vehicle { void go(); } /** * 飞行扫把 */ public class Broom implements Vehicle { @Override public void go() { System.out.println("broom go"); } } /** * 汽车 */ public class Car implements Vehicle { @Override public void go() { System.out.println("car go"); } } /** * 飞机 */ public class Plane implements Vehicle { @Override public void go() { System.out.println("plane go "); } }
/** * 交通创建工厂 */ public interface VehicleFactory { Vehicle create(); } public class BroomFactory implements VehicleFactory { @Override public Vehicle create() { System.out.println(" create broom before"); return new Broom(); } } public class CarFactory implements VehicleFactory { @Override public Vehicle create() { System.out.println(" create car before"); return new Car(); } } public class PlaneFactory implements VehicleFactory { @Override public Vehicle create() { System.out.println(" create plane before"); return new Plane(); } }
标签:err over factory 工厂 imp ret factor 定义 汽车
原文地址:https://www.cnblogs.com/huan30/p/12732978.html