[Design mode] application scenarios of Design Mode

Source: Internet
Author: User

 

Interface Mode  
Adapt a class interface to a customer's expected Interface Adapter Mode)
Provides a simple interface for a set of Classes Facade)
Defines an interface that can be applied to a single object or an object group. Composite)
Extract an abstract implementation to separate it so that it can change independently of each other. Bridge)
Responsibility Model  
Concentrate duties on a single instance of a class Singleton)
When an object changes, other objects dependent on this object can be notified, and this changed object does not need to know which objects are used. Observer mode (observer)
This class can monitor the interaction of other objects. Mediator)
Make an object a service of another object Proxy)
Allows a request to send an object chain to the upper-level until the request is processed by an object. Chain of responsibility)
Responsibilities of shared fine-grained objects Flyweight)
Constructor Mode  
Before constructing an object, we need to gradually collect information related to the structure. Builder Mode)
If you need to determine which class to initialize, then the confirmation process will be postponed until a suitable time is reached. Factory method)
Construct a family of objects. They need to share certain features. Abstract Factory)
Create an object through a given instance Prototype)
Reconstructs an object based on an object in sleep state. This object in sleep state only contains the internal state of the object. Memento)
Operation Mode  
Implement an algorithm in a method, but postpone the definition of some calculation steps in the algorithm so that the subclass can redefine these calculation steps. Template Method)
Assign an operation so that each class can express a different state. State)
Encapsulate an operation so that the implementation of the operation can be replaced with each other. Strategy)
Encapsulate a method call in an object Command)
Assign an operation so that each implementation can be applied to a different type of synthesis. Interpreter mode (Interpreter)
Extended Mode  
Enables developers to dynamically generate the behavior of an object. Decorator)
Provides a means to access elements in a set in sequence. Iterator)
Developers can define a new operation for a hierarchy without changing the classes in the hierarchy. Visitor mode (visitor)

 

 

 

 

Http://blog.csdn.net/rainylin/article/details/1721302

 

  1. Singleton Mode

It makes no practical sense to allow the creation of each class freely, and may cause system performance degradation.

Advantage: reduces the system overhead caused by creating Java instances.

This allows the system to track the lifecycle and status of an instance.

2Factory Model:

Factory models are divided into simple factory models and abstract factory models.

The advantage of the simple factory mode is that the object callers are separated from the object creation process. When the object callers need the object, they can directly request it from the factory. This avoids the coupling of the caller of the object and the implementation class of the object in hard coding mode to improve the maintainability and scalability of the system. The factory model also has a small defect: when the product is modified, the factory class also needs to be modified accordingly.

Actual use case: the Spring IoC container should be confused: is it a simple factory? Or abstract factory? In fact, I tend to think that the Spring IoC container is an abstract factory, because the Sping IOC container can include Vientiane, which can not only manage common bean instances, but also manage factory instances.

3Proxy Mode:

When the client code needs to call an object, the client does not actually care whether the object is obtained accurately or not. It only needs an object that provides this function, in this case, we can return the proxy of this object ).

Actual use case:

I believe that the reader should feel a little bit about the Spring AOP framework: When the proxy bean in the spring container implements one or more interfaces, the AOP proxy created by spring is such a dynamic proxy. What is the difference between Spring AOP and the example application? Spring AOP is more flexible. When sping defines invocationhandler class invoke (), it does not decide which Interceptor to call in hard encoding mode, but decides in invoke () through the configuration file () the Interceptor to be called in the method, which achieves more thorough decoupling-when the program needs to expand new features for the target object, there is no need to change the Java proxy, you only need to add more interceptor configurations in the configuration file.

4Command mode:

A method needs to complete a function. Most of the steps to complete this function have been determined, but a few specific steps may not be determined. You must wait until you execute this method. Specifically, assume that a method needs to traverse the array elements of an array, but it cannot determine how to process these elements when traversing the array elements. You need to specify the specific processing behavior when calling this method.

Example: hibernatecallback Interface

5Rule mode:

Encapsulated series of algorithms

Application Example: the dialect of hibernate will feel a little bit. This dialect class represents the abstract parent class of each database dialect, but there may be some differences in the persistent access of different databases, especially in the paging algorithm, different sub-classes of dialect represent a specific database access policy. To separate the client code from the specific database and dialect implementation classes, Hibernate needs to specify the dialect subclass used by the application in the hibernate. cfg. xml file.

Similar to this, spring's resource interface is also a typical policy interface. Different implementation classes represent different resource access policies. Of course, spring can select the appropriate resource implementation class very intelligently. Generally, spring can decide to use the appropriate resource implementation Class Based on the prefix; you can also decide to use the appropriate resource implementation Class Based on the implementation class of applicationcontext. For more information, see section 8.3.

6Facade mode:

With the continuous improvement and development of the system, they will become more and more complex, and the system will generate a large number of classes, which makes the program process more difficult to understand. The facade mode can provide a simplified interface for these classes to simplify the complexity of accessing these classes. Sometimes this simplification may reduce the flexibility of accessing these underlying classes, but in addition to the demanding clients, it generally provides all the required functions. Of course, demanding users can still directly access the underlying classes and methods.

Example: We can think that hibernatetemplate is the facade of sessionfactory, session, query and other classes. When the client program needs to perform persistent queries, the program does not need to call these classes, instead, you can directly call the hibernatetemplate method.

7Bridging Mode:

Due to actual needs, a class has two or more dimensional changes. If you only use inheritance, this requirement cannot be achieved, or the design becomes quite bloated.

Actual Application Dao

8Observer Mode

The observer mode defines one-to-multiple dependencies between objects, so that one or more observer objects can observe one topic object. When the status of a topic object changes, the system can notify all observer objects dependent on this object so that the observer object can be updated automatically.

Actual use case: JMS

[Design mode] application scenarios of 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.