提交 a37d9399 编写于 作者: C chenjianqiang

增加工厂模式(方法工厂)

上级 03521015
......@@ -2,18 +2,8 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="e43e9168-5945-4f45-b4c2-1b6410d1eef8" name="Changes" comment="增加策略模式案例&#10;Signed-off-by: chenjianqiang &lt;wushanxi@gmail.com&gt;">
<change beforePath="$PROJECT_DIR$/out/production/patterns/com/chen/InterpreterPattern/InterpreterMain.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/patterns/com/chen/InterpreterPattern/po/AndExpression.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/patterns/com/chen/InterpreterPattern/po/Expression.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/patterns/com/chen/InterpreterPattern/po/OrExpression.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/patterns/com/chen/InterpreterPattern/po/TerminalExpression.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/patterns/com/chen/PatternApplication.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/patterns/com/chen/StrategyPattern/Context.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/patterns/com/chen/StrategyPattern/StrategyMain.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/patterns/com/chen/StrategyPattern/po/OperationA.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/patterns/com/chen/StrategyPattern/po/OperationB.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/patterns/com/chen/StrategyPattern/po/Strategy.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/pattern/FactoryPattern/SimpleFactory/package-info.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/FactoryPattern/SimpleFactory/package-info.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/pattern/FactoryPattern/FunctionFactory/package-info.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/FactoryPattern/FunctionFactory/package-info.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/pattern/FactoryPattern/SimpleFactory/materials/SimpleFactoryMain.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/FactoryPattern/SimpleFactory/materials/SimpleFactoryMain.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/pattern/PatternApplication.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/PatternApplication.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
......@@ -21,12 +11,15 @@
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="CodeInsightWorkspaceSettings">
<option name="optimizeImportsOnTheFly" value="true" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Interface" />
<option value="package-info" />
<option value="Class" />
<option value="Interface" />
</list>
</option>
</component>
......@@ -100,6 +93,7 @@
<component name="PropertiesComponent">
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/src/com/pattern/FactoryPattern/AbstractFactory" />
<property name="settings.editor.selected.configurable" value="editor.preferences.import" />
<property name="vue.rearranger.settings.migration" value="true" />
</component>
<component name="RecentsManager">
......@@ -111,9 +105,11 @@
<recent name="E:\chen\patterns\src\com\chen\StrategyPattern" />
</key>
<key name="CopyClassDialog.RECENTS_KEY">
<recent name="com.pattern.FactoryPattern.FunctionFactory.materials.factory" />
<recent name="com.pattern.FactoryPattern.FunctionFactory.materials.po" />
<recent name="com.pattern.FactoryPattern.AbstractFactory.materials" />
<recent name="com.pattern.FactoryPattern.FunctionFactory" />
<recent name="com.pattern.FactoryPattern.SimpleFactory" />
<recent name="com.chen.StrategyPattern.po" />
<recent name="com.company.InterpreterPattern" />
</key>
</component>
<component name="RunManager">
......@@ -145,7 +141,7 @@
<workItem from="1634085227295" duration="1967000" />
<workItem from="1634087918450" duration="3646000" />
<workItem from="1634091679401" duration="1323000" />
<workItem from="1634093719115" duration="8837000" />
<workItem from="1634093719115" duration="10277000" />
</task>
<task id="LOCAL-00001" summary="增加策略模式案例&#10;Signed-off-by: chenjianqiang &lt;wushanxi@gmail.com&gt;">
<created>1634096220780</created>
......@@ -175,4 +171,8 @@
<MESSAGE value="增加策略模式案例&#10;Signed-off-by: chenjianqiang &lt;wushanxi@gmail.com&gt;" />
<option name="LAST_COMMIT_MESSAGE" value="增加策略模式案例&#10;Signed-off-by: chenjianqiang &lt;wushanxi@gmail.com&gt;" />
</component>
<component name="XSLT-Support.FileAssociations.UIState">
<expand />
<select />
</component>
</project>
\ No newline at end of file
package com.pattern.FactoryPattern.AbstractFactory.materials;
/**
* 主方法
*
* @author lx
* @date 2021/10/13 14:34
**/
public class AbstractFactoryMain {
public void main() {
}
}
package com.pattern.FactoryPattern.FunctionFactory.materials;
import com.pattern.FactoryPattern.FunctionFactory.materials.factory.FactoryA;
import com.pattern.FactoryPattern.FunctionFactory.materials.factory.FactoryB;
/**
* 主方法
*
* @author lx
* @date 2021/10/13 14:34
**/
public class FunctionFactoryMain {
public void main() {
FactoryA a = new FactoryA();
System.err.println("工厂A生产:" + a.getProduct().findName());
FactoryB b = new FactoryB();
System.err.println("工厂B生产:" + b.getProduct().findName());
}
}
package com.pattern.FactoryPattern.FunctionFactory.materials.factory;
import com.pattern.FactoryPattern.FunctionFactory.materials.po.Product;
/**
* @author lx
*/
public interface Factory {
/**
* 获取产品
* @return 产品
*/
Product getProduct();
}
package com.pattern.FactoryPattern.FunctionFactory.materials.factory;
import com.pattern.FactoryPattern.FunctionFactory.materials.po.Product;
import com.pattern.FactoryPattern.FunctionFactory.materials.po.ProductA;
/**
* @author lx
*/
public class FactoryA implements Factory {
/**
* 获取产品
*
* @return 产品
*/
@Override
public Product getProduct() {
return new ProductA();
}
}
package com.pattern.FactoryPattern.FunctionFactory.materials.factory;
import com.pattern.FactoryPattern.FunctionFactory.materials.po.Product;
import com.pattern.FactoryPattern.FunctionFactory.materials.po.ProductB;
/**
* @author lx
*/
public class FactoryB implements Factory {
/**
* 获取产品
*
* @return 产品
*/
@Override
public Product getProduct() {
return new ProductB();
}
}
package com.pattern.FactoryPattern.FunctionFactory.materials.po;
/**
* @author lx
* @date 2021/10/13 14:55
**/
public interface Product {
/**
* 获取名称
*
* @return 名称
*/
String findName();
}
package com.pattern.FactoryPattern.FunctionFactory.materials.po;
/**
* @author lx
* @date 2021/10/13 14:55
**/
public class ProductA implements Product{
/**
* 获取名称
*
* @return 名称
*/
@Override
public String findName() {
return "ProductA";
}
}
package com.pattern.FactoryPattern.FunctionFactory.materials.po;
/**
* @author lx
* @date 2021/10/13 14:55
**/
public class ProductB implements Product {
/**
* 获取名称
*
* @return 名称
*/
@Override
public String findName() {
return "ProductB";
}
}
......@@ -3,5 +3,18 @@
*/
package com.pattern.FactoryPattern.FunctionFactory;
/*
说明:
* 又称工厂模式、多态工厂模式和虚拟构造器模式,通过定义工厂父类负责定义创建对象的公共接口,而子类则负责生成具体的对象。一种常用的对象创建型设计模式,此模式的核心精神是封装 类中不变的部分
作用:
* 将类的实例化(具体产品的创建)延迟到工厂类的子类(具体工厂)中完成,即由子类来决定应该实例化(创建)哪一个类。
优点:
* 符合开-闭原则:新增一种产品时,只需要增加相应的具体产品类和相应的工厂子类即可
* 符合单一职责原则:每个具体工厂类只负责创建对应的产品
缺点:
* 增加了系统的复杂度:类的个数将成对增加
* 增加了系统的抽象性和理解难度
* 一个具体工厂只能创建一种具体产品
理解性描述:
* 一个抽象产品类,可以派生出多个具体产品类。一个抽象工厂类,可以派生出多个具体工厂类。每个具体工厂类只能创建一个具体产品类的实例。
*
*/
......@@ -15,4 +15,5 @@ public class SimpleFactoryMain {
Coffee instance = new CoffeeFactory().createInstance("Americano");
System.err.println("老板来一杯".concat(instance.findName()));
}
}
package com.pattern;
import com.pattern.FactoryPattern.SimpleFactory.materials.SimpleFactoryMain;
import com.pattern.StrategyPattern.materials.StrategyMain;
/**
* @author lx
*/
......@@ -17,8 +14,9 @@ public class PatternApplication {
//new StrategyMain().main();
//工厂模式 -- 简单工厂
new SimpleFactoryMain().main();
//new SimpleFactoryMain().main();
//工厂模式 -- 方法工厂
//new FunctionFactoryMain().main();
//工厂模式 -- 抽象工厂
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册