Design Pattern 8 (prototype pattern)

Source: Internet
Author: User

Prototype: Use a prototype instance to specify the object type and copy the prototype to create a new object.

Prototype, Prototype class, declares an interface for cloning itself

ConcretePrototype1 and ConcretePrototype2 are specific prototype classes to implement a clone operation.

Client, call, let a prototype clone itself to create a new object

In fact, the prototype mode is to create another custom object from an object without knowing any details of the Creation.

Prototype design code

ID {; Prototype (. ID =

ConcretePrototype

ConcretePrototype1 (Prototype)

This. MemberwiseClone () of the Clone method in its implementation class creates a superficial copy of the current object.

You can create a new object and copy the non-static fields of the current object to the new object. If the field is of the value type, perform a one-on-one copy of the field. If the field is of the reference type, copy the referenced object but do not copy the referenced object. Therefore, the original object and its copy reference the same object.
Client call

Main (= ConcretePrototype1 (=

Running Effect display

But. NET, Prototype is unnecessary for the Prototype abstract class, because cloning is too common, so. NET provides the ICloneable interface in the System namespace, which is the only method to Clone (). In this way, you only need to implement this interface to complete the prototype mode.

Related Article

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.