Good book finishing series - design mode: can be used for object-oriented software base 3

zhaozj2021-02-08  221

Chapter 3 Creating a Model Create a Model Mode Abstract Mechanization Process. They help a system independently of how to create, combine, and represent it. A class creation mode uses inheritance to change the instantiated class, and an object creation mode will instantiate to another object. As the system evolves more and more dependent on object composite rather than type inheritance, the creation mode is more important. When this happens, the center of gravity is transferred from the hardcodes of a set of fixed behaviors (H A R D - C O D I N g) to define a smaller basic behavior set, which can be combined to become more complex behavior of any number. This creation of object requirements for specific behavior is not only instantiated. There are two constant main melody in these modes. First, they all encapsulated which specific classes of the system use. Second, they hide how these classes are created and put together. The entire system knows the interface defined by the abstract class on these objects. Therefore, the creation mode is created, who creates it, how is it created, and when you create these aspects to give you a lot of flexibility. They allow you to configure a system with a "product" object that is very different from structural and functionality. The configuration can be static (i.e., specified at compile) or dynamic (at runtime). Sometimes the creation mode is competing in each other. For example, it is good to use P R O T O T Y P E (3. 4) or Abstract Factory (3. 1) in some cases. In other cases they are complementary: B U I L D E R (3. 2) can use other modes to implement the creation of a component. P R O T O T Y P E (3. 4) can be used in its implementation of S i N G L E T O N (3. 5). Because the creation mode is closely correlated, we will study all 5 modes to highlight their similarities and differences. We will also give a general example - Creating a labyrinth for a computer game - to clear their implementation. This maze and game will be slightly different from the various modes. Sometimes this game will just find an exit of a maze; in this case, the player may only see the local labyrinth. Sometimes maze includes some problems to be solved and the danger to overcome, and these games may provide some maze maps that have been explored. We will ignore the details in many maze and one of a maze game or multiple players. We only pay attention to how the maze is created. We define a maze as a series of rooms, a room knows its neighbors; possible neighbors either another room, or a wall, or a door to another room. Class R O O M, D O O R and Wa L are defined all parts used in all of our examples. We only define some parts of these classes to create an important role in creating a maze. We will ignore players, display operations and move around the labyrinth, as well as other important functions that are not related to creating maze. The next page shows the relationship between these classes. Each room has four sides. We use the enumerated type D i r e c t i o n in C to designate the northwest of the room: Enum direction {north, the implementation of the implementation of East, West}; S M A L t a lk is used to represent these directions. Class M a P S I T E is a public abstract class of all labyrinth components. For simplified examples, M a P S i TE is defined only an operation E N T E R, and its meaning is determined to enter what you enter. If you enter a room, then your location will change. If you try to enter a door, then there will happen in these two things: if the door is open, you entered another room. If the door is critical, then you will hit the wall. E N T E R provides a simple basis for more complex game operations.

For example, if you say "east" in a room, the game can only determine which M a P S i t e directly on the east and calls E n t e r. The E n t eR operation of a particular subclass will calculate your location changes, or you will hit the wall. In a real game, e n t e r can use the moving player object as a parameter. R O O M is a specific subclass of M A P S I T E, and M A P S I T E defines the main relationship between the components in the labyrinth. R O O M has a reference to other M a P S i t E objects and saves a room number, which is used to identify the room in the maze. The following class describes the walls or doors that appear in each of the rooms. Chapter 3 Creating Models 5 5 We not only need to know the part of the maze, but also define a M a z e class to represent the collection of rooms. A specific room can be found with R O O M N o operation and a given room number. R O O M N o can use line-shaped search, H a s h table, and even a simple array for a lookup. But we don't consider these details here, but focus on how to specify a maze object. Another class we define is M A z E G A M E, which is created by it. A simple and direct creating maze is to use a series of operations to add components into the maze and then connect them. For example, the following member function will create a maze, this maze considers a door between two rooms and them: considering that this function is only created a maze with two rooms, it is quite complex. . Obviously there is a way to make it simpler. For example, R O O M constructors can initialize each of the rooms in advance. But this is just to move the code elsewhere. The true problem of this member function is not that it is not flexible. It hardcodes the layout of the maze. Changing the layout means changing this member function, or redefining it - this means re-implementing the entire process - or changing its part - this is prone to errors and is not reused. The creation mode shows how this design is more flexible, but it may not be smaller. In particular, they will make it easy to modify the 5 6 design mode: the basis for object-oriented software

A class of a maze member. Suppose you want to reuse an existing maze layout in a new game of a magic maze that contains (all things). Apply a magic maze game with new components, like D Oor N Eeding S Pell, it is a door that is only with a spell that can be locked and opened; and e nchanded R Oom, one can have unusual things Room, such as magic keys or spells. How can you easily change C R e a t e m a z e to create a maze with these new types of objects? In this case, the biggest obstacle to change is hard coding for the instantiated class. The creation mode provides a variety of different ways to remove explicit references to these specific classes from instantiating their code: • If C Reate M Aze calls virtual functions instead of constructors to create the room, walls, and doors, Then you can create a subclass of M Aze G AME and redefine these virtual functions, thereby changing the alleged classes. This method is an example of the Factory Method (3. 3) mode. • If you pass an object to C R E A T E M A Z E Parameters to create a room, walls, and doors, you can pass different parameters to change the room, walls and gates. This is an example of the Abstract Factory (3. 1) mode. • If you pass an object to C Reate M Aze, this object can use to increase the room, wall and door in its labyrinth, to fully create a new maze, then you can use inheritance to change some parts of the maze Or the way the maze is built. This is an example of B U I L D E R (3. 2) mode. • If C R e a t e m a z e is parameterized by a variety of prototypes, walls and door objects, it is copied and increasing these objects into the maze, then you can replace these prototype objects with different objects to change the composition of the maze. This is an example of the P R O T O T Y P E (3. 4) mode. The remaining creative mode, S i N g L e t o n (3. 5), you can ensure that there is only one maze in each game and all game objects can quickly access it - no need to help global variables or functions. S i n g l e t o n also makes the maze easy to expand or replace, and does not need to change existing code. 3.1 Abstract Factory - Object creation mode 1. It is intended to provide an interface to create a series of related or interdependent objects without specifying their specific classes. 2. Alias ​​K i T3. Motivation Considering a user interface toolkit supporting a variety of vision (L O o K - a N D - F e E L) standard, such as M O T I f and Presentation Manager. Different vision stits are different looks and behaviors for user interfaces such as scroll bars, windows, and buttons. In order to ensure portability between the visual style standards, an application should not be hardly coded for a particular video appearance. Window assemblies that instantidate specific visual styles throughout the app will make it difficult to change the sensation style. To solve this problem, we can define an abstract Wi D g E TEF A C T O R Y class that declares a interface to create each class of basic window components. Each type of window component has an abstraction class, and the specific subclass implements a specific visual style of window components. For each abstract window component class, Wi D g e t f A c t o R Y interface has an operation that returns a new window component object. Customers call these operations to get the window component instance, but the customer does not know what specific classes they are using. This customer does not rely on a general video style, as shown in the following page. Chapter 3 Creating Models 5 7

Each of the visual criteria corresponds to a specific Wi D g e t f A c t O R y subclass. Each subclass implements the operations of the window components used to create a suitable visual style. For example, the C R E A T E S C R O L B A R of M O T O R Y is operated and returned to a M O T I f scroll bar, and the corresponding P M Wi D g e t f-t-t O R Y operation returns a scroll bar of a Presentation Manager. Customers create window components only via Wi D g e t f A C T O R Y interface, they don't know which classes have implemented a specific visual style window component. In other words, the client interacts with an interface defined by an abstract class without using a particular specific class interface. Wi D g e t f A c t o R y also enhances the dependencies between the specific window component classes. The scroll bar of MO T I f should be used with the M O T I f button, the M O T I F body editor, which is automatically coupled to the result of using M O T I f Wi D g e t f A c t o R y. 4. Applicability You can use the Abstract Factory mode in the following situations • A system to be independent of its product creation, combination, and representation. • A system is configured by one of multiple product lines. • When you want to emphasize a series of related product objects to be used in combination. • When you provide a product class library, just want to display their interfaces instead of implementation. 5. Structure This mode is shown below. 6. Participants • A B S T R A C T f A C T O R Y (Wi D G E T f A C T O R Y) 5 8 Design mode: can be used for object-oriented software

