NET design Pattern Instance Series article summary

Source: Internet
Author: User

1 What is design mode

The design pattern is a better solution to the recurring problems in the process of software design. As one expert says: Design patterns are a reproducible solution to the design problems often encountered by program designers (the Smalltalk Companion). Gof design patterns are often considered the basis of other design patterns, and as business complexity increases, new design patterns are emerging, and these new design patterns generally refer to the GOF model theory.

2 Why to learn design patterns

Consider it from a personal career plan. A software development engineer with the increase in the number of coding, development experience, the deepening of software theory understanding, will involuntarily think of some methods or shortcuts to improve their productivity, rather than face the repetitive problems do the same work, when you have this idea, you need to start learning design patterns, Design patterns will give you a better solution, not only solve the problem but also improve your ability, but also to the software designers and architects over the stage.

Consider it from a software architecture perspective. The rapid development of the economy makes the business more and more complex, so how to adapt the software to this complex business change, in software design and architecture, the appropriate use of design patterns can solve this problem, but also should be careful not to overuse design patterns, otherwise it will make the system more complex. Design patterns are also the basis and tools for code refactoring, and it is recommended that the code be refactored into design patterns as much as possible.

3 Design Pattern Principles

The fundamental reason for using design patterns is to apply changes, improve code reuse, and make software more maintainable and extensible. The following principles need to be followed: Single primary duty, open closure (open Closed Principal), dependency inversion principle, Richter substitution principle.

3.1Single principle of responsibility

As far as a class is concerned, there should be only one cause for his change. If a class takes on too much responsibility, it is tantamount to coupling these responsibilities, and a change in one's responsibilities may weaken or inhibit the ability of the class to perform other duties. This coupling can lead to fragile designs that can be subjected to unexpected breaks when changes occur.

3.2Open Closure principle

Software entities (classes, modules, functions, and so on) should be extensible, but not modifiable. In other words, the extension is open and the modification is closed. In general, in the face of demand, changes to the program are made by adding new code, rather than changing existing code.

3.3Dependency Inversion principle

Abstract should not be the details, the details should rely on abstraction, that is, the "face of interface programming, rather than the face of implementation programming." It can also be understood that high-level modules should not rely on the underlying modules, two should be abstract; abstractions should not depend on details, and details should be dependent on abstraction.

3.4Principle of substitution on the Richter scale

Subclasses must be able to replace their parent type. In other words, in the software development process, the subclass replaces the parent class, the function behavior of the program does not change. The parent class can be reused only if the subclass is able to replace the parent class, and the child class can also add new behavior based on the parent class, if the functionality of the Software Unit is not affected.

4 Design Patterns four basic elements

Design patterns make it easier and easier to reuse successful designs and architectures, and presentation of proven technologies into design patterns will make it easier for newly added system developers to understand their design ideas. The basic elements of design patterns include schema names, problems, solutions, and effects.

4.1Schema name

A mnemonic name used to describe design patterns, solutions, and effects. Design patterns allow for design at a higher level of abstraction. Based on a pattern glossary, the development team can discuss patterns and use them when writing documents. The name of the pattern helps us to think and facilitate communication between team members about design ideas and design results. Finding the right pattern name is also one of the difficult points in the design pattern cataloging work.

 4.2Problem

The problem mainly describes when to use design patterns. It explains the design problems and the causes and consequences of problems, specific design problems and how to use object representation algorithms. Typically, a sequence of prerequisites that a pattern must meet is a problem.

 4.3Solution Solutions

The solution describes the composition of the design, the interrelationships between them, and their respective responsibilities and ways of collaborating. Because the pattern is like a template that can be applied to many different situations, the solution does not describe a specific design or implementation, but rather provides an abstract description of the design problem and how to solve the problem with a general-meaning combination of elements (class or object combination).

4.4Effect

Describes the effects of pattern applications and the tradeoffs between usage patterns. While describing design decisions does not always refer to pattern effects, they are important for evaluating the cost and advantage of design choices and understanding usage patterns. Most software effects focus on measuring time and space, and they also express language and implementation issues. Because reuse is one of the elements of object-oriented design, pattern effects include its impact on system flexibility, extensibility, or portability, and explicitly listing these effects is useful for understanding and evaluating these patterns.

5 Design Pattern Classification

