Creation Pattern Thematic Summary (creational pattern)
——. NET Design Pattern series Seven
TERRYLEE,2006 year January
Overview
Creating patterns, which are patterns used to create objects, abstract the process of instantiation. It helps a system to be independent of those objects that create, group, and represent it. In this paper, we compare five commonly used creation patterns, and how to use the creation pattern in the example of a game development scenario.
Why do I need to create a pattern
All creation patterns have two timeless themes: first, they encapsulate what specific classes of information the system uses, and second, they hide how instances of these classes are created and organized. The outside world knows only their common interfaces for these objects, but not the specifics of their implementation. Because of this, the creation of what is created (what), by whom (WHO), and when (when) to create these aspects provide the greatest flexibility for software designers.
Suppose that in a game development scenario, a modern-style house object is used, according to our general idea, since the object is needed to create one:
Modernroom = new Modernroom ();
Well, now the object of the modern style of the house has been, if the style of the house has changed, the need is a classic-style house, modified:
Classicalroom = new Classicalroom ();
Imagine how many places in our program use this creation logic, and here is just a change in the style of the house, and you need to modify all of the statements in the program. Now we encapsulate the logic of object creation and place the object creation in a factory method:
Modernfactory factory = new Modernfactory ();
Modernroom = Factory. Create ();
When the style of the house changes, only need to modify
Classicalfactory factory = new Classicalfactory ();
Classicalroom = Factory. Create ();
And the rest of the room is still the same. That's why you need to create a pattern. The Creator mode function can be summarized as follows two points:
1. Encapsulating the creation logic is by no means simply a new object.
2. Package creation logic changes, customer code as far as possible without modification, or as little as possible.
Five common types of creation patterns
The single-piece mode (Singleton pattern) solves the problem of the number of entity objects, and the other is to solve the coupling problem caused by new.
Factory method Mode (Factory pattern) in the factory method, the factory class becomes an abstract class, and its actual creation will be done by its specific subclasses. The intent of the factory method is to define a factory interface that creates product objects, deferring the actual creation to subclasses, emphasizing the "single object" change.
Abstract Factory mode Abstract Factory is the most abstract and general form of all factory models. Abstract factories can provide customers with an interface that enables customers to create product objects in multiple product families without having to specify a specific type of product, emphasizing changes in "series objects".
The generator pattern (builder pattern) moves the logic of the constructed object instance to the outside of the class, defining the class's construction logic outside the class. He separates the construction process of a complex object from the representation of an object. The direct effect is to simplify a complex object into a relatively simple target object. He emphasized the process of product construction.
Prototype mode (Prototype pattern), like Factory mode, also hides object creation from the customer, but unlike constructing a new object by instantiating a class, the prototype pattern is to create a new object by copying an existing object.
How to choose to use Create mode
Continue to consider the game development scenario mentioned above, assuming that in this game scenario we are using several parts of the wall (Wall), Room, Door (Door). In this process, the creation of objects is also a problem, but different creation patterns are used depending on the problem to be solved.
If in the game a room only allows a door to exist, then this is an example of using the Signleton pattern, ensuring that only one instance of the door class is created. Resolves an issue with the number of object creation.
Example code:
Using System;
public sealed class Sigletondoor
{
static readonly Sigletondoor instance=new Sigletondoor ();
Static Sigletondoor ()
{
}
public static Sigletondoor Instance
{
Get
{
return instance;
}
}
}
In a game where you need to create a wall, an instance of a room, to avoid instantiating a class directly to a constructor call, this is the factory method pattern, and each part has its own factory class. The problem with the requirement change of "single object" is solved.
Example code:
Using System;
Public abstract class Wall
{
public abstract void Display ();
}
public class Modernwall:wall
{
public override void Display ()
{
Console.WriteLine ("Modernwall builded");
}
}
Public abstract class Wallfactory
{
Public abstract Wall Create ();
}
public class Modernfactory:wallfactory
{
public override Wall Create ()
{
return new Modernwall ();
}
}
In the game scene, it is impossible to have only one kind of wall or the room, may have the modern style (modern), the classical style (classical) and so on many series style parts. This is the creation of a series of objects, an example of an abstract factory. The problem of changing the demand of "series objects" is solved.
Example code:
Using System;
Public abstract class Wall
{
public abstract void Display ();
}
public class Modernwall:wall
{
public override void Display ()
{
Console.WriteLine ("Modernwall builded");
}
}
public class Classicalwall:wall
{
public override void Display ()
{
Console.WriteLine ("Classicalwall builded");
}
}
Public abstract class
{
public abstract void Display ();
}
public class Modernroom:room
{
public override void Display ()
{
Console.WriteLine ("Modernroom builded");
}
}
public class Classicalroom:room
{
public override void Display ()
{
Console.WriteLine ("Classicalroom builded");
}
}
Public abstract class Abstractfactory
{
Public abstract Wall Createwall ();
Public abstract guest createroom ();
}
public class Modernfactory:abstractfactory
{
public override Wall Createwall ()
{
return new Modernwall ();
}
public override guest Createroom ()
{
return new Modernroom ();
}
}
public class Classicalfactory:abstractfactory
{
public override Wall Createwall ()
{
return new Classicalwall ();
}
public override guest Createroom ()
{
return new Classicalroom ();
}
}
In a game scenario, the algorithm that makes up a scene is stable, for example: The scene is made up of four walls, a house, a door, but the exact style of the wall, the room and the door is constantly changing, which is an example of a generator pattern. The problem of changing the requirements of the "object part" is addressed.
Example code:
Using System;
Using System.Collections;
public class Director
{
public void Construct (builder builder)
{
Builder. Buildwall ();
Builder. Buildroom ();
Builder. Builddoor ();
}
}
Public abstract class Builder
{
public abstract void Buildwall ();
public abstract void Buildroom ();
public abstract void Builddoor ();
Public abstract Gamescene GetResult ();
}
public class Gamebuilder:builder
{
Private Gamescene G;
public override void Buildwall ()
{
g = new Gamescene ();
G.add ("Wall");
}
public override void Buildroom ()
{
G.add ("the");
}
public override void Builddoor ()
{
G.add ("Door");
}
public override Gamescene GetResult ()
{
return g;
}
}
public class Gamescene
{
ArrayList parts = new ArrayList ();
public void Add (string part)
{
Parts. ADD (part);
}
public void Display ()
{
Console.WriteLine ("Gamescene Parts:");
foreach (string part in parts)
Console.WriteLine (part);
}
}
If you need a lot of classical or modern-style walls or houses in the game, you can create new objects by copying an existing prototype object, which is an example of a prototype pattern. Solve the problem of creating "volatile objects" by cloning.
Example code:
Using System;
Public abstract class Roomprototype
{
Public abstract Roomprototype Clone ();
}
public class Modernprototype:roomprototype
{
public override Roomprototype Clone ()
{
Return (Roomprototype) this. MemberwiseClone ();
}
}
public class Classicalprototype:roomprototype
{
public override Roomprototype Clone ()
{
Return (Roomprototype) this. MemberwiseClone ();
}
}
The choice of which model is best depends on many factors. Designs that use the abstract Factory, Prototype pattern, or builder pattern are more flexible than those used with Factory method, but are also more complex, especially abstract Factory need a huge factory class to support. Typically, the design begins with the factory method, and when the designer discovers that more flexibility is needed, the design evolves to other design patterns, and when you weigh the tradeoffs between multiple design patterns, understanding multiple design patterns can give you more choice.
Summarize
The creator model is used to improve the maintainability and scalability of the system and to improve the ability to respond to changes in demand! Reference: "Design mode Chinese Version" "designpatternsexplained" idior "Do you know the Creator mode?" ---Creator mode in detail "MSDN webcast:http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/msdnwebcast.aspx
NET design pattern the second part of the creation pattern (6): Creation pattern Thematic summary (creational pattern)