- Declare an operation interface that creates an abstract product object. • C O N C R E (M O T O R Y, P M Wi D G E T f A C T O R Y) - Implementation of Creating A specific product object. • A B S T R A C T P R O D U C T (Wi N D O W S, S C R O L1 R) - An interface is declared for a class of product objects. • C O N C R E T E P R O D U C T (M O T I f Wi N D O W, M O T I f S C R O L B A R) - Defines a product object that will be created by the corresponding factory. Implementation A b S T R A C T P R O D U C T Interface. • C L i e n T - only uses an interface declared by the A b S T R a C TEF A C T O R Y and A B S T R A C T P RO D U. 7. Collaboration • An instance of a C O N C R e t E f A C T R O Y class is usually created at runtime. This specific factory creates a product object with a specific implementation. To create different product objects, customers should use different specific factories. • AbstractFactory delays the creation of the product object to its C O N C R e T E F A C T O R Y subclass. 8. Effects A B S T R A C T F A C T O R Y mode There are some advantages and disadvantages of the following: 1) It separates the specific class Abstract Factory mode to help you control the class of an object created. Because a factory package creates the responsibility and process of the product object, it separates the customer with the class. The customer manipulates an example through their abstract interface. The class name of the product is also separated in the implementation of the specific factory; they do not appear in the customer code. 2) It makes it easy to switch the product series A specific factory class only once in an application - ie when it is initialized. This makes it easy to change the specific factories of an application. It only needs to change the specific factories to use different product configurations because an abstract factory creates a complete product line, so the entire product line will change immediately. In the example of our user interface, we only need to convert to the corresponding plant object and recreate the interface, you can convert from the M O T I F window component to the Presentation Manager window component. 3) It is conducive to the consistency of the product When a series of product objects are designed to work together, it is important to use the objects that can only use the same series at a time. This is easy to achieve this. 4) It is difficult to support new types of products that are difficult to expand abstract factories to produce new types of products. This is because the A b S T R A C T F A C T O R Y interface determines a collection of products that can be created. Products that support new types need to expand the plant interface, which will involve the change in A B S T R A C T F A C T O R Y class and all its subclasses. We will discuss a solution to this issue in implementing a section. 9. Implementation below is some useful techniques to implement Abstract Factor mode: 1) Examples of using the plant as a single piece of application usually only one C O N C R e t e f A c t o R y. Therefore, the factory is usually best implemented as a S i N G L E T O N (3. 5). 2) Creating Products A B S T R A C T f A c t o R y only declares an interface to create a product, and truly create a product is implemented by C O N C R e t e P R O D U. The most common method is to define a factory method for each product (see Factory Method (3. 3)). A specific factory will redefine the plant method for each product to specify the product.

Although such implementation is simple, it requires a new specific factory subclass, even if these product lines are different. Chapter 3 Creating Models 5 9

If there are multiple possible product lines, specific factories can also be implemented using P R O T O T Y P E (3. 4) mode. The specific factory uses the prototype example of each product in the product line to initialize, and it creates a new product by copying its prototype. In a prototype-based approach, it is made to make a new specific factory class for each new product line. Here is a method of achieving a prototype based factory in S M A L t a lk. Specific factories store prototypes that will be copied in a dictionary called P A R T a t a l o g. Method M A K E: Retrieve the prototype and copy it: make: partname ^ (PartCatalog at: Partname) Copy specific factory has a method to add components to this directory. AddPart: PartTemplate Named: Partname Put: PartTemplate Prototype by identifying them with a symbol, thereby being added to the factory: Afactory AddPart: Aprototype Named: #acMewidget In the language of the first class object (eg S Malltalk And o bjective c), this prototype-based approach may vary. You can view a class in these languages ​​as a degraded factory, which only creates a product. You can store classes in a specific factory, and this specific factory creates multiple specific products in variables, which is very like prototypes. These classes have created new instances in place of the specific factory. You can define a new factory by using the product's instance of the product rather than subclasses. This method uses the characteristics of the language, and the principal-based method is independent of the language. Like the prototype-based factory based on S M A L L, the S M A L L, which is just discussed. The class-based version will have a unique instance variable P A R T c a t a l o g, which is a dictionary, and its primary key is the name of each part. P A R T C a T A L O G Stores the product of the product rather than storing the replicated prototype. Method M A K E: This is now: make: Partname ^ (Partcatalog at: Partname) New3) Defines Scalable Factory A B S T R A C T F A C T O R Y usually defines a product that it can produce. The type of product is encoded in the operating profile. Add a new product requirement to change the interface of A B S T R A C T F A C T O R Y and all classes related to it. A more flexible but less secure design adds a parameter to the operation of the creation object. This parameter specifies the type of the object that will be created. It can be a class identifier, an integer, a string, or anything else to identify this product. In fact, this method is used, and the A b S T R A C T f A C T O R Y only needs a "M a K E" operation and a parameter indicating the type of object to create. This is a technique based on prototype and class-based abstract factory that has been discussed earlier. This change is more likely to be used in dynamic type languages ​​similar to S M A L T A L K compared to C . You can use it in C when all objects have the same abstract base class, or when the product object can be requested to be able to force the customer security for customers to be properly type it. The implementation of the Factory Method (3.3) describes how to implement such parameterization operations in C . This method does not require type forced conversion, but there is still an essential problem: all products return the same abstract interface given by the return to the customer. Customers will not be able to distinguish or securely assumed the category of a product. If a customer needs to perform an operation related to a specific subclass, these operations cannot be obtained by an abstract interface.

Although customers can implement a down type conversion (D O W N C A S) (eg, D Y N M I C_ C A S T in C ), this is not always possible, because the down type conversion may fail. This is a typical highly flexible and extensible interface weighing 6 0 Design mode: can be used for object-oriented software. 10. Code Sample We will use the Abstract Factory mode to create the maze we discussed here in this chapter. Class M a Z e f A c t o R Y can create a maze component. It built the door between the room, the wall and the room. It can be used in a program that reads the maze instructions from the file and constructs a program of the corresponding labyrinth. Or it can be used for a randomly built labyrinth program. The program of the labyrinth will use M A Z E F A C T O R Y as a parameter so that the programmer can specify the rooms, walls, and doors, etc. to be created. Recall the establishment of a member of the small maze consisting of two rooms and the door of the door, C R e a t e m a z e. C R e a t e m a z e hardcodes the class name, which makes it difficult to create a maze with different components. Here is a new version of Createmaze with M A z E f A c t o R, which modifies the above disadvantages: We create a subclass of M a z e f A c t o R y, E N c H A N t e d m a z e f A c t o R, which is a factory that creates a labyrinth of the magic. E n c h a n t e d M A z e f A c t o R Y will redefine different member functions and return different subclasses such as R O O O M, Wa L L. Chapter 3 Creating Models 6 1

Now suppose we want to generate a maze game, in this game, there can be a bomb in each room. If this bomb explodes, it will (at least) destroy the wall. We can generate a subclass of R o O M to clear whether there is a bomb in the room and whether the bomb exploded. We will also need a subclass of Wa L L to clearly damage to the wall. We will call these classes of R o O M Wi T H A B O M B and B O M B E D Wa L. We will define the last class of B O m b E D M A z E F A C T O R Y, which is a subclass of M A z e f A C T O R Y, ensuring that the wall is B O m b E d Wa L-class and the room is R O O M Wi T H A B O m B. B o m b e D m a z e f A c t o R y requires only two functions: to build a simple labyrinth containing a bomb, we only call C R E A T E M A Z E with B O M B E D M A Z E F A C T O R Y. MazeGame Game; BombedmazeFactory Factory; g A m e. C R E A T E M A Z E (F A C T O R Y); C R E A T E M A Z E can also receive an E N C H A N t e d m a z e f a c t o R Y instance to construct a maze of the magic. Note that M A z e f A c t o R y is only a collection of factory methods. This is the most common way to implement the Abstract Factory mode. At the same time, pay attention to M A z e f A c t o R y is not an abstract class; therefore it is used as both A b S T R a C T f A C T O R Y, also as C O N C R e t E f A C T O R Y. This is another usual implementation of the simple application of the Abstract Factory mode. Because M A Z e f A c t O R Y is a specific class that is completely composed of factory methods, it is easy to generate a new M A z E f A c t o R y by generating a subclass and redefining an operation that needs to be changed. C R e a t e m a z e uses the S e t S I d e operation of the room to specify their respective faces. If it creates a room with a B O m b E d M A z e f a c t o R y, the maze will consist of the R O O M Wi T H A B O M B surface of the B O m b E d Wa L. If R O O M Wi T H A B O M B must access a member of the BO M b E D Wa L L, it will have to reference its wall reference to conversion from Wa L * to B O m b E d Wa L *. As long as this parameter is indeed a B O m b e d Wa L, this down type conversion is secure, and if the wall is created only by one B O M B E D M A z E F A C T O R Y, it can guarantee this. Of course, the dynamic type language such as S M a L L is no need to convert down type, but if they encounter a WA L-class in the subclass of Wa L, it may generate a runtime error. Use the Abstract Factory to build walls, which can help prevent these runtime errors by determining that only a particular type of walls can be created. Let us consider a S m a l t a l k version of M A z E F A C T O R Y, which only has a M a K e operation with the object species to generate as a parameter. In addition, the specific plant stores the class of products it created. First, we use S M a L L to write an equivalent C R E A T E M A Z E: 6 2 Design mode: can be used for object-oriented software

