In one sentence, head first design mode

Source: Internet
Author: User

The head first design model is an interesting book about design patterns.

Reading books at school is different from reading books at work. At school, we read books for reading. We can go from 0 to 1. We have time. But it is different after work.

I think the book at this time is more like the secret of the martial arts that connects the two sides of my supervision. In my daily work, I learned some things, but they cannot be integrated. Therefore, you need to clarify your ideas through books. This is the starting point for writing this article, and it is also my fragment!

After reading the book, it may be wrong to convert it into your own language and then express it again (that is, it must be). However, it is always better to have it. If some of you can get some inspiration from it, you are lucky!

I tried to describe a design pattern in one sentence!

  1. Rule mode!

    The unified things are used as the base class. The variable things act independently. The variables are referenced in the base class and the setter method is used to change the delegate variables, make it behavior changeable. API-oriented programming, rather than class-oriented programming. The advantage is that using the same interface, passing in different behavior classes produces different effects, making it easy to change the behavior.

  2. Observer mode!

    Similar to the publish-subscribe mode. The registration service and notification process exist. The implementation method can be understood as: when registering a service, the observer is added to the queue. When the topic changes, the topic actively calls it from the observer queue in sequence, in this way, notifications are actively pushed. The advantage is that the observer registers and observes at any time and can receive messages in real time, and the observer knows nothing about the message, so that the notification can be parsed at the same time.

  3. Decoration mode!

    Take an object as the main object. After it is generated, it is passed into the constructor. After it is decorated, it is then output in the row mode. This mode can be assembled independently by many scattered methods without affecting other changes. This mode is implemented through inheritance. The typical decorator mode is used to inherit Java I/O classes. There are multiple main classes and decoration classes, so that users can conveniently operate on the desired methods. The advantage is that the main behavior results can be repeatedly changed without changing the general direction. For some additional class changes, the data can be easily re-processed through this mode.

  4. Factory mode!

    It can be divided into simple factory mode-Abstract Factory mode. The so-called factory model is to combine the required products with the factory to get a specific process of the product, without knowing who the product is produced. The factory model reuse the variability of Multiple Products, avoiding class disorganized problems caused by instantiation of each class. Or, from another point of view, the factory just removes some code with high reusability. The advantage is that the generation of some classes is controlled in a unified manner, so that various classes are not scattered in every corner of the Code, which facilitates the upgrade and maintenance in the future.

  5. Singleton mode!

    That is, there is only one instance object access method (single process mode) globally ). A private constructor is required to make the external instance unable to be instantiated. A static method getinstance is required for external access instances, which is a lazy loading behavior. However, you should pay attention to the multi-thread concurrency problem, so as to create two instances, use the synchronized synchronization method or volatile to synchronize the instance, so as to solve the concurrency problem, but this will cause the application performance to be reduced by 100 times. When you use a large number of Singleton instances in an application, you have to consider whether it is appropriate, because there are not many scenarios suitable for Singleton. The advantage is that it reduces the performance overhead and space overhead when the system repeatedly creates a class, and can share some variables in multiple places (if needed ).

  6. Command mode!

    The request is passed as an object to another object to implement the command execution method. By separating requests from execution, you can easily perform command set operations, macro operations, and playback. Log queue operations can be easily implemented. The advantage is that the command request is separated from the command execution. Generally, the request will be completed quickly, but the execution is not necessarily. Because the request and the execution are separated, the post-event compensation can be easily implemented.

  7. Adapter mode!

    That is to say, the adaptation of the interface to the B interface is realized. For example, the connection between the triangle socket and the two-pin connector is realized. The adapter does not need to change or cannot change the existing interface at the same time, it is of great significance to connect new interfaces to the existing environment. The implementation is that the adapter inherits the Target Interface and passes in the adaptation interface to convert the logic of the adaptation interface into the expression of the Target Interface. You can inherit multiple interfaces for Bidirectional conversion. The advantage is that the new manufacturer's things are connected without modifying the existing code to adapt to the original method.

  8. Appearance mode!

    The intention of the appearance mode is to provide simplified interface operations without changing the original interface. Its implementation is a simplified type of packaging. It follows a design principle and only talks with the most intimate people. The advantage is that the original complex and variable interfaces are converted into a few fewer and practical interfaces, so that only the simplest thing is to do when calling the external interface.

  9. Template Method mode!

    Define an algorithm skeleton or operation flow in a parent class interface, and expose a method unique to a subclass as an abstract method, this allows you to use the operation flow of the parent class to call the unique method of the subclass at runtime. This mode saves a lot of mechanical code, so that subclass only pays attention to its own unique part. In this mode, there is also an ordinary and important concept, hook, hook in Java shows that a method with only null or default implementation, as long as the subclass overwrites the method, it can trigger the hook to implement switch control and its own intent. Because it is a high-level invocation of the lower layer, some operations are not obvious. If the lower layer calls the higher layer, it will be difficult to clarify the design. Therefore, we should follow the principle of Hollywood, only the parent class calls child classes, and the Child class cannot call the parent class. Therefore, if you want to know why you must implement a method in the framework, you only need to check the parent class for its call, however, you cannot call the parent method without authorization. The principle of dependency inversion is somewhat like this. The advantage is that the complex process is encapsulated and only a variable method is provided for the subclass to overwrite, so that a lot of repeated code is reduced by calling the parent class.

  10. Iterator mode!

    That is, to implement functions like the iterator interface, so that objects can traverse elements without paying attention to internal implementations. At present, it should be meaningless for us, because the syntax similar to for in has completely achieved this effect, and this mode does not make much sense. The advantage is that the internal structure can be traversed for ease of use.

  11. Combination Mode!

    The method that combines multiple interfaces into a larger interface, so that the operator can call the same interface without paying attention to the differences, however, if some sub-classes do not have a method, an exception must be thrown for external capture. You have to think carefully about the application scenarios. This function is the most obvious in Gui programming. When you drag components provided by several systems and combine a new structure on the page, the combination mode is used.

  12. Status mode!

    It is similar to the policy mode. Its role is to control the internal status flow of external operations, and it does not need to be known to the outside. Its operation is actually to free up a series of if else, so that the logic is clearer. It is implemented to close all states of the entire process. After a user operates on a certain State, this state only performs his/her own tasks and converts the state to the next state, when the user performs the next operation, the internal operation has changed, but for the class, the operation is still in a single State, so the logic is clear. However, this mode produces a large number of status classes, adds a large amount of code, and abstracts good States to test the technical level. Its role is to make the logic clearer and easier to expand.

  13. Proxy mode!

    That is, the access object is not directly accessed, but accessed by the proxy so that the proxy can communicate with the specific object. After the communication is completed, the result is returned to the visitor. Rmi remote calls are generally involved here, the proxy mode reduces the complexity of the system (at least to the caller ). Virtual proxy, cache proxy, synchronization proxy, firewall proxy, copy proxy when writing. The proxy mode is widely used in reality. It shields us from many complicated details. The proxies provided by the framework make us easy to operate and make us dumb.

  14. Compound mode!

    That is to say, the combination uses multiple modes. This mode is not exactly a mode, but also the most embodied mode in the framework, such as the famous MVC. Combination Strategy, adapter, observer, decorator, combination mode, factory, etc. The advantage is that, on the basis of the basic mode, another practical mode is encapsulated to solve more specific Application Scenario problems.

16. More!

Mode is a solution to a certain problem in a certain situation. Other modes, visitor mode, intermediary mode, prototype mode, bridge mode, and responsibility chain mode ,. Anti-pattern, that is, the poor pattern, looks good on the surface, and there will be a big pitfall when it is actually used.

 

Source: Wait for you to return to the link: http://www.cnblogs.com/yougewe/p/8240977.html

For more dry goods, you can pay attention to the public number and then reply to "dry goods" to receive a large number of dry goods for free.

In one sentence, head first design mode

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.