C # Design Pattern--Summary

Source: Internet
Author: User

First, Introduction:

In the front we learned the first 10 patterns in C # design pattern-Singleton mode, simple Factory mode, factory method mode, abstract Factory mode, builder mode, prototype mode, adapter mode, bridging mode, decorator mode, combined mode. Before continuing to learn the design patterns behind, it is necessary to summarize the design patterns previously studied. Let's review some of the basic concepts of these 10 design patterns.

Second, each design mode:

1. Singleton mode:

1.1, definition: Only one instance is allowed to operate at the same time, ensure that a class has only one instance, and provide a global access point to access it.

1.2, the realization method:

        1. Privatisation constructor, which makes it impossible for outsiders to create instances of this class.
        2. Declares an instance of a static variable receiving class.
        3. Defines a static common method that provides global access.

2. Simple Factory mode:

2.1, Definition: A factory object to decide which product class to create an instance (difficult to expand, once you add a new product will have to modify the simple factory method).

2.2. Advantages and Disadvantages:

2.2.1, advantages :

          1. Simple Factory mode solves the problem that the client is directly dependent on the specific object, and the client can eliminate the responsibility of directly creating the object, but only the consumer product. The simple factory model realizes the division of responsibility.
          2. Simple Factory mode also plays the role of code reuse, to buy a notebook owner only need to be responsible for consumption, do not need to implement in their own class, at this time the simple factory approach to all customers to share.

2.2.2, disadvantages :

          1. Difficult to scale, once you add a new product you have to modify the simple factory method.
          2. Once the factory class is not working properly, the entire system will be affected.

3. Factory Method Mode:

3.1, definition: the creation of specific products deferred to subclasses, the factory class is no longer responsible for the creation of all products, but only the specific factory must be implemented interface, so that the system can not modify the factory class logic to add new products, overcome the shortcomings of the simple factory model.

3.2. Member Roles:

        1. Abstract product roles , abstract classes of specific products. Any specific product should inherit the class.
        2. Specific product roles , the implementation of abstract product class-to-definition abstract methods, created by the specific factory class, there are one by one correspondence between them.
        3. A specific factory role used to create specific products.
        4. Abstract Factory role , any specific factory must inherit the abstract class.

4. Abstract Factory mode:

4.1. Definition: Abstract Factory mode provides an interface to create products that are responsible for creating related or dependent objects, without specifically specifying specific classes, and abstract factories allow customers to use abstract interfaces to create a set of related products without needing to know or care about what the actual product is actually produced.

4.2, the use of the premise:

        1. A system does not require reliance on how product class instances are created, combined, and expressed (the premise of all Factory mode applications).
        2. The system has a number of products, and the system only consumes one of the products.
        3. The system requires a library of product classes, all products appear on the same interface, and the client does not need to rely on a specific implementation.

4.3. Advantages and Disadvantages:

4.3.1, advantages : To delay the creation of specific products into the sub-class of the specific factory, so that the creation of objects encapsulated, can reduce the client and the specific product class dependencies, so that the system coupling is low, which is more conducive to later maintenance and expansion.

4.3.2, disadvantage : It is difficult to support the change of new kinds of products. This is because the abstract factory interface has identified a collection of products that can be created, and if a new product needs to be added, it is necessary to modify the interface of the abstract factory, which involves the transformation of the abstract factory class and all subclasses, thus violating the development closure principle.

5. Builder Mode:

5.1, definition: The construction of a complex object is separated from its representation, so that the same build process can create different representations, so that the separation between the construction code and the presentation code, so that the client does not have to know the details of the internal composition of the product, thereby reducing the client and the specific product coupling degree. The essence of the builder model is to decouple the assembly process from the creation of the specific product.