As we discussed in implementing section, M A Z E F A C T O R Y requires only one instance variable P A R t c a t a l o g to provide a dictionary, the primary key of this dictionary is the class of labyrinth components. Also think about how we implements m a k E: method: Now we can create a M A z E F A C T O R y and use it to implement C R E A T E M A z E. We will create the factory with a method C R e a t e m a z e f a c t o R y using class M a z E g A m. By associating different classes with their primary keys, a B O M b E D M A Z E F A C T O R Y or E N C H A N T E DM A Z E F A C T O R Y is created. For example, an E n c H a N t e d m a z e f a c t O R Y can be created: 11. It is known to apply I n t e R VI E W using "K i T" suffix [L i n 9 2] to represent the A b S T R A C T f A C T O R Y class. It defines Wi D g e t k i t and d i a l o g k i t abstract factories to generate user interface objects associated with a particular visual style. I N t e R VI E W also includes a L A Y O U T K i T that generates different composition (C O M P O S I Ti O N) objects based on the required layout. For example, a conceptually horizontal layout may require different constituent objects according to the location of the document (portrait or landscape). E T [W G M 8 8] uses the Abstract Factory mode to achieve portability between different window systems (eg, X Wi N D O W S and S U N Vi E W). Wi N D O W S Y S TET EM Abstract Bastrics Define some interfaces to create objects indicating the window system resource (eg M A K E Wi N D O W, M A K E f O N, M A K E C O L O R). The specific subclasses implements these interfaces for a particular window system. Running moments, e t create an example of a specific Wi N D O W S Y S TEM sub-class to create a specific system resource object. 12. Related Mode A B S T R A C T F A C T O R Y Class is usually implemented with a factory method (Factory Method (3)), but they can also be implemented with P R O T O T Y P. A specific factory is usually a single piece (S i N g l e t o n (3. 5)). 3.2 Builder - Object Create mode 1. It is intended to separate a complex object to its representation, so that the same build process can create different representations. Chapter 3 Creating Models 6 3

2. Motivation One RT FUT Format document exchange format reader should convert RT F to a variety of body formats. This reader can convert the RT F document into normal A S c i i text or convert into a text window component that can edit in an interactive manner. But the problem is that the number of possible conversion is unlimited. Therefore, it is easy to achieve an increase in new conversions, but does not change the RT F reader. One solution is to configure this RT F R E A D e R class with a TE x T C O N V e R t eR object that can convert RT F into another body representation. When RT F R e A D e r performs syntax analysis of the RT F document, it uses TE X T C O N V E R T ER to do conversion. Whenever RT F R e A D e R identifies an RT F mark (or a normal body or an RT F control word), it sends a request to convert this tag to TE X T C O N V E R T E R. TE X T C O N V E R T E R Objects are responsible for data conversion and representing this tag in a particular format, as shown below. The subclasses of TE X T C O N V E R T perform special processes for different conversions and different formats. For example, an A S c i c o N V e R t eR is only responsible for converting normal text and ignoring other conversion requests. On the other hand, a TE X C O N V E R T ER will operate to all requests to generate a T e x representation of all style information in the body. A TE X T Wi D g e t C O N V E R T ER will generate a complex user interface object so that users browse and edit the body. Each converter class will create and assemble a mechanism for complex objects hidden behind the abstract interface. The converter is independent of the reader, and the reader is responsible for grammar analysis of an RT F document. B U I L D e r mode describes all of these relationships. Each converter class is referred to in this mode as a generator (B U I L D e R), and the reader is called the guide (D i r e c t O r). In the above example, the B U I L D e R mode will analyze the algorithm of the analysis text format (ie the r r 文 语 分析 分析 分析 程序 program) and the description of how to create and indicate a conversion format. This allows us to re-use the syntax analysis algorithm of RT F R e A D e R, create different body representations based on the RT F document - only the subcategories of different TE X T C O N V E R T E R can be configured to configure the RT F R e a d e r. 3. Applicability Use B U I L D E R Mode in the following cases • When the algorithm for creating complex objects should be independent of the component of the object and their assembly mode. • When the constructor must allow the object being constructed to have different representations. 4. Structure This mode structure is shown in the following page. 6 4 Design mode: can be used for object-oriented software

5. Participants • B U I L D E R (TE X T C O N V E R T E R) - Specifies an abstraction interface to create a P RO D u c T object. • C O N C R E T E B U I L D E R (A SE X C O N V E R T E R, TE X C O N V E R T E R, TE x C O N V E R T E R, TE X C O N V E R T E R, TE X C O N V E R T E R, TE X C O N V E R T E R, TE X C O N V E R T E R, TE X C O N V E R T E R, TE X C O N V E R T E R, TE X C O N V E R T E R, TE X T Wi D g E R T E R, TE X T Wi D g E R T E R, TE X C O N V E R T E R, TE X C O N V E R T E R, TE x C O N V E R T E R, TE X T Wi D G E R T E R, TE X T Wi D g E R T E R, TE x C O N V E R T E R, TE x C O N V E R T E R, TE X T Wi D G E R T E R) - A interface to construct and assemble the product. - Definition and explicit it created. - Provides an interface for retrieving products (for example, G E T A S C I I TE X T, G E T TE X T Wi D G E T). • Director (RT F R E A D R) - Constructs an object using B U I L D e R interface. • P R O D U C T (A S C I I TE X T, TE X TE X T, TE X T Wi D G E T) - Indicates complex objects that are constructed. C O N C R e t e b U i l D e r Create the internal representation of the product and defines its assembly process. - Contains classes that define components, including assembling these components into the interface of the final product. 6. Collaboration • Customer Create a D i R E C T O R object and configure it with the B U I L D e R it wants. • Once the product component is generated, the guide will notify the generator. • The generator processes the request for the guides and adds the part to the product. • Customers retrieve products from the builder. The following interaction diagram illustrates how B U I L D E R and D i R E C T O R are collaborated with one customer. 7. Effects herein is the main effect of B U I L D e R mode: Chapter 3 Creating a model 6 5

1) It allows you to change the internal representation of the B U I L D e r to provide an abstract interface for a constructor for the guide. This interface allows the generator to hide the representation and internal structure of this product. It also hides how the product is assembled. Because the product is constructed by an abstract interface, you are only defining a new generator when changing the internal representation of the product. 2) It enhances the modularity of the object by encapsulating the creation and representation of a complex object by encapsulating the creation and representation of a complex object. Customers don't need to know all the information of classes that define the internal structure of the product; these classes are not in the B U I L D e R interface. Each C O N C R e t e b U I L D e r contains all code created and assembled. These codes only need to be written; then different D i R E C T O R can be multiplexed to construct a different P R O D U C T on the basis of the same part set. In the previous RT F example, we can define a reader for the format other than the RT F format, such as an SGMLR EAder, and use the same TE XT C Onverter to generate an SGML document ASCII TE XT, TE X TE XT and TE XT Wi dget translation. 3) It allows you to make more refined control of the construction process B U I L D E R mode to generate the creation mode of the product, which is the next step by step by step by step. Retrieve it from the generator when the product is completed. Therefore, B U I L D e R interface can better reflect the construction process of the product than other creation modes. This allows you to control the build process more refined, so that you can control the internal structure of the resulting product. 8. Implementation usually there is an abstract B U I L D e R class as a guide that may require each component that is created to define an operation. These operations are not done by default. A C O N C R e t e b U i l D e 类 The component that is interested in redifuncing these operations. Here is some other issues to consider: 1) Assembly and constructive interface builders are gradually constructing their products. Therefore, B U I L D e R class interface must be sufficiently common to construct a product for a variety of specific generators. A key design problem is the model of constructing and assembly processes. The result of the constructor is only added to the product, usually such a model is sufficient. In the example of RT F, the generator converts the next tag and adds it to the body it has converted. But sometimes you may need to access the product components that have been constructed in front. In the M A z E example given in the code example section, the M a Z e b U I L D e-interface allows you to add a door between the already existing rooms. The tree structure constructed on the bottom of the tree so that the tree so that the tree is designed is another example. In this case, the generator returns the subjunction to the guide, then the guide will pass them back to the generator to create a parent node. 2) Why do the products do not have an abstract class usually, a product generated by a specific generator, and their representation is that the difference is that there is not much meant to give different products in public parent categories. In the RT F example, the A S C I I TE X T and TE X T Wi D g e T objects are unlikely to have a common interface, and they do not need such an interface. Because customers typically configure the guidelines with a suitable specific generator, the location of the customer knows which specific subclass of B U I L D e R is used and can process its products. 3) In B U I L D e r, the method of the province is empty C , and the method is intentionally not stated as a pure virtual member function, but is defined as an empty method, which enables customers to only define the operations they are interested in. 9. Code Example We will define a variant of a member function of a C R E A T E M A Z e, which is used as a parameter with a generator object of class M a z e b U I L D e R. M a z e b U I L D e 类 Definition The following interface to create a labyrinth: 6 6 Design mode: can be used for object-oriented software

