What is a Prototype pattern?
--Prototype: Create an instance from "prototype". Not from class. --There is an entity class that implements the Cloneable interface and can clone itself.
Actions such as drawing a rectangle are common, but at that time
-Rectangle surrounded by "/" -Rectangle surrounded by "*" --Rectangle surrounded by "x"
When there are three templates that are divided like this, there is no point in making each in a separate class.
Classify only the shape of the figure to be drawn, and map the line type to Client as the same class.
And when repeating the same "drawing a triangle with the line of" / "", you can clone a new instance from the Prototype instance registered as a template and use it.
--Client calls Prototype --ConcretePrototype that implements Prototype is cloned by createClone
Character
--framework package --Product interface: Inherit Cloneable --Manager class: Cloneable is stored in Map and cloned --entity package --Main: Entry point --MessageBox class: Implemented Product. Draw Rectangle / ConcreteProduct --UnderLinePen class: Implemented Product. Underline / ConcreteProduct
Product
Product
package prototype.product.frameworks;
public interface Product extends Cloneable{
public abstract void use(String s);
public abstract Product createClone();
}
Manager
package prototype.product.frameworks;
import java.util.HashMap;
public class Manager {
private HashMap<String, Cloneable> showcase = new HashMap<>();
public void register(String name, Cloneable prototype) {
showcase.put(name, prototype);
}
public Product create(String protoname) {
Product p = (Product)showcase.get(protoname);
return p.createClone();
}
}
MessageBox
package prototype.product.entity;
import prototype.product.frameworks.Product;
public class MessageBox implements Product {
private char decoChar;
public MessageBox(char decoChar) {
this.decoChar = decoChar;
}
@Override
public void use(String s ) {
int length = s.getBytes().length;
for (int i = 0; i < length+4; i++) {
System.out.print(decoChar);
}
System.out.println("");
System.out.println(decoChar+" "+s + " " +decoChar);
for (int i = 0; i <length+4; i++) {
System.out.print(decoChar);
}
System.out.println("");
}
@Override
public Product createClone() {
Product product = null;
try {
product= (Product)clone();
}catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return product;
}
}
UnderLinePen
package prototype.product.entity;
import prototype.product.frameworks.Product;
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+" ¥");
for (int i = 0; i < length+4; i++) {
System.out.print(ulChar);
}
System.out.println("");
}
@Override
public Product createClone() {
Product product = null;
try {
product = (Product) clone();
}catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return product;
}
}
Main
package prototype.product.entity;
import prototype.product.frameworks.Manager;
import prototype.product.frameworks.Product;
public class Main {
public static void main(String[] args) {
Manager manager = new Manager();
UnderLinePen linePen = new UnderLinePen('x'); //1
MessageBox mbox = new MessageBox('*'); //1
MessageBox sbox = new MessageBox('/'); //1
manager.register("Emphasized message", linePen);// 2
manager.register("Warning box",mbox); //2
manager.register("Diagonal box", sbox); //2
//Generate
Product p1 = manager.create("Emphasized message");//3
p1.use("Hello world"); //4
Product p2 = manager.create("Warning box"); //3
p2.use("Hello world"); //4
Product p3 = manager.create("Diagonal box"); //3
p3.use("Hello world"); //4
}
}
Execution result
¥ Hello world ¥
xxxxxxxxxxxxxxx
***************
* Hello world *
***************
///////////////
/ Hello world /
///////////////
--Product is the role of Prototype --MessageBox, UnderLinePen is the role of ConcretePrototype --Manager is the role of Client
[6 . Prototype pattern \ | TECHSCORE ](https://www.techscore.com/tech/DesignPattern/Prototype.html/)
Recommended Posts