Introduction to design patterns Prototype

Introduction to design patterns Prototype

Overview

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.

Miso

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.

Conceptual diagram

concept.cld.jpg

--Client calls Prototype --ConcretePrototype that implements Prototype is cloned by createClone

Implementation drawing

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

Implementation source

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 /
///////////////

Interpretation

  1. Set the characters to be drawn on Concrete Product
  2. Register Concrete Products in HashMap of Manager
  3. Create a clone with Manager.create → Product.createClone returns its own clone
  4. Draw with Product.use

Implementation class diagram

classes.cld.jpg

--Product is the role of Prototype --MessageBox, UnderLinePen is the role of ConcretePrototype --Manager is the role of Client

Quote

Augmented and revised edition Introduction to design patterns learned in Java language|Hiroshi Yuki|Computer / IT|Kindle Store| Amazon

[6 . Prototype pattern \ | TECHSCORE ](https://www.techscore.com/tech/DesignPattern/Prototype.html/)

Recommended Posts

Introduction to design patterns Prototype
Introduction to design patterns (introduction)
Introduction to Design Patterns (Builder)
Introduction to Design Patterns (Composite)
Introduction to design patterns (Flyweight)
Introduction to Design Patterns (Iterator)
Introduction to Design Patterns (Strategy)
Introduction to Design Patterns (Abstract Factory)
Important design patterns to improve maintainability
Introduction to Ruby 2
Introduction to SWING
Java Design Patterns
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to migration
Design pattern ~ Prototype ~
Introduction to java
Introduction to Doma
Introduction to JAR files
Introduction to Ratpack (8)-Session
Introduction to RSpec 1. Test, RSpec
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
Introduction to Android Layout
Introduction to Practical Programming
Introduction to javadoc command
Introduction to jar command
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Introduction to java command
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Study GoF design patterns
Introduction to javac command
I read Hiroshi Yuki "Introduction to Design Patterns Learned in Java Language" (SB Creative)
Read design patterns in Ruby
Introduction to RSpec 5. Controller specs
Introduction to Android application development
Introduction to RSpec 3. Model specs
Introduction to Ratpack (5) --Json & Registry
Introduction to Metabase ~ Environment Construction ~
(Dot installation) Introduction to Java8_Impression
Introduction to Micronaut 2 ~ Unit test ~
Introduction to JUnit (study memo)
Introduction to Spring Boot ① ~ DI ~
[Java] Introduction to lambda expressions
Introduction to Spring Boot ② ~ AOP ~
Introduction to Apache Beam (2) ~ ParDo ~
[Ruby] Introduction to Ruby Error statement
Introduction to EHRbase 2-REST API
GitHub Actions Introduction to self-made actions
[Java] Introduction to Stream API
Introduction to Spring Boot Part 1
Introduction to Ratpack (1) --What is Ratpack?
XVim2 introduction memo to Xcode12.3
Why design patterns are needed
[Java] Summary of design patterns
Introduction to RSpec-Everyday Rails Summary-