框架:
1、接口
package com.polo.proto.fremework;
public interface Product extends Cloneable{
public abstract void use(String s);
public abstract Product createClone();
}
2、使用者
package com.polo.proto.fremework;
import java.util.HashMap;
/**
* @program: data-structure
* @description: 框架
* @author: Miller.FAN
* @create: 2020-02-08 14:28
**/
public class Manager {
private HashMap 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.createClone();
}
}
3、避免重复代码出现
package com.polo.proto.fremework;
/**
* @program: data-structure
* @description: 实现接口的公共部分
* @author: Miller.FAN
* @create: 2020-02-08 19:16
**/
public class AbstractPhoto implements Product{
@Override
public void use(String s) {
}
@Override
public Product createClone() {
Product p = null;
try {
p = (Product)clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return p;
}
}
使用
1、子类一
package com.polo.proto.son;
import com.polo.proto.fremework.AbstractPhoto;
import com.polo.proto.fremework.Product;
/**
* @program: data-structure
* @description: son of product
* @author: Miller.FAN
* @create: 2020-02-08 14:35
**/
public class MessageBox extends AbstractPhoto implements Product {
private String decochar;
public MessageBox(String decochar) {
this.decochar = decochar;
}
@Override
public void use(String s) {
int length = s.getBytes().length;
String printChar = "";
for(int i = 0; i < length + 4; i++) {
printChar = printChar + decochar;
}
System.out.println(printChar);
System.out.println("");
System.out.println(decochar + " " + s + " " + decochar);
System.out.println("");
System.out.println(printChar);
System.out.println("");
}
}
2、子类二
package com.polo.proto.son;
import com.polo.proto.fremework.AbstractPhoto;
import com.polo.proto.fremework.Product;
/**
* @program: data-structure
* @description: 下划线
* @author: Miller.FAN
* @create: 2020-02-08 15:21
**/
public class UnderLinePen extends AbstractPhoto implements Product {
private String underLineString;
public UnderLinePen(String underLineString) {
this.underLineString = underLineString;
}
@Override
public void use(String s) {
System.out.println(" " + s + " ");
int length = s.getBytes().length;
String printChar = "";
for(int i = 0; i < length + 4; i++) {
printChar = printChar + underLineString;
}
System.out.println(printChar);
System.out.println("");
}
}
3、测试
package com.polo.proto.test;
import com.polo.proto.fremework.Manager;
import com.polo.proto.fremework.Product;
import com.polo.proto.son.MessageBox;
import com.polo.proto.son.UnderLinePen;
/**
* @program: data-structure
* @description: main
* @author: Miller.FAN
* @create: 2020-02-08 15:25
**/
public class protoTest {
public static void main(String[] args) {
//准备
Manager manager = new Manager();
MessageBox messageBox = new MessageBox("*");
UnderLinePen underLinePen = new UnderLinePen("_");
manager.register("waring box", messageBox);
manager.register("strong message" , underLinePen);
//生成
Product p1 = manager.create("waring box");
Product p2 = manager.create("strong message");
p1.use("miller");
p2.use("baby");
}
}
结果:
**********
* miller *
**********
baby
________