Step by step. NET Design Patterns learning notes 5. Prototype (Prototype) Overview

Source: Internet
Author: User

Overview

In software systems, product classes sometimes change dynamically and have a certain hierarchical structure. In this case, if the factory model is used, the factory method classes parallel to the product class hierarchy will also change with this change, which is obviously not suitable. So how can we encapsulate this dynamic change? So that the client programs dependent on these variable objects do not change with the product class?

Intention

In software systems, the creation of "some objects with complex structures" is often faced. due to changes in requirements, these objects are often faced with drastic changes, however, they have stable and consistent interfaces. How to deal with this change? How can we isolate "these variable objects" from "customer programs (programs using these objects)" so that "customer programs dependent on these variable objects" will not change as demand changes?

<Design Pattern> example

Biological examples

In Prototype mode, a Prototype instance is used to specify the type of the object to be created. The prototype of a new product is usually created before all products. Such prototype is passive and does not participate in copying itself. The split of a cell produces two identical cells. This is an example of assuming an active role to copy its own prototype. This demonstrates the prototype. One Cell Division produces two cells of the same genotype. In other words, the cell clones itself.

Dependency inversion
-Abstract A directly depends on implementation details B
Abstraction should not depend on implementation details, but on abstraction.


-Abstract A depends on abstract B.
Details B depends on abstract B

Example:

A wolf is very team-oriented. Every appearance is a group of wolves. This time, we give an example of the appearance of four wolves. we only need to provide a wolf instance, and all the others will copy it and change the attribute. this mode is exactly our prototype.

Structure:

The following code is created:

Create Location. cs First:

Html # viewSource "commandName =" viewSource "highlighterId =" highlighter_504075 "> view sourceprint?
01 [Serializable]
02 publicclass Location
03  {
04     private int _X;
05     private int _Y;
06     public int X
07     {
08         get { return _X; }
09         set { _X = value; }
10     }
11     public int Y
12     {
13         get { return _Y; }
14         set { _

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.