提交 03521015 编写于 作者: C chenjianqiang

增加工厂模式(简单工厂模式)

上级 d4fd81c2
......@@ -2,18 +2,19 @@
<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$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/pattern/InterpreterPattern/po/AndExpression.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/InterpreterPattern/materials/po/AndExpression.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/pattern/InterpreterPattern/po/Expression.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/InterpreterPattern/materials/po/Expression.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/pattern/InterpreterPattern/po/InterpreterMain.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/InterpreterPattern/materials/InterpreterMain.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/pattern/InterpreterPattern/po/OrExpression.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/InterpreterPattern/materials/po/OrExpression.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/pattern/InterpreterPattern/po/TerminalExpression.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/InterpreterPattern/materials/po/TerminalExpression.java" afterDir="false" />
<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/PatternApplication.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/PatternApplication.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/pattern/StrategyPattern/po/Context.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/StrategyPattern/materials/po/Context.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/pattern/StrategyPattern/po/OperationA.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/StrategyPattern/materials/po/OperationA.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/pattern/StrategyPattern/po/OperationB.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/StrategyPattern/materials/po/OperationB.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/pattern/StrategyPattern/po/Strategy.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/StrategyPattern/materials/po/Strategy.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/pattern/StrategyPattern/po/StrategyMain.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/pattern/StrategyPattern/materials/StrategyMain.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -24,8 +25,8 @@
<option name="RECENT_TEMPLATES">
<list>
<option value="Interface" />
<option value="Class" />
<option value="package-info" />
<option value="Class" />
</list>
</option>
</component>
......@@ -98,15 +99,19 @@
</component>
<component name="PropertiesComponent">
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/src/com/pattern/InterpreterPattern/examples" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/src/com/pattern/FactoryPattern/AbstractFactory" />
<property name="vue.rearranger.settings.migration" value="true" />
</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="E:\chen\patterns\src\com\pattern\FactoryPattern\AbstractFactory" />
<recent name="E:\chen\patterns\src\com\pattern\FactoryPattern\FunctionFactory" />
<recent name="E:\chen\patterns\src\com\pattern\FactoryPattern\SimpleFactory\examples" />
<recent name="E:\chen\patterns\src\com\pattern\InterpreterPattern\examples" />
<recent name="E:\chen\patterns\src\com\chen\StrategyPattern" />
</key>
<key name="CopyClassDialog.RECENTS_KEY">
<recent name="com.pattern.FactoryPattern.SimpleFactory" />
<recent name="com.chen.StrategyPattern.po" />
<recent name="com.company.InterpreterPattern" />
</key>
......@@ -140,7 +145,7 @@
<workItem from="1634085227295" duration="1967000" />
<workItem from="1634087918450" duration="3646000" />
<workItem from="1634091679401" duration="1323000" />
<workItem from="1634093719115" duration="6342000" />
<workItem from="1634093719115" duration="8837000" />
</task>
<task id="LOCAL-00001" summary="增加策略模式案例&#10;Signed-off-by: chenjianqiang &lt;wushanxi@gmail.com&gt;">
<created>1634096220780</created>
......
package com.pattern.FactoryPattern.SimpleFactory.materials;
import com.pattern.FactoryPattern.SimpleFactory.materials.factory.CoffeeFactory;
import com.pattern.FactoryPattern.SimpleFactory.materials.po.Coffee;
/**
* 主方法
*
* @author lx
* @date 2021/10/13 14:34
**/
public class SimpleFactoryMain {
public void main() {
Coffee instance = new CoffeeFactory().createInstance("Americano");
System.err.println("老板来一杯".concat(instance.findName()));
}
}
package com.pattern.FactoryPattern.SimpleFactory.materials.factory;
import com.pattern.FactoryPattern.SimpleFactory.materials.po.Americano;
import com.pattern.FactoryPattern.SimpleFactory.materials.po.Cappuccino;
import com.pattern.FactoryPattern.SimpleFactory.materials.po.Coffee;
import com.sun.applet2.AppletParameters;
import java.util.HashMap;
import java.util.Map;
/**
* 工厂
*
* @author lx
* @date 2021/10/13 14:17
**/
public class CoffeeFactory {
private static final Map<String, Coffee> COFFEE_MAP = new HashMap<>(10);
static {
COFFEE_MAP.put("Cappuccino", new Cappuccino());
COFFEE_MAP.put("Americano", new Americano());
}
public Coffee createInstance(String name) {
return COFFEE_MAP.get(name);
}
}
package com.pattern.FactoryPattern.SimpleFactory.materials.po;
/**
* 美式
*
* @author lx
* @date 2021/10/13 14:15
**/
public class Americano extends Coffee {
/**
* 获取名称
*
* @return 名称
*/
@Override
public String findName() {
return "美式";
}
}
package com.pattern.FactoryPattern.SimpleFactory.materials.po;
/**
* 卡布奇诺
*
* @author lx
* @date 2021/10/13 14:16
**/
public class Cappuccino extends Coffee {
/**
* 获取名称
*
* @return 名称
*/
@Override
public String findName() {
return "卡布奇诺";
}
}
package com.pattern.FactoryPattern.SimpleFactory.materials.po;
/**
* 咖啡
* 可以做成接口或者抽象类
*
* @author lx
* @date 2021/10/13 14:13
**/
public abstract class Coffee {
/**
* 获取名称
*
* @return 名称
*/
public abstract String findName();
}
......@@ -3,5 +3,15 @@
*/
package com.pattern.FactoryPattern.SimpleFactory;
/*
说明:
* 属于创建型模式,又叫做静态工厂方法模式,不属于23种GOF设计模式之一。
* 是由一个工厂对象决定创建出哪一种产品类的实例。
* 实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类(这些产品类继承自一个父类或接口)的实例。
作用:
* 将“类实例化的操作”与“使用对象的操作”分开,让使用者不用知道具体参数就可以实例化出所需要的“产品”类,从而避免了在客户端代码中显式指定,实现了解耦。
缺点:
* 违背“开放 - 关闭原则”,一旦添加新产品就不得不修改工厂类的逻辑,这样就会造成工厂逻辑过于复杂。
理解性描述:
* 一个抽象产品类,可以派生出多个具体产品类。一个具体工厂类,通过往此工厂的static方法中传入不同参数,产出不同的具体产品类实例。
*
*/
/**
* 工厂模式
*/
package com.pattern.FactoryPattern;
/*
* 工厂模式大体分为简单工厂、工厂方法、抽象工厂等三种模式。
* 工厂方法模式也可称为工厂模式,与抽象模式都是属于GOF23种设计模式中的一员。
* 可以大概理解为:简单工厂进阶变成了工厂方法,然后再进阶成了抽象工厂。
* 难度逐步增加,也越来越抽象。下面按先易到难逐个分析。
*/
package com.pattern;
import com.pattern.FactoryPattern.SimpleFactory.materials.SimpleFactoryMain;
import com.pattern.StrategyPattern.materials.StrategyMain;
/**
......@@ -13,8 +14,12 @@ public class PatternApplication {
//new InterpreterMain().main();
//策略模式
new StrategyMain().main();
//new StrategyMain().main();
//工厂模式 -- 简单工厂
new SimpleFactoryMain().main();
//工厂模式 -- 方法工厂
//工厂模式 -- 抽象工厂
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册