The design pattern is mainly divided into the creation mode (Creational Patterns), the structural mode (Structural Patterns), the behavioral mode (behavioral Patterns).

5.1Creation Mode (creational Patterns) 5.1.1NET design pattern instances Simple Factory mode (Factory pattern)

The advantage of simple Factory pattern is that the factory class contains the necessary logical judgments, dynamically instantiating related classes based on the client's selection criteria, and for the client, to remove the reliance on the specific product

5.1.2 Net design pattern Instances Abstract Factory mode (abstract Factory pattern)

Abstract Factory pattern, which provides an interface to create a series of related or interdependent objects without having to develop their specific classes. A typical application of the abstract factory pattern is the use of abstract Factory + reflection + configuration files to implement the data Access Layer program

5.1.3 Net Design Pattern Example Singleton pattern (Singleton pattern)

Singleton mode (Singleton pattern), which guarantees that a class has only one instance, and provides a global access point to access it. Singleton mode because Singleton encapsulates its only instance, it can strictly control how a customer accesses it and when to access it.

Builder pattern for 5.1.4 net design pattern instances

Builder pattern, which separates the construction of a complex object from its representation, so that the same build process can create different representations. The builder pattern is when the algorithm that creates the complex object should be independent of the part of the object and the mode in which they are assembled

Prototype pattern of 5.1.5 net design pattern instances (Prototype pattern)

Prototype mode (Prototype pattern): Specifies the kind of object created with the prototype instance and creates a new object by copying the prototypes. Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.

Shallow copy differs from deep copy:

Shallow copy, all variables that are copied have the same value as the original object, and all references to other objects still point to the original object. Deep copy, the reference object's variable points to the copied new object, not the original referenced object.

NET namespace system provides a iconeable interface with only one method clone (), which allows the prototype mode (Prototype pattern) to be implemented only by implementing this interface.

5.2Structural mode (Structural Patterns) 5.2.1 NET design mode instance Adapter mode (Adapter pattern)

Adapter mode to replace a class with another interface that the customer expects. The adapter mode unifies the interfaces of incompatible objects so that those classes that would otherwise not work because of incompatible interfaces can work together.

Bridging mode for 5.2.2 Net design pattern instances (bridge pattern)

Bridging mode (bridge pattern) separates the abstraction from its implementation, allowing the abstraction and implementation to vary independently. Decouple an abstraction from it implementation so and the can vary independently.

What is aggregation / Combination:

Aggregation (Aggregation), when object A is added to object B and becomes part of Object B, an aggregation relationship is between object B and object A. Aggregation is one kind of correlation relation, it is strong association relation, emphasizing the relation between whole and part.

5.2.3Net Design Pattern Example combination mode (Composite pattern)

Combined mode, which combines objects into a tree structure to represent a "partial-whole" hierarchy, combining patterns makes the user consistent with the use of individual objects and composite objects. The solution of the integration with the parts can be treated uniformly.

5.2.4 Net design Pattern instance Decorator mode (Decorator pattern)

Decorative mode, which adds additional responsibilities to an object dynamically, and these responsibilities require the user to decide how and when to join. Decoration mode provides a "Plug and play" way to decide when to add functionality during run time. For added functionality, the adornment mode is more flexible than generating subclasses.

5.2.5 Net design Pattern instance appearance pattern (Façade pattern)

The appearance mode, which provides a unified interface for a group of subsystems, defines a high-level interface that makes the subsystems easier to use. The appearance mode can solve the layer structure separation, reduce the system coupling degree and provide the interface function for the old and new system interaction.

5.2.6 Net design pattern instances of the Flyweight pattern

Flyweight pattern, which uses shared technology to effectively support a large number of fine-grained objects. Use sharing to support large numbers of fine-grained objects efficiently. The enjoy meta-mode avoids the overhead of a lot of very similar classes. In programming, it is sometimes necessary to generate a large number of fine-grained class instances to represent the data. If these instances are found to be identical except for a few parameters, the number of classes that need to be instantiated can sometimes be affected by a substantial reduction. If you can move these parameters out of the class instance and pass them in as a method call, you can drastically reduce the number of individual instances by sharing.

The internal state and external State of the enjoy meta object:

The shared part of the internal state, which is inside the element of the object and does not change with the environment.

An external state that changes as the environment changes and cannot be shared.