This interface can be created: 1) Maze. 2) There is a room with a specific room number. 3) The door between the room with the number. G e t m a z e operation returns to this labyrinth to customers. The subclasses of M A z e b u i l D e r will redefine these operations and return them to the maze created. M a z e b U I L DER's operation is not done when the operation of the labyrinth is default. Do not define them as a pure virtual function is to facilitate derived classes to only define those methods interested in. With M A z E B U I L D e R interface, we can change the C R E A T E M A Z E member function to generator as its parameters. This C R E a T E M A z E version is compared to the original, however, the generator hides the internal representation of the maze - that is, the classes of the room, doors, and walls - and how these components are forming the final maze. Some people may guess that some classes are used to represent rooms and doors, but there is no indication which class is used to represent walls. This makes it easy to change a labyrinth, because customers with all M A Z E B U I L D e R need to be changed. Like other creation modes, the B u i l D e R mode encapsulates how objects are created, in which case is encapsulated by the interface defined by M A z E B U I L D e R. This means we can rewind M A Z E B U I L D e r to create different kinds of maze. C R E A T E C O M P L E X M A Z E Operations An example: Note M A z e b u i l D e ourselves do not create a maze; its main purpose is to define an interface for creating a maze. It is mainly for convenience to define some empty implementation. The subclass of M A z e b u i l D e r does actually. Subclass S t a n D A R D M A z e b u i l D e r is an implementation of a simple maze. It places it in the maze that it is created in the variable _ C U R RE N T M A Z E. Chapter 3 Creating schema 6 7C o m m o n Wa l l is a functional operation, which determines the orientation of the common wall between two rooms. The constructor of S t a n D A r D m A z e b U I l D e r is only initialized the _ C U R R E N T M A Z E. B U I L D m A z e instantiates a M A z e, which will be assembled by other operations and eventually returned to the customer (via g e t m a z e). B Uild R OM Operation Creates a room and built the wall around it: To build a door between two rooms, S Tandard M Aze B Uilder Find these two rooms in the maze and find their adjacent wall: Customer You can now create a maze with C REATE M AZE and S TANDARD M AZE B Uilder: We can place all S TANDARD M AZE B Uilder operations in M ​​Aze and let each M Aze create itself. However, M a z e becomes small to make it easier to understand and modify, and S T a N D A R D M A z e b U I L D e r is easily separated from M A z E. More importantly, the two are separated such that you can have multiple M A Z E B U I L D e R, each with a class of different rooms, walls, and doors. 6 8 Design mode: can be used for object-oriented software

A more special M A z E B U I L D E R is C O U N Ti N G M A Z E B U I L D E R. This generator does not create a maze at all; it only counts different types of components that have been created. The constructor initializes the counter, while the redemption M a z e b U I L D e R operation is only the corresponding increase count. Below is a customer may use C O U N t I n g m a z e b u i l D E R: 10. It is known to apply the RT F converter application from E T [W G M 8 8]. Its body generation module uses a generator to process the body stored in RT F format. The generator is a universal pattern in S M a L t a l k - 8 0 [p A r 9 0]: • The P A R s E R in the compilation subsystem is a D i R E C T O R, which is used as a parameter as a parameter in a P R O G R A m N o D e b U Il D e R object. Whenever a grammatical structure is identified, it notifies its P R O G R A M N o D e b U I L D e R object. When this syntax analyzer is finished, it requests the generator to generate the syntax analysis tree and returns the syntax to the customer. Chapter 3 Creating Models 6 9

• C L A S S B U I L D E R is a generator, and C L a s s uses it creates a subclass for yourself. In this example, a C L A S S is both D i R E C T O R and P R O D U C T. • B Y T E C O D E S T R E A M is a generator that creates a compiled method as a byte array. B Y T E C O D E S T R E A M is not a standard use of the B u i l D e R mode because its complex object is encoded as an array of byte, not normal S m a L t a l k object. However, the interface of B Y T E C O D e S T R E A M is a typical generator, and will easily use a different class that represents a program as a composite object to replace B Y T E C O D e S T R E A m. The service configurator (S E R V i C EC O N f I g U R A T O R) in Adaptive Communications Environment uses a generator to construct a network service component [S 9 4] that is dynamically connected to the server. These components are described using a configuration language for grammar analysis by the L A L R (1) syntax. The semantic action of this grammar analyzer operates on the generator loaded to the service component. In this example, the syntax analyzer is D i r e c t O r. 11. Related Mode Abstract Factory (3. 1) Similar to B U I L D E R because it can also create complex objects. The main difference is that B U I L D e R mode focuses on a complex object step by step. Abstract Factory focuses on multiple series of product objects (simple or complex). B U I L D e r returns the product in the final step, and for Abstract Factory, the product is immediately returned. C O m P O S I T E (4. 3) is usually generated with B U I L D e r. 3.3 Factory Method (Factory Method) - Object Create mode 1. It is intended to define an interface for creating an object, allowing subclasses to instantize which class. Factory Method delays a class of instantiation to their subclasses. 2. Does the Virtual Constructor 3. The motivation framework uses the relationship between the abstract class definition and maintenance objects. The creation of these objects is usually responsible by the framework. Consider such an application framework, which can display multiple documents to the user. In this frame, two main abstractions are class A p p L i c a Ti N and D O C u m e n t. Both classes are abstract, customers must be achieved with specific applications through their subclasses. For example, in order to create a drawing application, we define the class D R A W I N g A P P L I c a T I O N and D R A W I N G D O C U m E N T. A P P L I C A T I O N class is responsible for managing D O C U m e n t and creates them as needed - for example, when the user selects O P e n or N e w from the menu. Because of the active specific D Ocument subclavab is related to a particular application, the A PPLICATION class is unlikely to predict which D Ocument subclass will be instantiated - A PPLICATION class only knows when a new document should be created, I don't know which D Ocument will be created. This has created an embarrassing situation: the frame must be instantiated, but it only knows the abstract class that cannot be instantiated. Factory Method mode provides a solution.

It encapsulates which D O C U m e n T subclass will be created and separate this information from the frame, as shown in the following page. The subclasses of A P L I C A T I O N Redefine A P P L I C A T I O N Abstract Operation C R E A T E D O C U M E N T Returns the appropriate D O C u m e n T subclass object. Once an A P P L i c A T I O N subclass is instantiated, it can instantiate the application-related document without 7 0 Design mode: You need to know the class of the object-oriented software. We call C R E A T E D O C u m e n t is a factory method (F A C T O R Y M E T H O D), as it is responsible for "production" an object. 4. Applicability Under the following cases you can use the Factory Method mode: • When a class doesn't know the class of the object it must create. • When a class wants to specify the object it created by its subclass. • When classes will create a responsibility of the object to a plurality of help subclauses, and which helper subclass will be the information localization of the agent. 5. Structure 6. Participants • P R O D U C T (D O C U m E N T) - Defines the interface of the object created by the factory method. • C O N C R E T E P R O D U C T (M Y D O C U M E N T) - Implement the P R O D U C T Interface. • C R E A T O R (a P L I C A T I O N) - Declaration Factory Method, which returns a P R O D u C T type object. C R e a t O r can also define a default implementation of a factory method, which returns a default C O N C R E TE E P R O D U C T object. - You can call the factory method to create a P R O D U C T object. • C O N C R E T E C R E A T O R (M Y A P L I C A T I O N) - The factory method is redefined to return a C O N C R e T E P R O D u C T example. 7. Collaboration • Creator depends on its subclass to define a factory method, so it returns an appropriate C O N C R E TE E P R O D U C T Instance. 8. Effect Chapter 3 Creating Models 7 1

