Summary and application of several more important design patterns

Source: Internet
Author: User

1. The design pattern is divided into the creation type, the structure type, the behavior type.
2. Create a 2.1 factory method. Produce different products of the same interface. such as: Spring AOP in Aopproxyfactory (specifically implemented class Defaultaopproxyfactory) Production Aopproxy interface under the two products Cglib and jdkproxy. such as: Ibatis in Datasourcefactory (three factories to implement the interface) production interface for DataSource dbcp,jndi,simple three different data sources.
2.2 Single-case mode. One of the factories, but always only provides the same object, i.e. an object is only new once. A Hungry man (direct new, non-lazy loading). "Recommended," effective Java 71 is also recommended to use lazy loading lazy (lazy loading), there is a synchronization problem. You can add synchronized or use the volatile keyword. Double-check is also problematic, and the final recommended version of advanced is the private static inner class implementation.
3. Structural type 3.1 decorator mode. Each adornment class contains a reference to the parent class (parent interface), which can then be used to add new functionality to the newly implemented method, based on the reference. such as: Nested new objects in IO. New DataInputStream (New FileInputStream (""))
3.2 Adapter mode. The implementation class of the source interface is adapted to the implementation class of the target interface. The purpose is to connect two different interfaces of a system. such as: Springmvc in handleradapter such as: Spring AOP advisoradapter different advisors (front, back, around, etc.) to fit into the interceptor Methodinterceptor
3.3 Proxy mode. such as: Jdkproxy
4. Behavioral 4.1 policy mode. It is mainly for a set of algorithms that encapsulate each algorithm in a separate class with a common interface, so that they can be replaced with each other.
4.2 Observer pattern. The Observer (subject) is observed by n observers and changes by the observer, which causes all observers to change. Mainly used in life cycle control. For example, Tomcat's lifecycle and Lifecyclelistenerlifecyclelistener represent abstract observers, which define a lifecycleevent method, which is the method to be executed when the subject changes. And lifecycle is the subject, such as the specific theme Standardserver. But the overall management here is handed over to Lifecyclesupport. It can trigger the Start,stop function of the lifecycle itself to start off the sub-container (regardless of the observer mode), or it can trigger the Lifecyclelistener event by changing the lifecycle. For example, Spring's Applicationlistener and applicationevent implements the bean for listener interface, which triggers the event handling mechanism of these beans when the container fires events. If the container triggers a start-up event, it also allows some beans to respond to the initiating action accordingly.
4.3 Template method. The parent class defines the skeleton method, and the skeleton method recommendation is defined as the final type, not overridden by the quilt class. At the same time, the skeleton method usually calls several abstract methods, these methods are left to the subclass to implement. For example: HttpServlet service method.

Summary and application of several more important design patterns

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.