Proxy pattern for 5.2.7 net design pattern instances

The proxy pattern provides a proxy for other objects to control access to the object.

5.3Behavioral patterns (Behavioral Patterns) NET design pattern instance responsibility chain mode (Chain of Responsibility)

Post-Supplement This section

Command pattern for 5.3.1Net design pattern instances

The command pattern encapsulates the request as an object, allowing you to parameterize the client with different requests, queue or record the request log, and support revocable operations. When you need to have undo or redo operations, consider using command mode.

5.3.2Net design Pattern Instance interpreter mode (interpreter pattern)

The interpreter pattern (interpreter pattern), given a language, defines a representation of its grammar and defines an interpreter that uses that representation to interpret sentences in the language. When there is a language that needs to be interpreted and executed, and you can represent a sentence in that language as an abstract syntax tree, consider using the interpreter pattern.

5.3.3 Net design Pattern instance iterator pattern (Iterator pattern)

The iterator pattern (Iterator pattern) provides a way to sequentially access an element in an aggregated object without exposing the internal representation of the changed collection object. An iterator pattern is the separation of the traversal behavior of a set pair, and the abstraction of an iterator class to be responsible for not exposing the internal structure of the collection, but also allowing the external code to transparently access the data inside the collection.

5.3.4 NET design pattern instances in the interface mode (mediator pattern)

The Mediator pattern (mediator pattern), which defines a mediation object to encapsulate the interaction between series objects. The mediator makes the objects not need to be referenced to each other, so that they are loosely coupled and can change their interactions independently. The mediator pattern is typically applied to a set of objects where a well-defined but complex method of communication is used, and if you want to customize a behavior that is distributed across multiple classes rather than generating too many subclasses.

5.3.5 NET design Pattern Instance Memo mode (Memento pattern)

Memo Mode (Memento pattern) captures the internal state of an object without destroying the package, and saves the state outside the object. This will allow you to revert to the previously saved state for that object.

When the system function is complex, and the historical attributes need to be recorded to do the recovery action when needed. Originator can be restored to the previous state based on the saved memento information.

Observer pattern for 5.3.6Net design pattern instances (Observer pattern)

The Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time, and the subject object notifies all observer objects when the state changes, enabling them to automatically update themselves. The solution is "when an object changes the need to change other objects at the same time" problem. Finally, the observer pattern is further elaborated with the stock example.

5.3.7Net design mode instance state mode

State pattern, which, when an object's internal state changes, allows it to change its behavior, and the object looks like it has changed its class. A state pattern can be considered when an object's behavior depends on its state, and it must change its behavior according to state at run time.

NET design Pattern instance policy pattern (strategy pattern)

Post-Supplement This section

Template method pattern for 5.3.8Net design pattern instances (templates mothed pattern)

Template method pattern, which defines an algorithm skeleton in an operation, and delays some steps into subclasses. Template methods enable subclasses to redefine some specific steps of an algorithm without altering the structure of an algorithm. The template method pattern moves the invariant behavior to the superclass, thus removing duplicate code from the subclass, in fact the template method pattern is a code reuse platform.

To complete a process or series of steps that are consistent at a level of detail, but the individual steps are not implemented at a more detailed level, you can use the template method pattern to solve the problem.

5.3.9Net design Pattern Instance Visitor mode (Visitor pattern)

The visitor pattern represents an element action that acts on an object structure. It allows you to define new actions that act on these elements without changing the individual element classes. The purpose of the visitor pattern is to separate the processing from the data structure.

6 Summary of Design patterns

NET design Pattern series This article introduces the GOF 23 modes, introduces the design pattern from the pattern design idea, the design frame, the framework code, and each pattern is equipped with the pattern instance, the UML design and the C # code. The design tools used are ea:enterprise Architect. The main reference books are "Head First design mode" "Big Talk Design mode" "NET and design mode" "C # 3.0 design pattern" and some foreign articles of the example, Thanks to the authors of these books, before reading this series, we recommend that you read the summary of theUML Modeling series

At this point, the net design pattern instance series has been written, due to limited personal ability, inevitably some problems are unclear or not very correct, looking for expert guidance. Thanks again.

Reprinted from: http://www.cnblogs.com/ywqu/archive/2010/03/15/1686015.html

NET design Pattern Instance Series article summary

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.