Review the design pattern again--strategy mode

Source: Internet
Author: User

Design pattern before is learned, this newspaper soft test is both review and improve. Soft test needs to learn the design pattern through Java, although the previous learning is C #, but the idea of design mode is the same. Talk not much to the topic, the following first to understand what the strategy mode.

The policy pattern defines the algorithm family, which is encapsulated separately so that they can be converted to each other, so that the change in the algorithm does not affect the client using the algorithm.

As an example:

Now there is a duck Father class, we need it like a green-headed duck, a red-headed duck. Only need to write a virtual method in the parent class display and then let the subclass to rewrite, Green duck display to achieve green head, Red Duck to achieve red head. Now let all the Ducks can fly, the implementation is relatively simple, in the parent class to add a fly method on it. The same is true if all ducks need to be called, the quack method can be added to the parent class.

So here's the problem, and now we have a rubber duck that inherits from the parent class. The rubber duck will not fly, but it will not be as good as a real duck, the rubber duck can only squeak. We can only override the Fly method of the parent class in the rubber duck class to make it do nothing, and then overwrite the quack method to make it squeak.

Extend it again. A wooden fake duck won't fly or bark. Let it also inherit the parent class and then overwrite the parent class's fly and quack methods, so that both methods do nothing.

This does the rubber duck and the wood duck are realized, but the rubber ducks do not need fly method, wood ducks do not need fly and quack method, but all to write over the cover out the parent class, is simply the lily. It is even more painful to add new ducks to come in to consider whether you need to overwrite the parent class fly and quack. Well, how about that?

At this point we need to separate the parts that may change in the application and no longer affect other parts.

In this case, the flight behavior and the call behavior are separated from the duck class, and the duck class no longer needs to know the implementation details of the behavior. The class diagram is as follows:

Code implementation:

Duck class public abstract class Duck{flybehavior flybehavior;//instantiated Flight object Quackbehavior quackbehavior;//instantiation called object Public Duck () {} public abstract void display ();//representation of the virtual method public void Performfly () {flybehavior.fly ();} public void Performquack () {quackbehavior.quack ();} public void Swim () {System.out.println ("All Ducks Float,even decoys");}}

Green Head Duck public class Mallardduck extends Duck{public Mallardduck () {quackbehavior=new quack (); flybehavior=new Flywithwings ();} public void display () {System.out.println ("I ' m a real mallard duck");}}
Flight behavior interface public interface Flybehavior{public void Fly ();
Will fly for implementation of public class Flywithwings implements Flybehavior{public void Fly () {System.out.println ("I ' m flying!");}}
will not fly for implementation of public class Flynoway implements Flybehavior{public void Fly () {System.out.println ("I can ' t Fly");}}
Called Behavior interface public interface Quackbehavior{public void Quack ();}
The behavior of the public class quack implements quackbehavior{public void Quack () {System.out.println ("quack");}}
Do not call behavior to implement public class Mutequack implements quackbehavior{public void Quack () {System.out.println ("<Silence>");}}
Squeak behavior Implementation public class Squeak implements quackbehavior{public void Quack () {System.out.println ("Squeak");}}
Main function public class Miniducksimulator{public static void Main (string[] args) {Duck mallard=new mallardduck (); Mallard.performquack (); Mallard.performfly ();}}
Operation Result:

Advantages:

1. The strategy mode provides a way to manage related algorithm families. The hierarchy structure of a policy class defines an algorithm or a family of behaviors. Proper use of inheritance can transfer common code to the parent class, thus avoiding duplicate code.

2. The policy mode provides a way to replace the inheritance relationship. Inheritance can handle multiple algorithms or behaviors. If the policy mode is not used, then the environment class using the algorithm or behavior may have some subclasses, each of which provides a different algorithm or behavior. However, the user of the algorithm or behavior is mixed with the algorithm or the behavior itself. The logic that determines which algorithm to use or which behavior to take is mixed with the logic of the algorithm or behavior, so that it is impossible to evolve independently. Inheritance makes it impossible to dynamically change an algorithm or behavior. 3. Use the policy mode to avoid using multiple conditional transfer statements. Multiple transfer statements are difficult to maintain, and it mixes the logic of which algorithm or behavior is taken with the logic of the algorithm or the action, all in a multiple transfer statement, more primitive and backward than the method of using inheritance. Disadvantages: 1. The client must know all of the policy classes and decide which policy class to use at its own discretion. This means that the client must understand the differences between these algorithms in order to select the appropriate algorithm classes at the right time. In other words, the policy pattern applies only to situations where the client knows all the algorithms or behaviors. 2, the policy mode causes a lot of policy classes, each specific policy class will produce a new class. It is sometimes possible to design a policy class to be shareable by saving the environment-dependent state to the client, so that the policy class instance can be used by different clients. In other words, you can use the enjoy meta mode to reduce the number of objects.

Review design mode again-policy 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.