Design Pattern (6)-prototype Pattern

Source: Internet
Author: User

The prototype mode is an object creation mode.
Use a prototype instance to specify the type of the object to be created and copy the prototype to create a new object.
The prototype mode allows an object to create another custom object without any details.
The basic working principle of the prototype mode is to pass a prototype object to the object to be created
The object to be created is created by requesting the prototype object to copy the prototype itself.

The prototype includes three roles,
Abstract prototype class: defines interfaces with methods to clone itself
Specific prototype: implements a specific cloning method and returns a clone object of the current user in the cloning method.
Let a prototype clone itself to create a new object
Customer class: you only need to instantiate an object directly, and then copy multiple identical objects by calling the object cloning method.

Parent class Prototype Product

package org.test.design.protype;public class Product implements Cloneable{private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public Object copy(){        try {            return super.clone();        } catch (Exception e) {            e.printStackTrace();            return null;        }    }}

Specific subclass producta

package org.test.design.protype;public class ProductA extends Product{public ProductA(){}public ProductA(String name){super.setName(name);}public void myFunction(){System.out.println("ProductA myFunction");}}

Client call test:

Package Org. test. design. protype; public class testmain {/*** @ Param ARGs */public static void main (string [] ARGs) {product Pa = new producta ("AAA"); system. out. println (Pa. getname (); product pa_copy1 = (product) Pa. copy (); system. out. println (pa_copy1.getname () + "--" + (Pa = pa_copy1); product pa_copy2 = (product) Pa. copy (); system. out. println (pa_copy2.getname () + "--" + (Pa = pa_copy2); system. out. println ("------------ ---- "); // You need to understand the meaning of the cloned object. If you want to create the following object, it is not actually created. Both object B and Object C have the same reference as object, // That is to say, only one object a exists in the memory. Product A = new producta ("AAA"); system. out. println ("A --" +. hashcode (); product B = A; system. out. println ("B --" + B. hashcode (); system. out. println (A = B); product C = new product (); C = A; system. out. println ("c --" + C. hashcode (); system. out. println (A = C) ;}/ * print aaaaaa -- falseaaa -- false ---------------- A -- 6413875b -- 6413875truec -- 6413875 true */

In my opinion, the prototype mode is an object copy, and the type of the Copied object is the defined parent class,
This parent class is a prototype of this class of objects. That is to say, the copied objects are all transformation objects,
Only attributes and methods inherited from the parent class are retained. Unlike copying
Because it is a transformation object, the private attributes and methods of the existing object are filtered, and the object is returned to the parent class prototype.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.