Some opinions and thoughts on design patterns

Source: Internet
Author: User
After reading "designpatternexplained" recently, I plan to summarize it from a technical perspective, but I have no clue at all. Let's talk about your own superficial feelings. The book does not list all the design patterns in 23 and presents the class diagrams and source code. well, this method seems silly to the author. The author said that the goal is to focus on the design pattern to solve the problem itself "onlytellsuswhattodobutnotw

After reading design pattern explained recently, I plan to summarize it from a technical perspective, but I have no clue at all. Let's talk about your own superficial feelings.

The book does not list all the design patterns in 23 and presents the class diagrams and source code. well, this method seems silly to the author. The author said that the goal is to focus on the design pattern to solve the problem itself "only tells us what to do but not when to use and why to do it ". Yes, what we need is the process and method of thinking, rather than the results left by our predecessors.

From this perspective, what should we learn in the design model?

Design patterns are the crystallization of a group of great ideas. Our starting point is to cope with possible and existing needs, and reduce the difficulty of future maintenance. The technical means we use are object-oriented.

Therefore, we need to learn: what is object-oriented? Why is object-oriented? How is object-oriented?

Object-oriented is not an encapsulation inheritance polymorphism. The father of object-oriented tells us that encapsulation inheritance polymorphism is not his biggest idea ---- message passing! However, message passing only shows the surface. message passing reflects a very important point of view-an object shoshould responsible for themselves! In addition, we can see more things.

Why is object-oriented? Object-oriented makes our code easy to write and maintain. Do not think that using namespace or encapsulation is easy to write and maintain. the true ease of writing means that the difficulty of thinking when coding is reduced, when the demand changes, our changes are reduced.

How is object-oriented? In the book, we will consider conceptual level, specification level, and implementation level from three perspectives.

From process-oriented to object-oriented

It is difficult to get rid of the negative principle of things, as is program design. Process-oriented is a natural design concept because it is in line with the way computers handle problems. But he has his inherent shortcomings. In fact, he can regard process-oriented as a highly coupled, loose and cohesive object-oriented design method. Any two objects, or modules, can access each other's members. the division of labor between modules is unclear. the division of labor is based on the task completion sequence. In this way, we will have a headache in solving the problem, and we have to carefully control the module not to access data incorrectly. However, many other modules may be used to complete a module task. when a module is changed, it does not know what negative impact this will bring to other modules, as the author said, "Like a snowball that picks up snow as it rolls downhill, a focus on functions leads to a cascade of changes from which it is difficult to escape."

What are the benefits of high cohesion and low coupling object-oriented? Encapsulation is a very important feature in Object-Oriented Systems. it not only encapsulates data, but also behavior, concepts, implementation details, and so on, as much as possible, the owner is responsible for the object, and the external details that do not need to be concerned are hidden. This seems to be more in line with people's ideas. This book keeps talking about the timeless way of building. just like a building, a house is used for housing. as a user, we don't need to know how to build the house, we don't need to know the skeleton of the House-we only take care of it, and the House only protects us.

Traditional object-oriented and new ideas

The book repeatedly repeats three ideas:

  • Design to interfaces. encapsulate details so that users and users can better responsible for themselves
  • Favor composition over inheritance. prevents class explosion and improves cohesion.
  • Find what varies and encapsulate it. encapsulate changes to respond to possible demand changes.

The rest of the book is about several design patterns, which are applied in detail to these ideas.

Design Mode

I am afraid to repeat this part too much. I have not been able to apply these models to practice. Let's make a rough summary. There may be many misunderstandings. I hope you can correct them for me ~

  • The facde pattern: encapsulates complex interfaces or provides a simple interface to the outside without being understood by the outside.
  • The adaptar pattern: encapsulates different classes so that they have the same external interface for ease of use.
  • The bridge pattern: separation of concepts and implementations.
  • The abstract factory pattern: the controller is responsible for selecting object types, and the factory is responsible for producing corresponding objects. Hide the details of object merging.
  • The strategy pattern: this pattern implements two principles: "merging reuse" and "encapsulation change ". Encapsulate possible changes, combine the objects that encapsulate the changes in the original object, and control the policy selection by using a config object.
  • The decorator pattern: the essence of this pattern is the decorative class function, which implements new functions based on the implementation of the original class.
  • The singleton pattern: ensure that only one instance exists.
  • The observer pattern: to put it bluntly, a group of objects stare at the message queue and listen to the messages they can process to make corresponding actions.
  • The factory pattern: the factory is abstract and defines only what functions the subclass needs to implement and how to implement them depends on the subclass.

Let's talk about this.

This article is available at http://www.nowamagic.net/librarys/veda/detail/89.

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.