Java design mode-Original Model mode and java design mode Model

Source: Internet
Author: User

Java design mode-Original Model mode and java design mode Model

  • Introduction

  The original model mode belongs to the object creation mode. Specify the type of the object to be created through a prototype object, and then use the method of copying the prototype object to create more objects of the same type.

All Java classes are inherited from the java. lang. Object class. The Object class provides the clone () method to copy objects. Generally, the clone () method must meet the following conditions:

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, x. clone (). getClass () = x. getClass (). That is, the cloned object is of the same type as the original object.

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

  • Original Model Mode

  The original model mode can be divided into two types: 1. Simple Form; 2. Registration Form. The following two types are explained respectively.

Simple Form of original model pattern class diagram:

Three roles in this mode:

1. Customer role: the customer initiates a request to create an object.

2. Abstract prototype: an abstract role that provides the interface required for a specific prototype.

3. Specific prototype: The Copied object must implement the interface required by the abstract prototype.

Abstract prototype code:

1 public interface Prototype extends Cloneable{2     public Object clone() throws CloneNotSupportedException;3 }

Specific prototype code:

1 public class ConcrecePrototype implements Prototype{2     public Object clone() throws CloneNotSupportedException {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 operation(Prototype example) throws CloneNotSupportedException{4         Prototype p = (Prototype) example.clone();5     }6 }

Original Model class diagram in registration form:

Roles in this mode:

1. Customer role: the customer initiates a request to create an object.

2. Abstract prototype: an abstract role that provides the interface required for a specific prototype.

3. Specific prototype: The Copied object must implement the interface required by the abstract prototype.

4. Prototype MANAGER: creates a specific prototype object and records each created object.

Abstract prototype code:

1 public interface Prototype extends Cloneable{2     public Object clone() throws CloneNotSupportedException;3 }

Specific prototype code:

 1 public class ConcrecePrototype implements Prototype{ 2     public synchronized Object clone() throws CloneNotSupportedException { 3         Prototype temp = null; 4         try { 5             temp = (Prototype) super.clone(); 6         } catch (Exception e) { 7             System.out.println("clone fail"); 8         }finally{ 9             return temp;10         }11     }12 }

Prototype Manager Code:

 1 public class PrototypeManager { 2     private Vector vector = new Vector(); 3      4     public void add(Prototype e){ 5         vector.add(e); 6     } 7      8     public Prototype get(int i){ 9         return (Prototype) vector.get(i);10     }11 }

Client code:

1 public class Client {2     private PrototypeManager pm;3     private Prototype p;4     5     public void registerPrototype(Prototype prototype) throws CloneNotSupportedException{6         Prototype temp = (Prototype) prototype.clone();7         pm.add(temp);8     }9 }

We can see that if the prototype object to be created is small and fixed, you can consider using a simple prototype mode. If the prototype object to be created is not fixed, you can use the registered prototype. The prototype objects are stored in the original type manager. If any of the prototype objects are taken out directly, copy them and add them to the prototype objects.

  • Deep Dive

  Here we will think of important topics in java: shallow replication and deep replication.

Shallow copy: all variables of the Copied object have the same value as the original object, and all references to the object point to the original object. For a light copy, only the objects to be copied are considered, rather than the objects referenced by the objects to be copied.

Deep replication: all variables of the Copied object have the same value as the original object, and those variables that reference other objects point to the new object to be copied. In deep replication, variables that reference other objects will point to the new object that has been copied, that is, deep replication copies all the objects referenced by the object to be copied.

  • Advantages and disadvantages of original model Mode

Advantages:

  1. The original model mode allows you to dynamically add or remove product classes. Because the method for creating a product instance is internal to the product class, adding a new product has no impact on the entire structure.

2. The original model mode provides a simplified creation structure. The factory method mode often requires a structure with the same grade as the product class, which is not required in the original model mode.

3. Ability to dynamically load new functions for an application.

4. Product classes do not need to have any pre-determined level structure, because the original model mode applies to any level.

  Disadvantages:

  The primary disadvantage of the original model mode is that a replication method is required for each class.

In addition, if there are indirect objects in the prototype object, you can set the indirect object to transient and do not copy it, or create a similar object on your own.

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.