Factory methods no longer bind to your code related to specific applications. The code only processes the P R O D U C T interface; therefore it can be used with any C O N C R e t e P R O D U C T class defined. A potentially disadvantage of the factory method is that the customer may only create a subclass of C R E A T O R to create a specific C O N C R e TE E P R O D U C T object. When C R e a t O R subclass is not required, customers now necessarily handle other aspects of the evolution; but when customers must create a subclass of C R E A T O R, creating subclasses is also possible. Below is the other two effects of Factory Method mode: 1) Provide hook (H o o K) factory methods for subclasses in an internal creation of objects that are typically more flexible than direct creating objects. Factory Method gives a hook to the subclass to provide an extension of the object. In the example of D O C u m e n t, the D O C U m e n T class can define a factory method called C R E A T E F I L E D i a L O G, which opens an existing document to create a default file dialog object. The subclasses of D O C U m e n t can redefine this factory approach to define a file dialog associated with a particular application. In this case, the factory method is no longer abstract but provides a reasonable default implementation. 2) Connecting parallel level hierarchical, in the example we consider, factory methods do not only be called by C R e A T O R, customers can find some useful factory methods, especially in parallel levels. When a class entrusts its responsibilities to a separate class, it produces a parallel class level. Considering the graphics that can be interactive; that is, they can extend, move, or rotate with a mouse. Realizing such interactions is not always so easy, it typically requires storage and updating information on a given time to record the manipulation state, this state is only required when manipulating. So it doesn't need to be saved in the graphic object. In addition, different graphics have different behaviors when the user manipulates the pattern. For example, a linear graphic may generate an endpoint that is moved, and the extension of the body may change the distance. With these restrictions, it is best to use a separate M A N I P U L A T O R to interact interact and save any status related to a particular manipulation. Different graphics will use different M a N i P U L A T O R subclasses to handle specific interactions. The obtained M a N I P U L A T O R Class Hierarchy is parallel (at least partially parallel), as shown below. The F I g U r C class provides a C R E A T E M A N I P U L A T O R Factory Method, which allows customers to create a M a N i P U L A T O R corresponding to F I g U R E. F I g U R e subclass redefines the method to return an example of a suitable M a N i P U L A T O R subclass. As a choice, the F i g U R e-class can implement C R E A T E M A N I P U L A T O R to return a default M A N i P U L A T O R instance, and F I g U r e subclass can only inherit this default implementation. Such a F I g U R C class does not require the corresponding M A N I P U L A T O R Subclass - thus only partially parallel. Note how the factory approach defines the connection between two class hierarchies. Which types of information will be locally established together. 7 2 Design mode: can be used for object-oriented software

9. Implementation When using Factory Method mode, consider some of the following questions: 1) There are two different cases of the factory method mode: 1) The first case is that the c reator class is an abstract class. It does not provide the implementation of the factory method it declares. 2) The second case is that C R E A T O R is a specific class and provides a default implementation for the factory method. It is also possible that there is an abstract class defined by default, but this is not common. The first case requires subclasses to define implementations because there is no reasonable default implementation. It avoids problems that have to be instantiate unpredictable. In the second case, the specific C R E A T O R is mainly used by flexibility. The criterion it follow is, "Create an object with a separate operation, so that they can redefine their creation mode." This criterion ensures that the designer of the subclass can change the parent class when necessary. Object class. 2) Another case of parameter chemical plant method This mode allows factory methods to create a variety of products. The factory method uses a parameter that identifies the type of object to be created. All objects created by the factory method will share the P R O D U C T interface. In the example of D O C u m e n t, a P P L I C A T I O N may support different types of D O C u m e n t. You give C R e a t e d o c u m e n t to specify the type of document to be created. Graphics Editing Framework UNIDRAW [VL90] uses this method to reconstruct objects stored on disk. U N i d r a W defines a C R E A T O R class that has a factory method C r e a t e with a class identifier as a parameter. The class identifier specifies the class to be instantiated. When U N i D R A W will write an object store, it first writes the class identifier, then it is its instance variable. When it reconstructs the object from the disk, it first reads the class identifier. Once the class identifier is read, this framework uses the identifier as a parameter, calls C R e a t e. C R e a t e into the corresponding class in the constructor and instantiate the object with it. Finally, C R e a t e calls the RE A D operation of the object, read the remaining information on the disk and initialize the instance variable of the object. A parameterized factory method has the following general form, here M y P Roduct and YO UR P Roduct are subclats of P RODUCT: Refine a parameterized factory method allows you to make you simple and selective extension or change A product produced by c reator. You can introduce new identifiers for new products, or you can associate existing identifiers with different products. For example, subclass M Y C R e a t O r can exchange M Y P R O D U C T and YO U R P R O D U C T and support a new sub-class T h E R P R O D u C T: Chapter 3 Creation mode 7 3

Note that the last thing to do this is to call the parent class's C R E A T. This is because M Y C R E A T O R:: C R E A T E is only different from the processing of Y O U R S, M i N E and T H E Rs and the parent class. It is not interested in other classes. Therefore, M Y C R e a t O r extends the type of product created, and will delay the creation responsibility of all products other than a few products to the parent class. 3) Variations and problems of specific languages ​​and different languages ​​help to generate other interesting changes and warnings (C A V e a t). S m a ll t a l k program typically uses a method to return to the class of objective objects. The C R e A T O R factory method can use this value to create a product, and C O N C R e t e c R E A T O R can store even calculate this value. This result is a more late binding for the instantiated C O N C R e t e P R O D u c T type. Examples of the D O C U m E N T of S M a L L T a L K can define a D O C U m E N T C L A Sk method in A P P L I C A T I O n. The method returns a suitable D O C U m e n T class in the instantiation document, which returns M Y D O C U m e n T class in M ​​Y a P P L I C A T I O N. In this case, we have in class A p L I c a t i o n in the class M y A p L I c a t I o n, we have it returned to the class M Y D O C U m e n n to a pp L i c a T I O n. A more flexible approach to the parameter chemical plant method is to store the created class as a class variable of A P P L i c A T I O N. You use this method to change the product when changing the product. Subcounds of A P L I C A T I O N. Factory methods in C are virtual functions and are often pure virtual functions. Be sure to pay attention to the factory method in the constructor of C R E A T O R - the plant method is not available in C O N C R E T E C R E A T O R. As long as you use visitors who need to create a product, you can avoid this. You can avoid this. The constructor only initials the product to 0 instead of creating a specific product. Visitors returned to this product. But first it is to check to determine the existence of the product. If the product does not exist, the visitor creates it. This technique is sometimes referred to as L A Z Yi N I t I a L I Z A T I O n. The following code gives a typical implementation: 4) Use templates to avoid creating subclasses as we have already mentioned, factory methods Another potential problem is to force you to create C only to create an appropriate P RODUCT object Reator subclass. Another solution in C is a template subclass for providing C R e A T O R, which uses P R O D u c T class as a template parameter: 7 4 Design mode: can be used for object-oriented software

With this template, customers only provide product classes - without the need to create subclasses of C R E A T O R. 5) Naming conventions Using naming agreements are a good habit, it can clearly explain that you are using factory methods. For example, the application framework MACAPP [APP89] of M A C i N T O S h is always declare that the abstract operation defined as a factory method is C L A S S * DomakeClass (), where C L A S is is the P RO D u C T. 10. Code sample function C R e a t e m a z e (Chapter 3) is built and returned to a maze. One problem in this function is that it hardcodes the class of maze, room, door, and wall. We will introduce factory methods to make the subclass of these components. First we will define the factory method in M ​​A Z E G A M e to create maze, room, wall and door object: Each plant method returns a given type of maze member. M A Z E G A M A provides some default implementation, which returns the easiest labyrinth, room, walls and doors. Now we can rewrite C R e a t e m a z e: Chapter 3 Creating Models 7 5 Different games You can create a subclass of M A Z E G A M E to specify some of the maze parts. M a Z E G A ME E-class can redefine some or all factory methods to specify changes in the product. For example, a B OMBED M AZE G AME can redefine product R Oom and Wa LL to return a variant of the explosion: an e nchated M Aze G AME variant can be defined like this: 11. The application factory method is mainly used Toolkit and frame. The previous document example is a typical application in M ​​A C A P P and E T [W G M 8]. An example of the manipulator comes from U N i D R a W. SmallTalk-80 Model / VI E W / C O N T R O LERE RF Frame (Class Vi E W) There is a method of creating a controller D e f A U L T C O N T R O L1, which is a bit similar to a factory method [P A r 9 0]. However, the subclasses of VI E W specify the classes of the default controller by defining D E f A U L T C O N T R O L E R C L A S s. D e f A U L TE C O N T R O L E Returns the class of DE F A U L T C O N T R O L ER, so it is a true factory method, ie, ie should redefine it. A more profound example in S M a L t a l k - 8 0 is a factory method P a R s e r C L a s s defined by B E H A V I O R (superclass) defined by the class. This makes a class that can use a custom syntax analyzer for its source code. For example, 7 6 design mode: can be used for object-oriented software

