简单工厂模式
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(); } }
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25