(Original) No nonsense C # One of the design patterns: the beginning

Source: Internet
Author: User

No nonsense C # One of the design patterns: the beginning

 

What is the design model?

 

What is Shaolin boxing? Shaolin Boxing is a set of martial arts routines that Shaolin monks have drawn after a long summary. There is a secret of Martial Arts called Shaolin Boxing, which records the applicable groups, style routines, and after-learning effects of this boxing method. Although the design pattern is recorded in the design pattern book, it is not enough to grasp the design pattern simply by looking at the structure of each pattern and imitating it. Imagine who will be playing with you in accordance with the Shaolin Boxing method in 1234 in actual practice? It can only be used to look at routines. You can only learn this method when you can make a flexible offer based on different scenarios. Similar examples include the plan, which is also a model. Each strategy is for different scenarios ", how can we fight this battle?

In short, the design model must be active to play a role.

 

What is the purpose of the design model?

 

The design pattern prevents you from getting confused when your needs change. The design pattern can make your program more maintainable and scalable. The design pattern can improve the program performance. Of course, the premise is that the design pattern is used correctly. If it is abused, the design pattern can make the program understandable, slow the program to death, and make the program unable to be maintained, adding a new function is equal to redo.

 

What are the principles of design patterns?

 

L single responsibility: you do not want to change the CPU because the computer memory is damaged, nor should you give a class a variety of reasons for modification.

L open to expansion and closed to modification: you do not want the computer to have only one memory slot. You need to change the motherboard when adding the memory, the program should be able to expand its functions without modifying the original program.

L Lishi replacement: If the DX9 video card you bought does not support the DX9 feature, this video card cannot be used. If the method of the parent class is not implemented in the subclass, it will be dizzy. In the world of programs, never think that birds will fly. First, let's think about which birds will be there.

L Dependency inversion: programming the interface, so that no external code needs to be modified even if the implementation changes. In fact, the computer hardware, network communication and so on are in line with this principle, such as USB interface, PCI-E interface, TCP/IP protocol.

L Interface isolation: Do you need to spend 3000 to buy a mobile phone with a photo or MP3 function, or 1000 to buy a mobile phone, 1000 to buy an MP3, and 1000 to buy a digital camera? If you buy the former, you will have to repair your mobile phone. It is not necessarily because you cannot make a phone call. If you buy the following three types of phone calls, the repair will not affect other services. Which one do you want to buy?

I remember seeing an example that it is much easier to repair a computer than to repair a radio. If the computer breaks down, replace a part. The reason is that all parts in the computer are based on relatively stable interfaces, and the parts perform their respective duties without affecting each other. The computer itself is a product that conforms to the design principles. It's not that easy to repair a radio. There are no plug-ins. People who need to repair the radio must understand the principles of each of these components.

A small program is like a radio. It can be done by only one person, and it won't take much time even if it is done again. If a large project of dozens of people needs to be modified by everyone, the project will not take much time because of the high maintenance cost, and the BUG will be scrapped after maintenance.

 

How to Learn the design model?

 

What is the basis for learning new concepts? First, you must know 26 letters. If you have no idea about object-oriented, we recommend that you take a look at some Object-Oriented Knowledge first. After all, the design pattern is a summary of the object-oriented programming pattern. After learning 26 letters, you can learn new concepts. However, it is best to first learn the International Phonetic Alphabet for better learning. For the learning of design patterns, you can learn some UML knowledge. Of course, you can also learn the design mode without knowing the UML. During the learning process, you will learn the UML.

The design model is not very advanced. With this knowledge, learn it boldly. Many people say that they have read a lot of articles about design patterns. Why can't they understand them? I think there may be two reasons. The first is that you didn't take the time to read it carefully. The second is that the articles you read are not suitable as the starting point. No matter what you learn, the starting point is very important. If the starting point is not so approachable, you may be rejected. It is most suitable for beginners to switch from an instance. It is best to meet your own project instance as the starting point, so that you can know why the design mode is better.

If you want to level the learning realm of the design model, I will:

L The first article: can understand the design model

L second: be able to write a design pattern skeleton by yourself

L Third: Be able to compile a new example using the design pattern.

L Fourth: it seems that there is a proper design pattern when writing code. After reading the documents, I found this design pattern.

L Fifth: After understanding the needs of the project, you can realize where to use the design pattern for optimization.

L Sixth: fully grasp the essence of the design model, flexible use of various design models and their variants

In any case, taking multiple replicas as is the way to learn. Ten Examples of others cannot be used as an example, and none of the ten passive principles can be understood as a principle. Although each design mode has a skeleton, you do not need to emphasize this form too much. Many times, you can simplify it, change it, or mix some other design patterns as needed, as long as the goal can be achieved, it is also a good choice.

Many people think that few of these design patterns can be used. I don't think this is a problem. If you don't use it, you won't be able to use it. These design patterns are the essence of the Masters' experience in numerous large projects, it would be nice to use two or three of them in a small project you have created. The use of the first 20 or 30 projects is definitely a freak. You cannot use it. Otherwise, it is not conducive to the maintainability of the project and increases the workload.

Many may think that many of these design patterns are similar. In addition, everyone feels different. Some people think that A and B are very similar, but some people think that A and B are very different, but B and C are very similar. It is very easy to differentiate. It means that you are looking at the focus of the design model. It also means that what you see is still its shape. Although the twins are in the same shape, they must be different. As long as they identify the problems solved by the design model, they will not be mistaken.

 

About this series of articles

 

These contents were originally used for weekly knowledge sharing activities within the company. Since there are some content, I 'd like to sort it out and paste it out. For this reason, some examples in this article are based on projects that can be understood by internal team members. These projects may be unfamiliar to everyone, but the advantage is that the examples are relatively close to the actual ones. There are about 20 articles in this series. Apart from introducing some of the common parts of the 23 GOF design patterns (some design patterns have been implemented more easily in C, some design patterns are not very commonly used.) Some other useful design patterns may also be introduced. In these articles, I will not talk too much about theoretical things, nor have a structure diagram (these contents are everywhere on the internet). All the content is centered around relative practical examples. I think that only in this way can we absorb the god of design patterns more quickly, not its shape. When reading this article, we recommend that you read it in conjunction with the book "design patterns" and other design patterns related articles in the blog Park, so that you can fully understand the design patterns.

Each article has the following parts:

L intention: copy the design model book, because the intention is too important, so you have to list it first.

L scenario: A Practical scenario is used to explain why the design model should be introduced.

L sample code: Describe the scenario after the design mode is introduced.

L Code Description: describes several roles in the design mode and the precautions in the code.

L when to adopt: This mode is explained from the code and application perspectives.

L Key Points of implementation: What are necessary to implement this mode, or what are the main features of the mode.

L note: Advantages and disadvantages of the mode and when the design mode should not be used.

[Note] due to the publication cycle of this series of articles, the content is short and incomplete, and new articles published will not appear on the homepage. If you are interested, please follow the BLOG.

To view all the articles, please click (original) No nonsense C # design mode series articles

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.