人人都是产品经理 2017-11-28
Product:抽象产品角色,它定义了这个产品的通用属性,相当于模子,它定义了一些功能,这些功能可以由子类去实现
ConcreteProduct:具体产品角色,它实现了抽象产品所定义的功能,每个 ConcreteProduct 相当于一个产品种类
SimpleFactory:具体工厂模式类,它实现了抽象工厂定义的所有功能,负责创建具体的产品,它根据这个工厂的具体实现来生产具体的产品,但是这个产品也都具有符合抽象产品类定义的功能
Product
package design.pattern.simplefactory; public abstract class Product { private String type; public Product(String type) { this.type = type; } public String getType() { return type; } public abstract void function(); }
ConcreteProduct
package design.pattern.simplefactory; public class ConcreteProduct extends Product { public ConcreteProduct(String type) { super(type); } @Override public void function() { System.out.println("ConcreteProduct"); } }
SimpleFactory
package design.pattern.simplefactory; public class SimpleFactory { public Product getProduct() { return new ConcreteProduct("Type A"); } }
Client
package design.pattern.simplefactory; public class Client { public static void main(String[] args) { new SimpleFactory().getProduct().function(); } }