Java and mode Learning Series-Rule Mode

Source: Internet
Author: User

 

I. Strategy(Strategy)Schema Structure

The rule mode is intended for a groupAlgorithmEncapsulate each algorithm into an independent class with a common interface so that they can replace each other. The policy mode allows the algorithm to change without affecting the client.

The structure is as follows:

If all specific policy classes have common behaviors, put these public behaviors into the same abstract policy role.StrategyClass. At this time, the abstract policy role must useJavaAbstract class implementation.

2. The policy mode is inJavaApplication in

Examples of using the rule mode are mainly inJava. AWTAndSwingLibrary.

AWTInLayoutmanager

Java. AWTThe library must be dynamically determined by the client during running.ContainerHow can an object arrange all itsGuiComponent.JavaThe language provides several different arrangement methods, packaged in different classes:Borderlayout,Flowlayout,Gridlayout,Gridbaglayout,Cardlayout. The class diagram is as follows:

SwingInBorder

In anySwingYou can draw borders on all components.(Border)For examplePanel,ButtonAndSwingLibrary provides many border types, includingBevel,Line,TitledAndCompoundborderClass,SwingThe base class of the component isJcomponentClass, and this class is responsibleSwingDraw a border on the component.

JcomponentClassPaintborder ()And keep a private reference to the border object. BecauseBorderIs an interface rather than a specific class, so this reference can point to any implementationBorderExcuse the border object. The class diagram is as follows:

Iii. When to use the policy Mode

1If there are many classes in a system, the difference between them is only their behavior, then the policy mode can dynamically allow an object to select a behavior among many behaviors.

2A system must dynamically select one of several algorithms. These algorithms can be encapsulated into specific algorithms, and these specific algorithm classes are subclasses of an abstract algorithm class. In other words, these specific algorithm classes have a unified interface. Due to the polymorphism principle, the client can choose to use any specific algorithm class and only hold one data type as the object of the abstract algorithm class.

3The data used by a system algorithm cannot be known to the client. The policy mode prevents clients from involving complex and algorithm-only data that is not necessary for access.

4If an object has a lot of behaviors and the appropriate mode is not used, these behaviors must be implemented using multiple conditional selection statements. The policy mode can avoid the use of multiple conditional selection statements that are difficult to maintain.

 

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.