5.2. Key points of realization:

        1. In the builder mode, the conductor deals directly with the client, and the conductor divides the client's request to create the product into a build request for each part, which is then delegated to the specific builder role, and the concrete builder's role is to complete the construction of the specific product, but it is not known to the customer.
        2. The builder pattern is primarily used to "step through the construction of a complex object", where "step-by" is a fixed combination process, and the parts of the complex object change frequently.
        3. The product does not require abstract classes, as the final product created by the build pattern may vary greatly, so it is unlikely that an abstract product class will be extracted.
        4. The abstract factory model solves the demand change of the "series", while the builder model solves the need for "product parts".
        5. Since the builder hides the assembly process for a specific product, it is necessary to change the internal representation of a product by implementing a specific builder, which will be able to respond well to changes in the requirements of the constituent components of the product.

6. Prototype Mode:

6.1, Definition: Prototype mode use a prototype object to indicate the type of object to be created, and then use the method of copying the prototype object to create more of the same type of object.

6.2, use the background: When creating an instance of a class is complicated, and we need to create instances of such classes, if we use new to create such instances of the class, it will increase the complexity of the creation of classes and more memory space, because the memory is allocated a number of identical class instance objects. Because each class instance is the same, when we need more than one instance of the same class, it is not necessary to use new to create the same class instance object each time, we only need to create a class instance object, if more such instances are needed later, can be created by copying a copy of the original object, This makes it unnecessary to create more than one instance of the same class in memory, thus reducing memory consumption and achieving reuse of class instances.

6.3. Advantages and Disadvantages:

6.3.1, advantages :

          1. Prototype mode hides the complexity of creating new instances from the customer.
          2. Prototype mode allows for dynamically increasing or fewer product classes.
          3. Prototype mode simplifies the creation of an instance, and the factory method pattern needs to have a hierarchical structure that is the same as the product class hierarchy, which is not required for prototype mode.
          4. The product class does not need to determine the hierarchy of the product in advance, because the prototype pattern applies to any hierarchy.

6.3.2, disadvantages :

          1. Each class must be equipped with a clone method.
          2. Having a cloning method requires a holistic view of the functionality of the class, which is not difficult for new classes, but not necessarily easy for existing classes, especially when a class reference does not support serialization of indirect objects, or when a reference contains a looping structure.

7. Adapter Mode:

7.1, Definition: The adapter mode will "put the existing objects" in the new environment to invoke, so that the new environment does not need to repeat the implementation of the existing implementation is very good to the current object (referring to existing objects in the original environment) to join the new environment to use. The adapter mode has two forms: the adapter mode of the class and the adapter mode of the object.

7.2, realization of thinking: In the adapter mode, the adapter can be an abstract class, and the implementation of the adapter mode is very flexible, we can completely adapter mode "Existing objects" as the new interface method parameters, the adapter class can be based on parameter parameters can return an appropriate instance to the client. Transforming a class's interface into another interface that the client expects, so that the original interface does not match the two classes that cannot work together can work together.

7.3, the use of the premise:

        1. The system needs to reuse existing classes, and the interfaces of that class do not meet the requirements of the system.
        2. You want to create a reusable class that works with some classes that are not too much related to each other, including some that might be introduced in the future.
        3. For the object adapter pattern, there is a need to change the interfaces of several existing subclasses in the design, and if you use the class's adapter pattern, you should make an adapter for each subclass, which is not practical.

8. Bridging mode:

8.1, Definition: Bridging mode will be part of the abstraction and implementation of decoupling, to achieve the abstraction and realization of decoupling, so that they are independent of each other, so that they can change independently.

8.2, the use of the premise:

        1. A system needs to add more flexibility between the component's abstract role and the materialized role, avoiding the need to establish a static connection between the two levels.
        2. Design requires that any changes to the implementation role should not affect the client, or that changes to the implementation role are completely transparent to the client.
        3. Graphics and windowing systems that need to span multiple platforms.
        4. There are two independently varying dimensions for a class, and two dimensions need to be expanded.

8.3. Advantages and Disadvantages:

8.3.1, Advantages:

          1. Decoupling the abstract interface from its implementation.
          2. Abstractions and implementations can be extended independently, without affecting each other.
          3. Implementation details are transparent to the customer and are used to hide specific implementation details.