A customer can define a class S Q L p A R s E R to analyze the source code of the class embedded in the S Q L statement. B E H A V I O R Class implements P A R S E R C L A S S, returns a standard SMALLTALK PARSER class. A calibration that includes embedded S Q L statements defines the method (in the form of a class method) and returns S Q L P a R S ER. Orbix ORB Systems of Iona TE C H N O L O G I E E E E E E E E S When the object gives a remote object reference Send request, use the Factory Method to generate an appropriate type of agent (see P R O X Y (4. 7)). Factory Method makes it easy to replace the default agent. For example, you can replace it with a proxy with a client cache. 12. Related Mode Abstract Factory (3. 1) often uses factory methods to be implemented. An example of the motivation section in the Abstract Factory mode also describes the Factory Method. Factory methods are typically called in Template Methods (5. 1 0). In the above document example, N e w d o c u m e n is a template method. P R O T O T Y P E S (3. 4) Does not need to create a subclass of C R E A T O R. However, they usually require an operation of I N i T i a L i z e for P R O D U C T. C R E A T O R initializes the object using I N i T i a L I z E. Factory Method does not require this operation. 3.4 Prototype - Object Create mode 1. Intensive Use Prototype Instance Specify the type of object to create an object, and create a new object by copying these prototypes. 2. Motivation You can construct a score editor by customizing a universal graphic editor framework and adding new objects that represent notes, rests, and five-wire profiles. This editor frame may have a tool selection board to add these music objects into the score. This selection board may also include tools for selecting, movement, and other manipulated music objects. Users can click the quarter note tool and use it to add quarters to the score. Or they can use the mobile tool to move a note up and down on the five-wire spectrum, thereby changing its tone. We assume that the framework provides an abstract G R a P H i C C class for the graphics components such as notes and five tabs. In addition, in order to define those tools in the selection board, there is also an abstract class to O L. The frame is also a tool for creating a graphic object instance and adds them to the document to predefine a G R a P H i c to O L subclass. But G R a P H i c to O L brought a problem to the frame designer. The category of the note and the five-wire spectrum is specific to our application, while G R a P H i c to O L is a framework. G R a P H i c to o L does not know how to create an instance of our music class and add them into the score. We can create a subclass of G R a P H i C To O L for each music object, but there is a large number of subclasses, which are only different in the categories of music objects they initial. We know that object composite is a more flexible choice than creating subclasses. The problem is how the framework uses it to parameterize the instance of G R A P H i C To O L, and these instances are created by the G R a P H i C class. The solution is to create a new G R A P H i C by copying or "clone" an example of a G R a P H i C subce, we call this example as a prototype. G R A P H i C To O L The prototype should be cloned and added to the document as a parameter.

If all G R a P H i C subclasses support a C L O N e operation, then G R A P H i C to O L can clone all types of G R A P H i c, as shown in the following page. Therefore, in our music editor, each tool for creating a music object is an instance of G R a P H i c to O L in different prototypes. By cloning the prototype of a music object and adding this clone to the score, each G R a P H i c to o L instance generates a music object. Chapter 3 Creating Models 7 7 We can even further use the P R O T O T Y PE mode to reduce the number of classes. We use different classes to represent full notes and halftone, but it may not need to do this. They can be an example of the same class using different bitmaps and time delay initialization. A tool for creating a full note is such G R a p H i c to O L, which is an original M u s i c a L N o TEU that is initially formed into a full note. This can greatly reduce the number of systems in the system, and it is also easier to add new notes in the music editor. 3. Applicability When a system should be independent of its product creation, makeup, and representation, use P Rototype mode; and • When the class to be instantiated is specified at runtime, for example, by dynamic loading; or Avoid creating a factory class level parallel to the product class level; or • When a class is only one of several different status combinations. Establish a corresponding number of prototypes and clone them may be more convenient than manually modified each time you use a suitable state. 4. Structure 5. Participants • P R O T O T Y P E (G R A P H i C) - Declare a cloning of itself. • C O N C R E TE E P R O T O T Y P E (S T a F, W H O L E N O T E, H A L f N o T E) - implements a cloning itself. • C L i e n t (g R a p h i c to o l) - Let a prototype clone itself to create a new object. 7 8 Design mode: can be used for object-oriented software

6. Collaboration • Customer requests a prototype clone itself. 7. Effects P R O T O T Y P E has many effects as Abstract Factory (3. 1) and B U I L D E R (3. 2): It hides the specific product class for customers, thus reducing the number of the names known by customers. In addition, these patterns allow customers to use classes associated with specific applications without changing. Another advantage of the P R O T O T Y PE mode is listed below. 1) Operation Time Increase and Delete Product P R O T O T Y P E Allows to incorporate a new specific product class to the system only through a customer registration prototype instance. It is more flexible than other creation modes because customers can establish and delete prototypes at runtime. 2) Change the value to specify the high-grade dynamic system of the new object allows you to define new behaviors through object composite, for example, by specifying values ​​for an object variable - and does not define new classes. You can effectively define new categories of objects by instantiating existing classes and registers these instances as a customer object. Customers can represent the role to prototype, thus showing new behaviors. This design allows users to define new "classes" without programming. In fact, cloning a prototype similar to instantiation. P R O T O T Y P E mode can greatly reduce the number of classes required by the system. In our music editor, a G R a P H i C To O L class can create countless music objects. 3) Change the structure to specify a number of new objects to create objects from components and sub-parts. For example, the circuit design editor is constructed by a sub-circuit. For convenience, such applications typically allow you to instantiate complex, user-defined structures, ratio, once again repeatedly use a specific sub-circuit. This is also supported by P R o T O T Y P. We only need to increase this sub-circuit as an original to the available circuit element selection board. As long as the composite circuit object is implemented as a deep copy, a circuit with different structures can be prototype. 4) Reduce the construction of subclasses Factory Method (3) often produces a Class C-class hierarchical level parallel to the product level hierarchy. P R O T O T Y P E-mode allows you to clone a prototype instead of requesting a factory method to generate a new object. So you don't need a Class of C RE A T O R Class at all. This advantage is mainly suitable for languages ​​that do not use classes as a class object like C . Language like S M a L L t a l k and Objective C will benefit less, because you can always use a class object as a generator. In these languages, class objects have played the same role in prototypes. 5) Use the Dynamic Configuration Apply Some Runtime Environments Allow you to dynamically load the class into your application. In a language like C , the P R O T O T Y PE mode is the key to this function. An application that wants to create an instance of a dynamic load class cannot be static reference to the constructor. And the instance of each class should be automatically created by the running environment, and this instance is registered with the prototype manager (see Implementation section). This application can request an instance of a newly loaded class to the prototype manager, which is not connected to the program. E T Application Frame [W G M 8 8] There is a running system to use this solution. The primary defect of P R o T o T y P e is that the subclass of each P R O T O T Y P e must implement C L O N e operation, which may be difficult. For example, it is difficult to add C L O N e operation when the class considered existence. Implementing clones may be difficult when they include some objects that do not support copying or cyclic references. 8. Implement Chapter 3 Creating Modes 7 9

Such applications reflect C O M P O S I T E (4. 3) and D E C O R A T O R (4. 4) mode. Because in a static language like C , the class is not an object, and the runtime can only get very few or any type information, so P R O T O T Y P e is particularly useful. In the language of S M a L t a l k or Objective C, P R O T O T Y P E is not that important, because these languages ​​provide an equivalent prototype (ie, class object) to create instances of each class. P R O T O T Y P E Mode is inherently inherent based on the prototype language like S E L F [U S 8 7], and all objects are created by cloning a prototype. When realizing the prototype, consider some of the following questions: 1) Use a prototype manager When the number of originals in a system is not fixed (that is, they can be dynamically created and destroyed), to maintain an available prototype. Customers will not manage prototypes themselves, but store and retrieve prototypes in the registry. The customer requests the prototype to the registry before cloning a prototype. We call this registry as a prototype manager. The prototype manager is an associated memory (which returns a prototype that matches the given keyword. It has some operations to be used to register prototypes and released by keywords. Customers can change even at runtime or browse this registry. This allows customers to extend and get system lists without writing code. 2) The most difficult part of achieving the clone operation P R o T o T y P E mode is to properly implement C L O N e operation. This is particularly tricky when the object structure contains cyclic references. Most languages ​​provide some support for cloning objects. For example, S M A L1 provides an implementation of C O P Y, which is inherited by the subclass of all O b J E C T. C provides a copy constructor. But these facilities do not solve the "shallow copy and deep copy" problem [G R 8 3]. That is, the cloning is a subject to clone its instance variable, or shared these variables by cloning objects and the original object? Shallow copy is simple and is usually enough, it is deficed by S M A L t a lk. The default copy constructor in C implements a copy of the member, which means that between the copy and the original object is a shared pointer. However, a complex prototype cloning usually requires deep copy because copy objects and original objects must be independent of each other. Therefore, you must ensure that the components of the cloned object are also clones of the components of the prototype. Cloning forces you to decide what if all things have been shared. If the objects in the system provide S A V e and L O A D operation, you only need to provide a default implementation for C L O N e operation by saving objects and immediate loading objects. The S A V e operation stores the object in the memory buffer, while L O A D creates a copy by reconstructing this object from the buffer. 3) Initialization Clone Object When some customers have been quite satisfactory, others will want to use some of the values ​​they choose to initialize some or all internal states of the object. It is generally not possible to deliver these values ​​in C L O N e operation, because the number of these values ​​will vary depending on the type of prototype. Some prototypes may require multiple initialization parameters, and others may not. Transfer parameters in C L O N e destroy the unity of cloning interfaces. This may be, the class of the prototype has set some key status values ​​for (heavy). If this is, the customer can use these operations immediately after clonality.

