Design pattern prototype

Source: Internet
Author: User

Prototype pattren)

In prototype mode, a prototype instance is used to specify the type of the object to be created, and a new object is created by copying the prototype.


After the examination is over, the school has a holiday, and we have entered the Learning Mode of full-time improvement:

-- Get up (of course, young people always love sleep, always stay in bed in the morning, and sometimes get up)
-- Buy breakfast at the middle door and bring it to the IDC
-- Go home for lunch and take a nap
-- I started my afternoon study again.
-- Go home for dinner and go to the IDC
-- Go home, wash and go to bed
In this way, we live every day. Only Thursday can we relax and have a good rest. We must have ideals and goals so that we can have the motivation to learn and live!


Let's get down to the truth. Next, let's take a look at the relationship between our daily life and the prototype model we are talking about today. From the above, we can see that our daily life is a cycle. What is a cycle? This is to repeat the same action. The concept of the prototype mode is to use the prototype instance to specify the type of object to be created, that is, our daily life. If we want to generate a daily life pattern for many days, you only need to assign values to the objects created by the prototype, instead of having to create an instance object every time. This will greatly save the various resources used to create objects. Next, let's take a look at how to implement it.

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace prototype mode {// prototype class Daylife: icloneable {private string getup; private string getfood; private string noon; private string afternoon; private string dinner; private string Gohome; public void setgetup (string getup) {This. getup = getup;} public void setgetfood (string getfood) {This. getfood = getfood;} public void setnoon (string noon) {This. noon = noon;} public void setafternoon (string afternoon) {This. afternoon = afternoon;} public void setdinner (string dinner) {This. dinner = dinner;} public void setgohome (string Gohome) {This. gohome = Gohome;} // display public void display () {console. writeline (getup); console. writeline (getfood); console. writeline (noon); console. writeline (afternoon); console. writeline (dinner); console. writeline (Gohome); console. writeline ();} public object clone () {return (object) This. memberwiseclone () ;}} class program {static void main (string [] ARGs) {Daylife A = new Daylife ();. setgetup ("get up at");. setgetfood ("go to the middle door to buy breakfast and bring it to the machine room");. setnoon ("go home for lunch at and take a nap");. setafternoon ("another afternoon study started at");. setdinner ("go home for dinner at and go to the data center");. setgohome ("go home at, wash and go to bed");. display (); Daylife B = (Daylife). clone (); B. setgetup ("bed in the morning, get up at"); B. display ();}}}





We can see that only the first Daylife object is created through new, and the second is generated by clone! This is the prototype mode: a new object is generated by cloning an existing object using its existing conditions, and some content is modified to form a new object entity!




The prototype schema includes the following roles:

Prototype (Abstract prototype ):It is the interface for declaring the clone method. It is the public parent class of all the specific prototype classes, which can be an abstract class, an interface, or even a specific implementation class.

Concreteprototype (prototype ):It implements the clone method declared in the abstract prototype and returns a clone object of its own in the clone method.

Client (customer class ):Let a prototype clone itself to create a new object. In the customer class, you only need to create a prototype object directly by instantiating it or using the factory method, then, multiple identical objects can be obtained by calling the object cloning method. Because the customer class is programmed for the prototype of the abstract prototype, you can select a specific prototype as needed. The system has good scalability and is convenient to add or replace a specific prototype.



When using the prototype mode, note the following:

1. The object construction method is not executed when the object is cloned.

Generally, when an object is created using the new operator, the class constructor is executed, and some data loading or initialization operations can be performed in the constructor. However, when the clone method is used to generate a new object in the class implementing the cloneable interface, the constructor of the class will not be executed!


2. Shallow replication and deep Replication

Shortest: only copies the original data types of the object, such as int, float, and string. It does not copy arrays or object references.

Deep replication: not only copies the original data type, but also copies the array and object reference in the object to completely replicate the object.

Main advantages:

1. When the object instance for information creation is complex, you can use the prototype mode to simplify the object creation process. By copying an existing instance, you can improve the efficiency of creating a new instance.

2. the prototype mode provides a simplified creation structure. The factory method requires a factory level structure that is the same as the product type registration structure, in the prototype, product replication is implemented by the clone method encapsulated in the prototype class, and no special factory class is required to create the product.

3. you can use the deep replication method to save the object state. In the prototype mode, copy an object and save its State, this function can be used as needed (for example, to return to a historical status) to assist in undo operations.


Main disadvantages:

1. You need to configure a clone method for each class, And the clone method is located inside the class. When modifying an existing class, you need to modify the source code, which violates the "open/closed principle"

2. complex code is required for deep replication. When multiple nested references exist between objects, the classes corresponding to each layer of objects must support deep replication for deep replication, it may be difficult to implement

Usage:

1. The process of generating objects is complicated, and many resources are required for comfort;

2. If you want to separate the framework prototype from the generated object;

3. the same object may be accessed by other callers;








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.