Policy patterns, mediator patterns, and template method patterns

Source: Internet
Author: User


These days our Java learning about several important design patterns of the relevant content, so write your own on the policy model, intermediary mode and template Method model understanding

A policy model

The policy pattern is simply the encapsulation of a set of algorithms that can implement the different functions of the program requirements, they can be replaced by each other, and the policy pattern makes the algorithm independent of the customer who uses it, that is, the desired algorithm can be dynamically selected.

The policy pattern is used to encapsulate the algorithm, but in practice we can use it to encapsulate almost any type of rule, and we can consider using the policy pattern when we need to apply different rules in different situations in the analysis process.

In the basic policy model, the responsibility for selecting the specific implementation is assumed by the client object, which is then transferred to the context object for use.

UML View


The structure of the policy model includes three roles:

1 Policy (strategy): A policy is an interface that defines several algorithm identifiers (algorithm ()), that is, a number of abstract methods are defined;

2 Specific strategy (CONCRETESTRATEGY): The specific strategy is to implement the class of policy interface, each specific strategy of the algorithm () method to rewrite, to achieve the above mentioned algorithm to different requirements of different functions;

3 context: A reference to a docking port policy that relies on the class of the interface policy, and the context contains a variable that has a policy declaration. Context provides a method that delegates a policy variable to a method in the policy interface overridden by a specific policy. (such as the Lookalgorithm () method in the UML view);

For specific related code, please click the following

http://blog.csdn.net/u012186491/article/details/13080227

Second intermediary model

First, a definition is given to the mediator pattern: A Mediation object is used to encapsulate a series of object interactions. Intermediaries do not need to refer explicitly to each other, so that they are loosely coupled and can independently alter their interactions. In simple terms, the original two direct references or dependent objects are disassembled, and a "mediation" object is added to the middle to make the two ends of the object and the "mediation" object referenced or dependent.

In the broker model, because the specific mediator controls centralization, the interaction complexity becomes the mediator's complexity, making the intermediary more complex than any one of the specific colleagues.

His advantages:

1 The presence of a mediator reduces the coupling between colleagues, making it possible to change and reuse colleagues and intermediaries independently;

2 because of the abstraction of the object's collaboration, the mediator is taken as an independent concept and encapsulated in an object so that the object of concern shifts from the behavior of the object itself to the interaction between them;


Four roles for mediator mode:

1 intermediary: An interface that defines the communication between the roles of each colleague;

2 Specific intermediaries: a class that implements the interface of the mediator, which contains references from all specific colleagues, and enables collaborative behavior by coordinating the roles of each colleague.

3 colleague: An interface that defines the methods that a specific coworker needs to implement.

4 Specific colleagues: a class that implements simultaneous interfaces, including references to specific intermediaries, through specific intermediaries, and other specific interactions.

http://blog.csdn.net/u012186491/article/details/13081733

Three template method Patterns

When we want to accomplish a process or a sequence of steps at a more detailed level that is consistent at a particular level of detail, we usually consider using template method patterns to handle it. The template method pattern makes inheritance more meaningful by raising all the duplicated code to the parent class, rather than having each subclass repeat itself.

Template method Pattern Definition: Defines an algorithm skeleton in an operation (defines an abstract class), delays a small number of details different from the skeleton into subclasses to implement (implement abstract methods in abstract classes).

The template method pattern includes two roles:

1 Abstract Template: An abstract class that defines one or more abstract methods that subclasses can implement. The abstract class also implements a template method to define the skeleton of an algorithm. This template method not only calls the preceding abstract method, but also invokes other operations, as long as it completes its own mission.

2 Specific method: The concrete template is the subclass of the abstract template, overriding the abstract method defined in the abstract Touch Board has completed the steps associated with a particular subclass in the algorithm.


Above all is oneself study and understand, what mistake please everybody pointed out in time, thank you.

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.