8.3.2, Disadvantages:

          1. Increases the complexity of the system.

9. Decorator Mode:

9.1, definition: in a transparent manner to the customer to add additional responsibilities to an object dynamically, the use of object composition rather than inheritance to achieve the ability to dynamically expand the function of the object, compared to the generation of sub-classes can be more flexible to increase functionality, and can be expanded as needed to multiple functions, Avoids the flexibility of using inheritance alone and the multi-subclass derivation problem.

9.2. Background: In software development, we often want to add different functions to a class of objects, if the use of inheritance at this time, we need to define a myriad of classes, which will lead to the "sub-class explosion" problem, in order to solve this problem, decorator pattern appeared.

9.3. Member Roles:

        1. Abstract Component Role : An abstract interface is given to standardize the objects that are prepared to accept additional responsibilities.
        2. Concrete Component Roles : Defines a class that will receive additional responsibilities.
        3. Decorative Role : Holds an instance of a component (Component) object and defines an interface that is consistent with the abstract component interface.
        4. Specific Decorative role : Responsible for the Component object "affixed" additional responsibility.

9.4, the use of the premise:

        1. You need to extend the functionality of a class or add additional responsibilities to a class.
        2. You need to dynamically add functionality to an object that can be revoked dynamically.
        3. There is a need to increase the number of features that arise from the permutations of some basic functions.

9.5. Advantages and Disadvantages:

9.5.1, advantages :

          1. Decoration This mode and inheritance are intended to extend the functionality of objects, but decorator mode is more flexible than inheritance.
          2. By using different decorative classes and permutations of these classes, designers can create combinations of many different behaviors.
          3. Decorator mode is well-extensible.

9.5.2, disadvantages :

          1. Decorator mode causes many small objects to appear in the design, and if overused, it makes the program more complex. And more objects are going to make mistakes that are difficult, especially when these objects look alike.

10. Combination mode:

10.1. Definition: Combined mode simple objects and composite objects must implement the same interface, decoupling the internal structure of the client and complex elements, so that the client program can handle complex elements like simple elements. Allows you to combine objects into a tree structure to represent a "part-to-whole" hierarchy, allowing customers to handle a single object and the combination of objects in a consistent manner.

10.2, the background: in the software development process, we often encounter the processing of simple objects and composite objects, because simple objects and composite objects in the functional differences, resulting in the operation process must distinguish between simple objects and composite objects, which will lead to customer calls unnecessary trouble, but as a customer, They want to be able to always treat simple objects and compound objects consistently, but the combination pattern is the solution to such problems.

10.3, the use of the premise:

        1. You need to represent the whole or part of an object hierarchy.
        2. If you want users to ignore the combination object and the individual object, the user will use all the objects in the composite structure uniformly.

10.4. Realization Mode:

        1. Transparent combination mode: Abstract build classes define all common interfaces and default behavior for the objects that participate in the composition.
        2. Secure Combination Mode: The method declaration of the management sub-object is placed inside the compound object.

10.5. Member Roles:

        1. abstract Component (Component) role : This is an abstract role, which, in the transparent combination mode, defines the common interface and default behavior for the participating objects, which can be used to manage all the sub-objects. In the safe combination mode, the component role does not define the method of managing the sub-object, which is given by the branch structure object.
        2. leaf component (leaf) role : A leaf object when there are no subordinate child objects, define the behavior of the original object that participates in the composition.
        3. Branch Component (Composite) Role : Represents an object that participates in a group of subordinate sub-objects, and the tree object gives a method implementation of all the managed sub-objects.

10.6. Advantages and Disadvantages:

10.6.1, Advantages:

          1. The combined pattern enables client code to handle objects and object containers consistently, without the need for a single object for relational processing, or as a combined object container.
          2. Decouple the customer code from the complex object container structure.
          3. New artifacts can be added to the composition object more easily.

10.6.2, Disadvantages:

          1. Make the design more complex. The client needs to spend more time clearing the hierarchy between classes.

C # Design Pattern--Summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.