提交 45d8cc4c 编写于 作者: C chenjianqiang

原型模式 ╰(*°▽°*)╯

上级 decb603f
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
......@@ -3,7 +3,6 @@ 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;
......
......@@ -96,6 +96,16 @@ public class PatternApplication {
*/
/*
9.原型模式
new PrototypeMain().main();
2021-11-24: 传入一个类,被传入的对象调用类中的方法
*/
}
}
package com.pattern.PrototypePattern.materials;
import com.pattern.PrototypePattern.materials.po.Shape;
import com.pattern.PrototypePattern.materials.po.ShapeCache;
/**
* 主进程
* @author lx
* @date 2021/11/24 14:54
**/
public class PrototypeMain {
public void main() {
ShapeCache.init();
Shape c = ShapeCache.getShape("c");
System.err.println(c.getName());
}
}
package com.pattern.PrototypePattern.materials.po;
/**
* 圆形
*
* @author lx
* @date 2021/11/24 14:47
**/
public class Circle extends Shape {
private Integer x;
private Integer y;
public Integer getX() {
return x;
}
public void setX(Integer x) {
this.x = x;
}
public Integer getY() {
return y;
}
public void setY(Integer y) {
this.y = y;
}
}
package com.pattern.PrototypePattern.materials.po;
/**
* 图形抽象
*
* @author lx
* @date 2021/11/24 14:44
**/
public abstract class Shape implements Cloneable {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public Shape clone() {
try {
return (Shape) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}
package com.pattern.PrototypePattern.materials.po;
import java.util.Hashtable;
/**
* @author lx
* @date 2021/11/24 14:49
**/
public class ShapeCache {
private static final Hashtable<String, Shape> SHAPE_MAP = new Hashtable<String, Shape>();
public static Shape getShape(String name) {
Shape cachedShape = SHAPE_MAP.get(name);
return cachedShape.clone();
}
public static void init() {
// 很复杂的过程去生成这两个实体
Circle c = new Circle();
c.setName("c");
c.setX(1);
c.setY(2);
SHAPE_MAP.put("c", c);
Square s = new Square();
s.setName("s");
s.setA(99);
s.setB(88);
SHAPE_MAP.put("s", s);
}
}
package com.pattern.PrototypePattern.materials.po;
/**
* 正方形
*
* @author lx
* @date 2021/11/24 14:47
**/
public class Square extends Shape {
private Integer a;
private Integer b;
public Integer getA() {
return a;
}
public void setA(Integer a) {
this.a = a;
}
public Integer getB() {
return b;
}
public void setB(Integer b) {
this.b = b;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册