Home  >  Article  >  Java  >  Design Pattern Prototype Pattern Example Tutorial

Design Pattern Prototype Pattern Example Tutorial

零下一度
零下一度Original
2017-06-27 10:26:331131browse

Definition (From Baidu Encyclopedia)
Use prototype instances to specify the types of objects to be created, and create new objects by copying these prototypes.

UML class diagram:

Specific code:

public class Client {public static void main(String[] args) {//        Director d = new Director(new ConcreteBuilder());//        d.construct();ConcretePrototype1 prototype = new ConcretePrototype1();for (int i = 0; i < 10; i++) {
            ConcretePrototype1 x = (ConcretePrototype1) prototype.clone();
            x.test1();
        }
    }
}public class Prototype  implements Cloneable {public Prototype clone() {
        Prototype prototype = null;try{
            prototype = (Prototype)super.clone();
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }return prototype;
    }
}public class ConcretePrototype1 extends Prototype {public void test1() {
        System.out.println(this);
        System.out.println("123");
    }
}

Description of each part:
Prototype prototype class implements the Cloneable interface.
ConcretePrototype1 Subclass of the prototype class Prototype

Mode details:
Copy an object instance through cloning
The cloned new object copies the value of the original instance

Notes on prototype mode:
Using prototype mode to copy an object will not call the constructor of the class. Because the copy of the object is completed by calling the clone method of the Object class, even the access permissions are invalid for the prototype mode. Remember the singleton pattern? In singleton mode,
As long as the access permission of the constructor is set to private, a singleton can be implemented. However, the clone method directly ignores the permissions of the constructor method. Therefore, the singleton mode conflicts with the prototype mode, so special attention should be paid when using it.
The clone method performs a shallow copy, which means that if it is a reference type attribute, it will not copy, but only copy the reference.

Advantages and disadvantages:
Advantages:
The clone method is executed by the virtual machine directly copying the memory block, which is much faster than new
Obtaining the object at runtime And the corresponding status
Disadvantages:
Prototype must implement the clone method. If the attributes in the Prototype have many reference types, or even applications in objects, then the clone method must be implemented recursively, which will be very complicated. When adding reference attributes , will change the implementation of the clone method.

The above is the detailed content of Design Pattern Prototype Pattern Example Tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn