Java design pattern-Primitive Model mode

Source: Internet
Author: User

    • Brief introduction

  The original model schema belongs to the creation mode of the object. A prototype object is used to indicate the type of object to create and then to create more objects of the same type with the method of copying the prototype object.

All Java classes inherit from the Java.lang.Object class, and the object class provides the Clone () method to copy the objects. The general call to the Clone () method requires that the condition be met:

1, for any object x, there are: X.clone ()!=x. That is, the cloned object and the original object are not an object.

2. For any object x, there are: X.clone (). GetClass () =x.getclass (). That is, the cloned object is the same type as the original object.

3. If the equal () method of object x is properly defined, then X.clone (). equal (x) should be established.

    • Original model mode

  The original model pattern is divided into two types: 1, simple form, 2, the form of registration. Each of these types is explained below.

Primitive model pattern class diagram in simple form:

Three roles for this pattern:

1. Customer role: The customer proposes to create the object request.

2, abstract Prototype: Abstract role, give the specific prototype required interface.

3. Concrete prototype: The object to be copied, the interface needed to implement the abstract prototype.

Abstract prototype Code:

1  Public Interface extends cloneable{2      Public throws clonenotsupportedexception; 3 }

Specific prototype code:

1  Public classConcreceprototypeImplementsprototype{2      PublicObject Clone ()throwsclonenotsupportedexception {3         Try {4             return Super. Clone ();5}Catch(Exception e) {6             return NULL;7         }8     }9}

Customer Role Code:

1  Public class Client {2     Private Prototype Prototype; 3      Public void throws clonenotsupportedexception{4         Prototype p = (Prototype) Example.clone (); 5     }6 }

The original model model class diagram of the registration form:

The role of this model:

1. Customer role: The customer proposes to create the object request.

2, abstract Prototype: Abstract role, give the specific prototype required interface.

3. Concrete prototype: The object to be copied, the interface needed to implement the abstract prototype.

4. Prototype Manager: Create a concrete prototype class object and record each created object.

Abstract prototype Code:

1  Public Interface extends cloneable{2      Public throws clonenotsupportedexception; 3 }

Specific prototype code:

1  Public classConcreceprototypeImplementsprototype{2      Public synchronizedObject Clone ()throwsclonenotsupportedexception {3Prototype temp =NULL;4         Try {5temp = (Prototype)Super. Clone ();6}Catch(Exception e) {7System.out.println ("Clone fail");8}finally{9             returntemp;Ten         } One     } A}

Prototype Manager code:

1  Public classPrototypemanager {2     PrivateVector vector =NewVector ();3     4      Public voidAdd (Prototype e) {5 Vector.add (e);6     }7     8      PublicPrototype Get (inti) {9         return(Prototype) vector.get (i);Ten     } One}

Client code:

1  Public classClient {2     PrivatePrototypemanager pm;3     PrivatePrototype p;4     5      Public voidRegisterprototype (Prototype Prototype)throwsclonenotsupportedexception{6Prototype temp =(Prototype) Prototype.clone ();7 Pm.add (temp);8     }9}

As you can see, you can consider using a simple form of prototype mode if you want to create a prototype object that is small and fixed. If you are creating a prototype object that is not fixed, you can use the prototype mode of the enlistment form. The prototype object is stored in the prototype manager, and if one of them is directly taken out, it is not copied and added to it.

    • Lead depth

  The important topics in Java are seen here: shallow copy and deep copy.

Shallow copy: All the variables of the copied object have the same value as the original object, and all references to its objects point to the original object. Shallow replication considers only the objects that are considered for replication, not the objects that it refers to.

Deep copy: All variables of the copied object have the same value as the original object, and those referencing the other object point to the new object being copied. The variables in the deep copy that refer to other objects will point to the new object being copied, that is, the deep copy copies the objects referenced by the object being copied.

    • Advantages and disadvantages of the original model pattern

Advantages:

  1. The original model model mode allows to dynamically increase or decrease the product class. Because the method of creating a product class instance is internal to the product class, adding new products has no effect on the entire structure.

2. The original model mode provides a simplified creation structure. The factory method pattern often requires a structure that is the same as the Product class level, which is not required for the original model pattern.

3. Has the ability to dynamically load new functions for an application software.

4, the product class does not need to have any predetermined hierarchy structure, because the original model mode applies to any class.

  Disadvantages:

  The main disadvantage of the original model pattern is the need to have a copy method for each class.

In addition, if there are indirect objects in the prototype object, you can set the indirect object to transient not copy or create the equivalent of the same object yourself.

Java design pattern-Primitive Model mode

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.