Otherwise, you may have to introduce an I n i t i a l i z e operation (see Code Examples section), which uses initialization parameters and sets the internal state of the micro object. Pay attention to deep copy C L o N e Operation - Some copies may have to be deleted before you reinitialize them (delete can be done inside I n i t i z e). 9. Code Example We will define the subclass M A z E P R Ot O R Y of M A Z E F A C T O R Y (3. 1). The subclass will initialize the prototype of the object it want to create, so we do not need to generate Sub 8 0 design mode only in order to change the wall or room of the room it created: can be used for object-oriented software. . M Aze P Rototype F Actory uses a constructor with a prototype to expand the M aze f Actory interface: The new constructor only initializes its prototype: The member function used to create walls, rooms, and doors is similar: each To clone a prototype, then initialize. The following is the definition of M AKE WA LL and M AKE D OR: We only need to initialize the prototype of the basic maze member, you can create a prototype or default maze: in order to change the maze Type, we use a different prototype collection to initialize M Aze P Rototype F Actory. The following call creates a maze with a B O M B E D D O O R and a R O O M Wi T H A B O M B: An object that can be used as a prototype, such as a case of Wa L, must support C L O N e operation. It must also have a copy of Chapter 3 Creating Models 8 1

Betcules are used to clone. It may also need a separate operation to reinitialize internal status. We will add I n i t i a l i z e operation to D O O R to allow customers to initialize the room of the clone object. The definition of D O O R is compared to Chapter 3 of Chapter 3: The B O M B E D WA L L subcatency must redefine C L O N e and implement the corresponding copy constructor. Although B O M B E D WA L:: C L O N E returns a WA L *, its implementation returns a pointer to a new instance of subclass, ie B O M b E d Wa L *. We define that C l O N e is in the base class, to ensure that the clonal original customers do not need to know the specific subclasses. Customers must never need to convert the return value of C L O N E to the desired type. In S M A L, in S M A L, you can reuse the standard C O P Y method inherited from O B J E C T to clone any M A P S i t e. You can 8 2 design mode: can be used for object-oriented software

Use m a z e f a c t o R y to generate the prototype you need; for example, you can provide name # r o o m to create a room. M a z e f A c t o R Y has a dictionary that maps the name as a prototype. Its Make: The method is as follows: Assuming useful prototype initializing the appropriate way M aze f Actory, you can create a simple maze with the following code: The definition of the CREATE M AZE ON: The definition will be 10. It is known to apply possible P The first example of the RotoType mode appears in the S ipchPad system of Ivan Sutherland [S UT 6 3]. This mode is in the object-oriented language in T hing L AB, where the user can generate a composite object, then install it into a reusable object library to cause it to become a prototype [B or 8 1 ]. Both G O L D b E RG and R O B S O N proposed a prototype as a mode [G R 8 3], but C O P L I e n [C O P 9 2] gives a more complete description. He describes the terminology associated with P R O T O T Y P E-mode and gives a lot of examples and variants. E T G D B is a debugger front end based on E T , which provides a point-in-tuning (P O I N-a N D-C L I C K) interface for different row-oriented (L i n E - O R I E N T E D) debugger. Each debugger has the corresponding D e B U g g E R A D A P t O R subclass. For example, G D B A D A P O r adapts E T G D b to G N u's G D b command syntax, while S u n D b x a d a pt O r adapts E T G D b to adapt the S u n's D b X debugger. E T G D B does not have a set of hard-encoded D e B U g g E R A D A P t O R classes. It reads the name of the adapter you want to use from the environment variable, query the prototype according to a particular name in a global table, and then clone this prototype. The new debugger can be added to the E T G D B by linking D e b U g g E R A D A P t O R corresponding to the debug. "Interaction Technique Library" in Mode Composer stores prototypes for multiple interaction techniques [S H A 9 0]. Place any interaction technology created by Mode Composer in this library, which can be used as a prototype. P R O T O T Y P E-mode allows Mode Composer to support the number of unlimited interactions. Examples of the music editor discussed earlier are [V L 9 0] based on the U N I D R A W Drawing Frame. 11. Related Mode Chapter 3 Creating Models 8 3

As we discussed in this chapter, P R O T O T Y P E and Abstract Factory (3. 1) mode compete in some way. But they can also be used together. Abstract Factory can store a collection of prototypes that is cloned and returns product objects. A large number of designs of C O m P O S I T E (4. 3) and D E C O R (4) are usually benefited from P R O T O T Y P E mode. 3.5 Singleton - Object Creation mode 1. Intensive to ensure that a class has only one instance and provides a global access point access to it. 2. Motivation for some classes, only one instance is very important. Although there are many printers in the system, it should only have a print spooler, and should only have a file system and a window manager. A digital filter can only have an A / D converter. A accounting system can only be dedicated to a company. How can we guarantee that only one instance is a class and this instance is easy to access? A global variable allows an object to be accessed, but it does not prevent you from instantiating multiple objects. A better way is that the class is responsible for saving its unique instances. This class ensures that no other instance can be created (by intercepting a request for creating a new object), and it can provide a method to access the instance. This is S i N g l e t o n mode. 3. Applicability Use S i N g L e t o n mode in the following situations • When the class can only have one example and the customer can access it from a well known access point. • When this unique instance should be expandable by subclassization, and the customer should use an extension instance without changing the code. 4. Structure 5. Participants • S i N g l e t o n- Define an I n S t a n c e operation allows customers to access its unique examples. I N S T A N c E is a class operation (ie, one class method in S M a L t a Lk and a static member function in C ). - May be responsible for creating its own unique instances. 6. Collaboration • The customer can only access an example of one S i N g l e t o n by I n s t a n c e operation by S i n g l e t o n. 7. Effects S i N g l e t o n mode has many advantages: 1) Controlled access to the unique instance is encapsulated by S i N g l e t O n class, so it can strict control 8 4 design mode: can be used for object-oriented software

How do customers and when to access it. 2) Reduce the namespace S i n g l e t o n mode is an improvement to global variables. It avoids global variable contamination names for storing unique instances. 3) Allowing the refining Si N g L e t o n category of operation and representation, it is easy to configure an example with this extension class. You can configure applications at runtime using instances you need. 4) Allow variable objective instances This mode makes you easy to change your ideas and allow S i n g l e t O n to multiple instances. In addition, you can use the same way to control the number of instances used by the application. Only the operation that allows access to S i N g l e t o n instance needs to be changed. 5) Method for more flexible and other packaging functions than class operations is the use of class operations (ie, static member functions in C or class methods in S M A L T a Lk). But both language technologies are difficult to change to allow a class to have multiple instances. In addition, the static member functions in C are not virtual functions, so the subclasses cannot redefine them. 8. Implementing the implementation problem to consider using S i N g l e t o n mode: 1) To ensure a unique instance S i n g l e t o n mode makes this unique instance of a class of a class, but only one instance can be created. One common way to do this is to hide the operation of this instance hide behind a class action (ie, a static member function or a class method), which guarantees that only one instance is created. This operation can access variables that save unique instances, and it can ensure that this variable is initialized with this unique instance before the return value. This method guarantees that the single piece is created and used before its first use. In C , you can define this class operation with the static member function I n S t a n c e of the S i N g L e t o n. S i n g l e t o n also defines a static member variable _ i n s t a n c e contains a pointer to its unique instance. The Singleton class defines the following implementation is that the customer will access this single piece only through the I n s t a n c e. Variable _ i n s t a n c e initializes 0, and the static member function I n S t a n c e returns the variable value if its value is 0, and it is initialized with a unique instance. I N S T a N c e initializes inert (L A Z Y); its return value is created and saved until it is first accessed. Note that the constructor is protected. Attempting to directly instantiate the customer of S i n g l e t o n will get an error message when compiling. This ensures that only one instance can be created. Further, since _ i n s t a n c e is a pointer to the S i N g l e t o n object, I n S t a n c e member function can assume a pointer to the subclass of S i n g l e t o n to this variable. We will give one such example at the code example. Chapter 3 Creating Models 8 5

