Preface: Design patterns I believe many experts have already written Article Now, I am not going to write it in detail. I am writing an article to summarize my own learning and hope that I can share it with experienced talents.
Prerequisites:I personally think that learning the design model requires a certain degree of foundation and experience, a good oo idea, and a certain degree of experience.ProgramDevelopment Experience (that is, there are many projects ). I am a good example. I just learned Asp.net (C #) for more than two years, so I rushed to see the design model (haha, programmers who have just started out have a very low eye ). The conclusion is that "Design Model" = "software structure ~ Later, I thought I knew the design pattern, and sometimes I was talking to others. However, as I learned a lot and gained more experience, I began to feel a little confused about my understanding. After three years of mixing, I decided to re-view the design model, which is quite different from the original cognition. I am ashamed of my previous remarks ~ I hope I have not misled new users before...
MATERIALS:I am talking about the gof model. Of course, I want to look at the classic book, design patterns-the basis for reusable object-oriented software. However, this book must be well understood by a certain number of people. For Beginners, we recommend you read some easy-to-understand materials. When you get to understand it, watching this classic will benefit a lot.
Body:After a bunch of nonsense, I finally started the text.
I have summarized three terms, which basically run through the entire design model:Change, coupling, dependency. The following concepts are cited from these three words:The change point is there, and we encapsulate it (or the change point is there, and the design pattern is used there ); one important purpose of the emergence of design patterns is to achieve a loosely coupled Software Architecture (in fact, our world is a great loosely coupled architecture); to flexibly mix design patterns, be sure to understand the dependencies between objects and modules and handle these dependencies.Remember:CodeIt is just an expression of the design mode. The design mode itself is not a system architecture, but a system architecture design concept.Remember:The design pattern should not be set in disorder. The design pattern is like wearing a big sweater in hot days. Do not use the design pattern if you do not need it. There are two situations where the design model is not used: the entire system remains unchanged, and the entire system is constantly changing.People who want to learn the design model will be close to each other:It must be programmed based on interfaces.
I am looking at the gof design patterns. There are 23 gof object-oriented design patterns in total, including the three major blocks: creation, structure, and behavior. I will first look at the creation model.
The creation mode is for the New Keyword. The creation mode includes five in total: Singleton single piece, Abstarct factory Abstract Factory, Builder, factory method, prototype
Singleton:This is the simplest and most commonly used. To put it bluntly, an object is unique in the system. It is loaded into a memory space during system compilation or the first access, then, any user only uses this object in the memory space. The expression in the Code is also very simple. You only need to make sure that only one variable or object is created from the system running to the end. For example, some string variables, usually I will add the static and readonly modifiers (readonly is optional, to avoid the object reference being re-directed to another memory space ). Even programmers who have never learned the design pattern should write this frequently. There are many places to use, such as writing SQL statements in code, and some objects that store system information.
Abstarct factory Abstract Factory, factory method, prototype:Why are these three modes put together? Because these three models are similar. The same point is that what they want to solve is the same as the direction, both to solve the changes between multiple series. The difference is that the implementation methods are different. These three modes are basically interchangeable, and the factory method is basically used together with the Abstarct factory. I believe many people know that the main structure of Microsoft's latest petshop system is written in the factory mode. If you haven't seen it, you can check it. In petshop, the factory will express the image, but don't be confused about this architecture... just like me, after reading this architecture, Everything is written in factory mode, just like a sb. The change points of these three models are all over the multi-series changes. For example, if an automobile engine is divided into many series, such as V6 and V7, we only need to do a good job of interface of this engine, then, use the factory to select the engine that inherits the interface. The external car parts only need to know the interface and parameters of the engine. As for the specific engine, let's do it with the factory of the engine. As for prototype in this example, a replica provider with a simulation capability of up to 100% has obtained a class that inherits the interfaces of this engine, you only need to copy from the prototype continuously. Ask msdn about how to copy objects in depth.
Builder:This mode is similar to the above three modes. The common point is to solve the changes between multiple series, but the difference is that there is a complex relationship between these multi-series objects. Or we can understand it from the word "Builder". For example, there is a website that is hosted, modern, and classic, these series are composed of many different CSS, such as CSS defining the background color. Each style is defined by a different CSS. At this time, we create a generator, it is used to combine these different CSS to make them various types of styles. Modern models have modern generators. Ancient models have typical generators. Then create another Generator interface. The application calls the interface of the generator to achieve the effect of the generator. Of course, there must be classes that manage to call that generator.
To sum up, the creation mode is mainly used for changes in multiple series (ignored by Singleton). In our actual application development, it seems that there are few such applications, it seems that it can only be used in multi-database development. However, if you fully understand the mode, you will find that all parts need to be used... for example, employees who calculate their salaries have multiple levels of employees, and the calculation methods for each level are different. This is multi-series, and different wage calculation methods are the change points. We need to use the design mode above the change points, the relationship between the customer program or application program and the wage calculation module is loosely coupled, while the application program only depends on the interface of the wage calculation program.
PS: Be sure to read this book: design patterns-the basis for reusable object-oriented software. If you have a good English level, you 'd better read the English version. I am a cainiao like E. I read it together with Chinese in order to learn E.
I hope this summative Article can help people who have just read the design model. If anyone finds that I have made a mistake, please let me know immediately. Thank you very much.
Any questions can be sent to this mailbox: ou.jasmine@gmail.com I will try to make sure there is a must