Design Patterns: first

Source: Internet
Author: User
Design pattern, I think it's already a commonplace for friends in the garden. Especially those works in the garden, such as Bruce Zhang, terrylee, and Lu Zhenyu, must have been read by everyone. I am also a little benefited.
Even so, I would like to share my views and opinions on the design model here. I will give a summary of my own learning about the design model, and I also hope that my friends in the garden can correct the mistakes, it also reduces your detours. It would be better to help some people.
For the definition of the design pattern, every book has its writing method, and everyone has his own opinions, here, I reference the phrase "design pattern describes a general solution to a type of common problems in the software design process. ", Indeed, each design pattern has a specific problem to be solved, and changes in requirements lead to changes in the solution, so the pattern we will use to solve the problem will change accordingly. That is to say, "change" is the premise of using the design model. If the software design does not change once the code is complete, then the design model does not need to be discussed here, there is no need for its existence. Many people may think when they are learning the design model. Since the design model solves some common problems, we only need to use each design mode. Code If you are familiar with it, you can use it. I thought about it like this, which reminds me of an article. Article He told the interviewer that he had developed a software. In this software, he used 23 design models mentioned in gof, as a result, he was not hired. I think if a software needs to use all 23 design patterns, you should think about whether the design transition has passed (maybe some large software does need it, I don't know about this (:). During software development, the demand changes are complex and unpredictable. If we are familiar with the usage of these design patterns, we may also copy them to the project, but once the demand changes, it cannot be addressed. This is because software development is even more unpredictable. Currently, there is no pattern to cope with any changes, or we can say that the current 23 design patterns cannot satisfy these changes, in this case, only the fundamental points of these design patterns can be fully understood. This requires the accumulation of experience.
The basis of the design pattern is object-oriented, and there is no way to talk about the design pattern without the object-oriented pattern. Therefore, before learning the design model, I think the first thing to understand is the three major object-oriented mechanisms, namely "closed", "inheritance", and "polymorphism ". Only by understanding this can we better understand the essence of the design model. Of course, after understanding the design model, we can better use the object-oriented principle.
Of course, there are some design principles that can be followed by the design model. The following is a copy of the book:

1. Single responsibility principle.
We need to segment functions as much as possible. Each class should be responsible for only one piece of content or only one task. So how can we achieve a single responsibility? That is, when a class has only one reason for its change.

2. Open and closed principles
We should do this. Try not to modify the original class, but extend the existing functions.

3. Replacement Principle
Subclass must be able to replace their base classes.

4. Dependency inversion principle
This is only due to the classic saying that the high-level module should not depend on the lower-level module, and both should depend on abstraction. abstraction should not depend on implementation details, implementation Details should depend on abstraction.

5. Interface isolation principles
Each interface must be clearly defined and should not be forced to customers.ProgramDepending on the methods they do not need.

It can be said that the above five points are principles that can be followed in the design. When our program implements these principles, we have already used the design pattern. After hearing the design patterns of instructor Li Jianzhong's series of courses, I personally think that the most classic and general usage of the design patterns is "encapsulate where there is a change ". Of course, we must first know where the changes are. This requires us to continue to practice and comprehend.

I have mentioned this in the first article. I feel like it's all a general discussion. Haha, I have limited experience. Sorry. In the following sections, I will use a practical example to describe several design patterns I understand.

Note: This article is based on lectures by Li Jianzhong and some articles about design patterns, such as Bruce Zhang, terrylee, and Lu Zhenyu. The examples used in the following articles are also taken from terrylee's article 《. net Design Patterns (3): Virtual cases in abstract factory, because I think this case can be well expressed for my understanding, I don't have to bother thinking about a case. I hope terrylee won't mind if I can better understand my friends in the garden. (Of course, I will mark the copyright of others)

Next article: design patterns: factory Method)

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.