Prototype模式(设计模式)
2020-05-12 本文已影响0人
DarknessShadow
适用场景:
JAVA创建类实例基本上是适用new关键字。有时候需要使用 “在不指定类名的前提下生成实例”的需求。
对象种类繁多,无法将他们整合到一个类中
难以根据类生成实例
解耦框架和生成的实例
一旦在类中使用到了别的类名,就意味着该类与其他类紧密的耦合在一起了
一旦在代码中出现要使用的类的名字,就不能和该类分离,也无法实现复用。也就意味着两者是耦合在一起的。
clone方法和java.lang.Clonable接口
被复制对象的类必须实现java.lang.Cloneable接口,否则调用clone()会产生CloneNotSupportedException异常。
clone()方法进行的是浅拷贝
Cloneable接口只是一个标识接口,clone是Object类中的native方法(https://www.cnblogs.com/KingIceMou/p/7239668.html)
Prototype模式.png
小案例
Product.java
package framework;
/**
* @Author:DarknessShadow
* @description:框架 - 产品类
* @date 2020/5/12 13:36
*/
public interface Product extends Cloneable {
public abstract void use(String s);
public abstract Product creatClone();
}
Manager.java
package framework;
import java.util.HashMap;
import java.util.Map;
/**
* @Author:DarknessShadow
* @description:框架 - 管理类
* @date 2020/5/12 13:53
*/
public class Manager {
private Map showcase = new HashMap();
public void register(String name, Product proto) {
showcase.put(name, proto);
}
public Product create(String protoname) {
Product p = (Product) showcase.get(protoname);
return p.creatClone();
}
}
MessageBox.java
import framework.Product;
/**
* @Author:DarknessShadow
* @description:
* @date 2020/5/12 13:59
*/
public class MessageBox implements Product {
private char docochar;
public MessageBox(char docochar) {
this.docochar = docochar;
}
@Override
public void use(String s) {
int length = s.getBytes().length;
for (int i = 0; i < length + 4; i++) {
System.out.print(docochar);
}
System.out.println("");
System.out.println(docochar + " " + s + " " + docochar);
for (int i = 0; i < length; i++) {
System.out.print(docochar);
}
System.out.println("");
}
@Override
public Product creatClone() {
Product p = null;
try {
p = (Product) clone();
} catch (Exception e) {
e.printStackTrace();
}
return p;
}
}
UnderllinePen.java
import framework.Product;
/**
* @Author:DarknessShadow
* @description:
* @date 2020/5/12 14:08
*/
public class UnderlinePen implements Product {
private char ulchar;
public UnderlinePen(char ulchar) {
this.ulchar = ulchar;
}
@Override
public void use(String s) {
int length = s.getBytes().length;
System.out.println("\"" + s + "\"");
System.out.print(" ");
for (int i = 0; i < length; i++) {
System.out.print(ulchar);
}
System.out.println("");
}
@Override
public Product creatClone() {
Product p = null;
try {
p = (Product) clone();
} catch (Exception e) {
e.printStackTrace();
}
return p;
}
}
Main.java
import framework.Manager;
import framework.Product;
/**
* @Author:DarknessShadow
* @description:主测试程序
* @date 2020/5/12 14:11
*/
public class Main {
public static void main(String[] args) {
Manager manager = new Manager();
UnderlinePen upen = new UnderlinePen('~');
MessageBox mbox = new MessageBox('*');
MessageBox sbox = new MessageBox('/');
manager.register("Strong message", upen);
manager.register("warning box", mbox);
manager.register("slash box", sbox);
Product p1 = manager.create("Strong message");
p1.use("hello , world");
Product p2 = manager.create("warning box");
p2.use("hello, world.");
Product p3 = manager.create("slash box");
p3.use("hello, world.");
}
}