Design Mode note prototype

Source: Internet
Author: User

Design Mode note prototype

 

 

// ----------------------------- 15/04/07 ----------------------------

 

// Prototype: Object Creation Mode

 

/*

1: Intention: Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.

2: motivation:

3: Applicability:

1> when a system is independent of its product creation, composition, and representation

2> when the class to be instantiated is scheduled at the runtime, such as through dynamic loading

3> to avoid creating a factory class level parallel to the product class level

4> when an instance of a class can only have one of several combinations of different States

4: structure:

Client:

Prototype ---------------------------------> Prototype:

Operation () Clone ()

{P = prototype-> Clone ()} |

|

ConcretePrototype1: ConcretePrototype2:

Clone ()

{Return copy of self}

5: participants:

1> Prototype: declare an interface for cloning itself.

2> ConcretePrototype: implements a clone operation.

3> Client: let a prototype clone itself to create a new object.

6. Collaboration: the customer requests a prototype to clone itself.

7: effect:

1> advantages:

1) You can add or delete products at runtime to make them more flexible.

2) Change the value to specify a new object. You can specify a new value for the object to instance the new object,

This design can greatly reduce the number of classes required by the system.

3) change the structure to create new objects. For objects created by parts and child parts,

Reflects the Composite mode (changing the number or type of child) and the Decorator mode (unknown flag is unknown for the moment)

Here, Clone needs to be implemented as a deep copy. Otherwise, the structure of child is the same, and other structures will change.

4) Reduce the subclass construction. Compared with the Factory Method, you do not need to create a creator class hierarchy,

You only need to clone an object, instead of creating it by using the creator class.

5) dynamically configure applications with classes (do not understand <Unknown tag>)

2> defects:

Each subclass must implement the Clone operation. When the class already exists, it is difficult to add a Clone operation. When some internal

Supports copy (copying is not supported, so you cannot call the copy constructor to perform Clone operations) or circular references (to implement deep copy

It is also difficult to clone objects that need to be repeated.

8: Implementation:

1> use a prototype manager. When the number of original types in a system is not fixed (that is, the customer dynamically creates and destroys them ),

Use the prototype registry. Customers can store and retrieve prototypes through the registry.

2> perform the clone operation. Perform deep copy on some complex objects.

3> initialize the cloned object. If the prototype class has already defined the operation for setting the key state, the initialize operation is not required,

Otherwise, you must provide an initialize operation to initialize the parameter.

9: Sample Code :*/

 

// Prototype Factory

Class MazePrototypeFactory: public MazeFactory

{

Public:

MazePrototypeFactory (Maze *, Wall *, Room *, Door *);

 

Virtual Maze * MakeMaze () const;

Virtual Room * MakeRoom (int) const;

Virtual Wall * MakeWall () const;

Virtual Door * MakeDoor (Room *, Room *) const;

 

Private:

Maze * _ prototypeMaze;

Room * _ prototypeRoom;

Wall * _ prototypeWall;

Door * _ prototypeDoor;

};

 

// Initialize each prototype

MazePrototypeFactory: MazePrototypeFactory

(Maze * m, Wall * w, Room * r, Door * d)

{

_ PrototypeMaze = m;

_ PrototypeRoom = r;

_ PrototypeWall = w;

_ PrototypeDoor = d;

}

 

// Implement the virtual method of the base class and internally call the Clone implementation

Wall * MazePrototypeFactory: MakeWall () const

{

Return _ prototypeWall-> Clone ();

}

 

// Initialize the cloned object after utility Clone.

Door * MazePrototypeFactory: MakeDoor (Room * r1, Room * r2) const

{

Door * door = _ prototypeDoor-> Clone ();

Door-> Initialize (r1, r2 );

Return door;

}

// Usage

MakeGame game;

MazePrototypeFactory simpleMazeFactor (new Maze, new Wall, new Room, new Door );

Maze * maze = game. CreateMaze (simpleMazeFactor );

/////////

 

// Replace the following components to implement the new "prototype class". To act as a component, the clone operation must be implemented.

MazePrototypeFactory bombedMazeFactory (

New Maze, new BombedWall, new RoomWithABomb, new Door );

 

// There are two data values in the Door that can be set or need to be set. Therefore, an initilize function must be provided.

Class Door: public MapSite

{

Public:

Door ();

Door (const Door &);

 

Virtual void Initialize (Room *, Room *);

Virtual Door * Clone () const;

Virtual void Enter ();

Room * OtherSideFrom (Room *);

 

Private:

Room * _ room1;

Room * _ room2;

};

 

Door: Door (const Door & other)

{

_ Room1 = other. _ room1;

_ Room2 = other. _ room2;

}

 

Void Door: Initialize (Room * r1, Room * r2)

{

_ Room1 = r1;

_ Room2 = r2;

}

 

Door * Door: Clone () const

{

Return new Door (* this );

}

 

// There should also be an initialize function. Used to set whether a bomb exists

Class BombedWall: public wall

{

Public:

BombedWall ();

BombedWall (const BombedWall &);

 

Virtual Wall * Clone () const;

Bool HasBomb ();

 

Private:

Bool _ bomb;

};

 

BombedWall: BombedWall (const BombedWall & other): Wall (other)

{

_ Bomb = other. _ bomb;

}

 

Wall * BombedWall: Clone () const

{

Return new BombedWall (* this );

}

 

// In this way, you must re-use the component to create a "class" every time you use it ".

// You can use a map to store the class.

Map MazeTable;

MazeTable. insert (make_pair ("bombedMazeFactory", bombedMazeFactory ));

// Retrieve it when you want to use it:

Game. CreateMaze (map ["bombedMazeFactory"]);

// You can encapsulate the direct map into the game, and then pass in a string.

// However, you need to provide a registration function and a destroy function to register and destroy the class.

 

// The following is the parsing version of the ios design mode:

/*

1. The prototype mode is considered in the following scenarios:

1> the object to be created should be independent of its type and creation method. (Too abstract <Unknown mark>)

2> the class to be instantiated is determined at runtime.

3> do not select the factory level that corresponds to the product level.

4> the differences between instances of different classes are only several combinations of States. Therefore, prototype of the number of replication responses is more convenient than manual instantiation.

5> classes are not easy to create. For example, each component can use other components as a combination object of child nodes. Copy existing combinations

Object, and it is easier to modify the copy.

2: consider scenarios in prototype mode:

1> there are many related classes with slightly different behaviors, and the main difference lies in internal attributes, such as names and images;

2> Xu Ao uses a composite object as the basis for other things. For example, he uses a composite object as a component to build another composite object.

*/

 

 

 

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.