There is still a little attention to the implementation of C . Define a single piece as a globally or static object, depending on the automatic initialization, which is not enough. There are three reasons: a) We cannot guarantee that only one instance of static objects will be declared. b) We may not have enough information to instant each single piece when static initialization. One piece may need to be calculated later in the program execution. c) C does not define the call sequence of the constructor of the global object on the translation unit [E S 9 0]. This means that there is no dependency between single pieces; if there is, then the error will be inevitable. There is another (despite the small) disadvantage using the implementation method of global / static objects, which makes all single items to be created or not. All these issues are avoided using a static member function. In S M a L t a lk, the function of returning the unique instance is implemented as a class method for the S i N g L e t O n. To ensure that only one instance is created, the N e w operation is redefined. The resulting S Ingleton class may have the following two ways, where S OLE I Nstance is a class variable that is not used in other places: 2) The main problem with the subclass of the S Ingleton class is not as good as the definition. Its unique instance so customers can use it. In fact, the variables that point to the single instance must be initialized by an instance of subclasses. The simplest technique is to determine which one you want to use in the i n s t a n c e operation of S i N g l e t o n. An example in the code example section illustrates how this technology is implemented with environment variables. Another method of selecting Si N g l e t o n is separated from the implementation of I N S T a N C E from the parent class (ie M A z E F A C T O R Y) and puts it into a subclass. This allows C programmers to determine the class class at the link time (ie, by linking into an object file containing different implementations), but covers this. The link method determines the choice of the single class at the time of the link, which makes it difficult to select a single class class at the runtime. Use the conditional statement to determine the subclass more flexible, but this hard-defined (H A R D - W I r e) has a collection of possible S i N g l e t o n classes. These two methods are not flexible in all situations. A more flexible approach is to use a single-part registry (Registry of Singleton). The set of possible S i N G L e t O n is not defined by I N S T a N c e, and the S i n g L e t O n can register their single-piece instances in a well known registry in a well known registry. This registry establishes a mapping between a string name and a single piece. When I N s t a n c e requires a single piece, it refers to the registry and requests a single piece according to the name. The registry queries the corresponding single piece (if present) and returns it. This method makes I n S t a n c e no longer need to know the possible S i N G L e t O n or examples. It is only a common interface of all S i n g l e t o n classes, which includes operations for registry: 8 6 Design mode: can be used for object-oriented software

RE G I S TER is registered with S i N g l e t o n instances at a given name. In order to ensure the registry is simple, we will let it store a column N a m e s I n g l e t o n p A I r object. Each N a M e s I n g l e t o n p A I r maps a name to a single piece. The L O O K u p operation is found according to the name of a given single piece. We assume that an environment variable specifies the name of the desired single piece. Where is the S i N g l e t o n? One may be in their constructors. For example, M Y S i N g l e t o n subclass can do so: Of course, this constructor will not be called unless an instantiation class, this is reflecting the problem of S i N g l e t o n to try to solve it! In C we can define a static instance of M Y s i n g l e t o n to avoid this problem. For example, we can define in a file that contains M y s i n g l e t o n: static mysingleton trissingleton; S i N g l e t o n Class is no longer responsible for creating a single piece. Its main responsibility is to make the selection single-piece object can be accessed in the system. Static object methods still have a potential disadvantage - all possible examples of all possible S i N g l e t o n subclasses must be created, otherwise they will not be registered. 9. Code example assumes that we define a M a z E F A C T O R Y class to build the maze described in Chapter 3. M a z e f A c t o R y defines an interface to construct a different part of the labyrinth. Subclasses can redefine these operations to return instances of a particular product class, such as using B O m b E d Wa L object instead of ordinary WA L L objects. The problem related to this is that M a z e application requires only an example of a labyrinth factory, and this instance is available to the code of any component of the labyrinth. This introduces S i N g l e t o n mode. As a single piece of M A z e f A C t O R Y, we need to have a global accessibility that can make the labyrinous objects with global variables. For the sake of simplicity, we assume that the subclass of M A z E F A C T O R Y is not generated. (We will then consider another option.) We generate a S i N g l e t O n in C by increasing the static i n s t a n c e operation and static member _ i N S T a N C E to save the unique instance. We must also protect constructors to prevent accidental instances because unexpected instantiation may cause multiple instances. Chapter 3 Creating Models 8 7

The corresponding implementation is: Now let's consider the case when the M a Z E F A C T O R Y is present, and the application must decide which subscate is used. We will select the type of maze through environment variables and increase the code based on the value of the environment variable to instantiate the appropriate M A z E F A C T O R y subclass. I N S T A N c E operation is a great place to increase these code because it has been instant M a z e f A c t o R y: Note, whether when a new M A Z E F A C T O R Y is defined, i n S t a n c e must be modified. This may have no relationship in this application, but for the abstract factory defined in a framework, this may be a problem. A possible solution will be a method of using the registry described in the implementation section. This dynamic link may also be also useful - it makes the application not need to load those subclasses that do not need. 10. Examples of S i N g l e t o n mode in S M A L t a l k - 8 0 [P A R 9 0] are the collection of code, that is, Changeset Current. A more clever example is the relationship between classes and their categories (M E T a C L a s s). A class class is a class of classes, and each class class has an example. The category has no name (unless indirectly through their unique instance), they record their unique instances and usually no longer create other instances. I N t e R VI E W S User Interface Toolbox [L C i 9 2] Using S i N G L E T O N mode Access SE SE SE SES SO I O N and Wi D G E T K I T Class Single Examples. S e s S i o n defines the application's main event scheduling loop, stores the user's style preference database, and manages connections to one or more physical display. Wi D g e t k i t is an Abstract Factory (3. 1) for defining the user's window components. Wi D g e t k i t:: i n s t a n c e () operation determines a particular Wi D g e T K I T subclass, which is instantiated according to the environment variables defined by S E S S I O N. One similar operation of S e s S I o n determines supporting monochrome or color display and 88 design mode: can be used for object-oriented software

According to this, instances of single piece S e S S I O N are configured. 11. Related modes Many modes can be implemented using S i N g l e t o n mode. See Abstract Factory (3. 1), B U I L D E R (3. 2), and P R O T O T Y P E (3. 4). 3.6 Discussion of Creating Models There are two common methods for parameterizing systems with those objects created by a system. One is a subclass of a class that creates an object; this corresponds to using Factory Method (3. 3) mode. The main disadvantage of this method is that only a new subclass may be created only in order to change the product class. Such a change may be cascaded (C A S c a d e). For example, if the product's creator itself is created by a factory method, then you must also redefine its creator. Another method for parameterizing the system more dependent on object composite: Defines an object responsible for clarifying the class of the product, and uses it as the parameters of the system. This is a key feature of Abstract Factory (3. 1), B U I L D E R (3. 2) and P R O T O T Y P E (3. 4) mode. All three models involve creation a new "factory object" responsible for creating product objects. Abstract Factory generates multiple classes objects by this factory object. B U I L D e r Use a relatively complex protocol by this factory object to gradually create a complex product. P R O T O T Y P e creates product objects by copying the original object by the plant object. In this case, because the prototype is responsible for returning the product object, the factory object and prototype are the same object. Consider the drawing editor frame described in the P R O T O T Y PE mode. There are several ways to parameterize G R a P H i c to o L: • Use the Factory Method mode to create a subclass of G R a P H i C to O L for the subclass of each G R a P H i C in the selection board. G R A P H i c to O L will have a N e w g r a p h i c operation, and the subclasses of each G R a P H i c to O L will define it. • Use the Abstract Factory mode to have a G R a P H i C S f A C T O R Y level layer corresponding to subclasses of each G R a P H i C. In this case, each plant only creates only one product: C i r C L e f A c t O R Y will create C i r C L E, L i n e f A c t o R Y will create L i n e, and the like. G R A P H i C To O L will be used as a parameter in the factory that creates a suitable kind G R a P H i C. • Using the P R O T O T Y P E mode, the subclass of each G R a P H i C will implement C L O N e operation, and G R a P H i c to O L will be used as the prototype of G R a P H i C it created. Which mode is preferably dependent on many factors. In our drawing editor frame, the first looks, F A C T O R Ym e T H o D mode is the easiest. It is easy to define a subclass of a new G R A P H i C To O L and only when the selection board is defined, the instance of G R A P H i C to O L is created. Its main disadvantage is that the number of G R a P h i c to o L subclasses has not been made, and they have not made a lot of things.

Abstract Factory has not improved because it requires a hierarchical level of G R a P H i C S f A C T O R Y. Only when there is a G Raphics F Actory class level, Abstract Factory is better than Factory Method - or because the compiler is automatically provided (like the S Malltalk or Objective C) or because of the other parts of the system G RAPHICS F ACTORY class level. In general, P R O T O T Y P E-mode may be the best on the drawing editor framework because it only needs to implement a C L O N e operation for each G R a P H I C S. This reduces the number of classes, and C L O N e can be used for other purposes, not just pure instantiation (eg, a D u P L I C A T E menu operation). Factory Method makes a design can be customized and only slightly complicated. Other design patterns require new classes, and FactoryMethod only needs a new action. People usually create FACTORY METHOD as a standard creation of Chapter 3 Creating a Model 8 9 method. However, this is not necessary when the instantiated class does not change or when the instantiation occurs in the subclass, which can be easily redefined (for example in the initialization operation). The design of using Abstract Factory, P R O T O T Y P e or B U I L D e r is even more flexible than those designs that use Factory Method, but they are more complicated. Typically, the design is designed to start using Factory Method, and when the designer finds greater flexibility, the design will evolve to other creation modes. When you trade between design standards, you can give you more options. 9 0 Design mode: can be used for object-oriented software

转载请注明原文地址:https://www.9cbs.com/read-3316.html

New Post(0)