Design Mode Summary
Ii. Structural Mode
1, Adapter Mode
Convert a third-party class method to a method called in another class.
Advantages: Enable classes that are incompatible with the original interface and cannot work together, and reuse methods
Disadvantage: modifying the source class will cause trouble
Summary: it is mostly used for post-maintenance. It is used when similar classes, methods, or controls are used for modification.
Class translator // Adapter
{
Private f = new F ();
Pulic override void add () {f. Add ();}
}
2,Bridging Mode
Separate the abstract part from its implementation part so that they can all change independently.
Advantage: different abstract interfaces and implementations can be combined and expanded separately.
Modification to the abstract implementation does not affect the client.
Summary: The Bridge Extraction Section is complex and can be combined at will. Does not affect the client.
Mostly used when the client call implementation has different combinations.
Class extract action // Extract
{
Protected implementor;
Public void setimplementor (implementor)
{This. implementor = implementor ;}
Public Virtual void operation () {implementor. Operation ();}
}
3, Combination mode
Combined into a tree structure represents the part of the object-the overall hierarchy.
Advantage: users use the same single object and composite object, ignoring the differences between a single object and the entire object.
Disadvantage: It must be partially used-overall. Distinguish leaf from node.
Conclusion: The combination mode places complex logic inside to reduce the pressure on the client.
It has obvious features, tree structure, and is used in combination systems.
It is easy to understand when comparing leaf and node.
Class leaf: component // indicates the leaf node object in the combination. The leaf node does not have subnodes.
{
Public leaf (string name): Base (name ){}
Public override void add (component C) {console. writeline ("cannotadd to a leaf ");}
Public override void remove (component C) {("cannot remove from aleaf ");}
Public override void display (INT depth)
{Console. writeline (new string ('-', depth) + name );}
}
Class Composite: component // defines the behavior of a branch node, used to store sub-Parts
{
Private list <component> Children = new list <component> ();
Public composite (string name): Base (name ){}
Public override void add (component C) {children. Add (c );}
Public override void remove (component C) {children. Remove (c );}
Public override void display (INT depth)
{
Console. writeline (new string ('-', depth) + name );
Foreach (Component CO in children) {Co. Display (depth + 2 );}
}
}
4, Decoration Mode
Add more functions dynamically for existing functions.
Advantage: Dynamic addition. All inherited and decorative classes of instantiation can add functions to each other.
Conclusion: All implementation classes inherit from the same decoration class.
The implementation methods of these Implementation classes must be added to each other. When the two conditions are met.
Abstract class decorator: component // decoration class
{
Protected component;
Public void setcomponent (componentcomponent) {This. Component = component ;}
Public override voidoperation () {If (Component! = NULL) {component. Operation ();}}
}
5, Appearance Mode
Encapsulate a group of interfaces provided by the subsystem to facilitate program calls.
Advantage: it encapsulates complex interface method calls of some columns, which is more concise and does not need to be concerned about more
Disadvantage: When encapsulating a complex set of interfaces, the interface is blocked and not easy to modify. You do not know the internal mechanism.
Conclusion: The appearance Mode features high cohesion and low coupling. But it is not easy to modify when encapsulating a group of internal interfaces. Internal Method
Use when the series is stable
Class home // appearance Mode
{
Key key; door;
Public home () {key = new key (); door = new door ();}
Public void inhome () {key. inkey (); door. opendoor ();}
Public void outhome () {door. closedoor (); key. outkey ();}
}
6, Metadata Mode
Use sharing technology to effectively support a large number of fine-grained objects
Advantage: reduces the use of a large number of objects and places the majority of object states outside
Disadvantage: Except for the external status, the object must be stable because it is shared.
Conclusion: this function is used when similar objects are used in large quantities. For example, a chess piece in go can put the coordinate State outside.
Usingsystem. collections;
Classwebsitefactory // website Factory
{
Private hashtable flyweights = new hashtable ();
Public website getwebsitecategory (stringkey) // obtain the website category
{
If (! Flyweights. containskey (key ))
{Flyweights. Add (Key, new concretewebsite (key ));}
Return (website) flyweights [Key]);
}
Public int getwebsitecount () // obtain the total number of websites {return flyweights. Count ;}
}
7, Proxy Mode
Hide real modules. Provides a proxy for other objects to control access to this object.
Advantage: Hiding real modules
Summary: similar to the adapter, but the class names are different and the method names are the same
Class Proxy: Subject // proxy class
{
Realsubject;
Public override void request ()
{
If (realsubject = NULL) {realsubject = new realsubject ();}
Realsubject. Request ();
}
}