[JAVA] Prototype Pattern

Prototype Pattern Copy and use the instance

On this page ・ Confirmation of Prototype Pattern -Store and use the instance in the Map collection I will describe about

Design Pattarm MENU

Confirmation of Prototype Pattern

Check with the following class structure

class Explanation
samPrototype.class Have a method to make a copy of yourself
user(Main.class) Check the operation

If you use the clone () method, implement the java.lang.Cloneable interface

Let's check

samPrototype.class


class samPrototype implements Cloneable{         // java.lang.Implement Cloneable
  void todo(){System.out.println("prototype");}
  
  samPrototype getClone(){
    samPrototype pro = null;
    try{pro=(samPrototype) clone();              // clone()And make a copy of itself
    }catch(Exception e){}
    return pro;
  }
}

user(Main.class)


public static void main(String[] args){
  samPrototype pr = new samPrototype();
  pr.getClone().todo();
}}

Store and use instances in the Map collection

For Prototype Pattern, register multiple instances in advance There is a usage to execute clone () if necessary

Implement with the following configuration

name of the class Explanation
prototype.interface Implement Cloneable and clone()Define a method
prototypeImple.class prototype.Implement interface and clone()Implement the method
prototypeManager.class Keep a class instance that implements prototype in a collection
clone in prototype type()Issue an instance using a method
user(Main.class) Check the operation

Let's make a class

prototype.interface


interface prototype extends Cloneable{
  void todo();
  prototype exeClone();
}

prototypeImple.class


class prototypeImple implements prototype{
  String name;
  prototypeImple(String name){this.name=name;}

  public void todo(){System.out.println(name);}

  public prototype exeClone(){
      prototype pro = null;
      try{pro = (prototype) clone();}catch(Exception e){}
      return pro;
  }
}

usr(Main.class)


public static void main(String[] args){
  prototypeManager pm = new prototypeManager();
  pm.set(0, new prototypeImple("A"));
  pm.set(1, new prototypeImple("B"));
  prototype p1 = pm.getClone(0);
  prototype p2 = pm.getClone(1);
  p1.todo();
  p2.todo();
}}

Recommended Posts

Prototype pattern
Prototype Pattern
Design pattern ~ Prototype ~
Memento Pattern
Mediator pattern
Iterator pattern
Composite pattern
Observer Pattern
Builder pattern
Bridge Pattern
Command Pattern
Builder Pattern
Strategy pattern
Iterator Pattern
Visitor pattern
Adapter Pattern
Proxy Pattern
Strategy Pattern
Composite Pattern
Singleton pattern
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Facade pattern
Visitor Pattern
Bridge pattern
abstract Factory Pattern
Design pattern ~ Builder ~
[Java] Strategy pattern
Design pattern ~ Visitor ~
Java design pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Factory Method Pattern
Design pattern ~ Strategy ~
Design pattern ~ Composite ~
Design pattern (1): AbstractFactory
[Java] Singleton pattern
Abstract Factory pattern
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Template Method pattern
Design pattern ~ Decorator ~
Template Method Pattern
Design pattern ~ Interpreter ~
Factory Method pattern
Design pattern ~ Observer ~
[Java] Adapter pattern
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
Java pattern memo