Problem focus: the essence of the structural model is object combination. The model presented in this section is a typical structural model. What structure is the combination of objects? The combination mode combines objects into a tree structure, indicating the "part-whole" hierarchy.
Intention: Combine objects into a tree structure to represent the "Whole-part" hierarchy. The combination mode ensures consistency between the use of a single object and a composite object.
Motivation: Take a graphic application such as the drawing editor and graphics capturing system as an example. Designers can combine multiple simple components to form larger components. This violent combination method is also frequently used. DESIGN: Define classes for elements such as Text and Line, and define classes as the container classes for these elements. This method has the following problems: the code using these classes must differentiate the processing method of the combination mode between the primitive object and the container object: The Key To Recursive combination blowing in the combination mode: an abstract class, it can represent both elements and containers of elements. Design:
Abstract class (Graphic, declares operations related to specific feature objects, such as Draw, and operations shared with all composite objects)
- Subclass Line, Rectangle, and Text define some primitive objects
- The Picture class defines the aggregation of a Graphic object. The Draw operation of Picture is implemented by calling Draw for its child parts. Picture also uses this method to implement operations related to its child parts. At the same time, because the Picture interface is consistent with the Graphic interface, the Picture object can be recursively combined with other Picture objects.
Shows the structure of a typical combination object consisting of recursive Graphic objects:
Applicability: Use the combination mode below:
- Indicates the part of the object-the overall hierarchy
- If you want to ignore the differences between a composite object and a single object, you will use all the object structures in the composite structure in a unified manner:
Typical combination mode object structure:
VcDgvdO/2tPr1 + region/region + vH87eiy824 + Mv8tcTX07K/vP6jrNTa16q3osfrx/region + bG + ttTP87rN1 + m6z7bUz/region + Region Release + m8/release/Str3ateM8bGk + release + tb24uLK/release/X6brPveG5ubXEsenA + release + 3X6bz + release/dTayrnTw7XEvt/M5bXETGVhZ Authorization/authorization + ob/authorization/ehowo0IMn5w/e53MDt19Oyv7z + authorization/vP61xLLZ1/e3xdTas + authorization + 21 M/authorization + 8/rXEstnX96Os0vLOqta70 9DX6brPttTP87LF09DX07K/examples + examples/examples + 8wse9q9fTvdq1473hus + 2qNLlzqrSu7j2yrXA/bHkwb + jrMi7uvPJ + cP30rvQqbLZ1/Summary/Examples Examples/m96cnctcS1/examples + hu2txit73d538540 + examples + W05rSittTL/examples/gudjQxc + samples + s/samples/i72MrVu/rWxrXE0 +/samples/6u9nKsaOsz Examples/examples/b7dveG5uQogICAgyv2 + examples/examples + wP2jugq8xsvju/qjrNL0z + examples/t9Yt1fvM5bLjt M694bm5u/release/Eo8q9vfjQ0LPpz/release + cP30rvQqbLZ1/release + release "brush: java;"> class Equipment {public: Virtual ~ Equipment (); const char * Name () {return _ name}; virtual Watt Power (); virtual Currency NetPrice (); virtual Currency DiscountPrice (); virtual void Add (Equipment *); virtual void Remove (Equipment *); virtual Iterator * CreateIterator (); protected: Equipment (const char *); private: const char * _ name ;};
Disk Drive (Leaf node Leaf) class FloppyDisk : public Equipment{public FloppyDisk(const char*); virtual ~FloppyDisk(); virtual Watt Power(); virtual Currency NetPrice(); virtual Currency DiscountPrice();};
CompositeEquipment is the base class that contains other devices and is also a subclass of Equipment. Class CompositeEquipment: public Equipment {public: virtual ~ CompositeEquipment (); virtual Watt Power (); virtual Currency NetPrice (); virtual Currency DiscountPrice (); virtual void Add (Equipment *); virtual void Remove (Equipment *); virtual Iterator
* CreateIterator (); protected: CompositeEquipment (const char *); private: List
_ Equipment ;}; // CompositeEquipment defines some operations for accessing and managing sub-devices // operation Add and Remove insert and delete devices from the list of devices stored in the _ equipment member variable. // The CreateIterator operation returns the iterator traverses this list. // The default value of NetPrice is to use the CreateIterator class to accumulate the actual price of the sub-device. Currency CompositeEquipment: NetPrice () {Iterator
* I = CreateIterator (); Currency total = 0; for (I-> First ();~ I-> IsDone (); I-> Next () {total + = I-> CurrentItem ()-> NetPrice ();} delete I; return total ;} // now we represent the computer's Chassis as the sub-class of CompositeEquipment Chassisclass Chassis: public CompositeEquipment {public: Chassis (const char *); virtual ~ Chassis (); virtual Watt Power (); virtual Currency NetPrice (); virtual Currency DiscountPrice ();};
Class configuration is complete, including a single part and some composite parts containing several parts. Then we can assemble the computer. Cabinet * cabinet = new Cabinet ("PC Cabinet"); // Chassis * chassis = new Chassis ("PC Cabinet "); // motherboard node cabinet-> Add (chassis); // assemble the motherboard into the chassis * Bus = new bus ("MAC Bus "); // bus node bus-> Add (New Card ("16Mbs Token Ring"); // chassis of a component on the bus-> Add (bus ); // motherboard Mount bus chassis-> Add (new FloppyDisk ("3.5in Floppy ")); // cout <"The net price is" <chassis-> NetPrice () <endl;
Related mode: the component-parent component connection is usually used in the Responsibility of Chain Mode Decorator mode and Composite mode. The Flyweight mode allows you to share the component Iterator mode to traverse the operations and behavior localization that should have been distributed in the Composite and Leaf classes.
Reference: Design Pattern: Basis for reusable Object-Oriented Software