Head first-Explanation of policy Mode

Source: Internet
Author: User
I. Summary:


The Strategy mode is a previous design mode in both the big talk design mode and the head first mode. It can be seen that it is relatively simple.

Strategy is behavior-oriented, that is, it focuses on methods.


II. Introduction:


Now, by simulating a duck instance, let's talk about the strategy.


Level 1: simple inheritance


First of all, all kinds of ducks are screaming while swimming. We can easily retrieve the class: Duck; Method: Swimming, calling, and appearance. Therefore, we have drawn the soy sauce:


Code implementation:

/*** @ Author qmx * @ version 1.0 * @ created 21-October 11-2014 15:44:32 */public abstract class duck {public duck () {} public void display () {} public void fly () {}public void queck () {} public void swim (){}}

Subclass: (write only one)


/*** @ Author qmx * @ version 1.0 * @ created 21-October 11-2014 15:44:32 */public class mallardduck extends duck {/*** the appearance is green */Public void display () {}}


Layer 2: added Interfaces


Of course, we are not satisfied with this step. Every subclass is different from its parent class, so a lot of code will be generated. For example, if I add a rubber duck, I will not fly, but inherit the fly method in the parent class. For example, if you add a bait duck, it will not fly or call it. To solve this problem, we introduced interfaces.



The figure is messy. We should remember that the interface defines the fly method, but it is not implemented. Mallarduck and redheadduck have the same two fly implementations. In this case, we have to ask them to rewrite the fly method in the interface. Isn't this a lot of repetitive code?


Therefore, we further integrate it by referencing the parent class to the subclass object.


Layer 3: Rule Mode



Note:

1. Various fly Methods inherit the flybehavior interface and encapsulate it. This allows code reuse.

2. Add the member variable flybehavior to the duck abstract class. (If a class is a member variable of another class, the relationship between the two classes is an aggregation relationship ). In this way, the abstract class can be used to delegate the fly action to the flybehavior class.

3. The local parameter in setflybehavior in duck is flybehavior. In this way, the dynamic call is implemented. The setflybehavior parameter can be used to call the fly subclass.

4. In the duck performquack method, the delegate is applied and the fly object of the subclass in the flybehavior interface is called directly.


Code:

/*** @ Author qmx * @ version 1.0 * @ created 21-October 11-2014 16:47:39 */Public writable ack class duck {flybehavior; // The member variable quackbehavior; public duck () {} public abstract void display () {} public void initialize mquack () {flybehavior. fly (); // delegate the fly action to the flybehavior class} public void initialize mquack () {quackbehavior. quack ();}/***** @ Param FB */Public void setflybehavior (flybehavior FB) {fybehavior = f B; // dynamically point to the subclass object}/***** @ Param QB */Public void setquackbehavior (quackbehavior QB) {quackbehavior = QB;} public void swim () {system. out. println ("all ducks float, even decoys! ");}}

Subclass inherits the parent class (write only one ):


/*** @ Author qmx * @ version 1.0 * @ created 21-October 11-2014 16:47:40 */public class mallardduck extends duck {public mallardduck () {flybehavior = new flynoway (); quackbehavior = new quack ();} public void display () {system. out. println ("I'm a mallard duck! ");}}

Interface flaybehavior:

/*** @ Author qmx * @ version 1.0 * @ created 21-October 11-2014 16:47:39 */public interface flybehavior {public void fly ();}

Implement the flyrocketpowered interface (write only one ):


/*** @ Author qmx * @ version 1.0 * @ created 21-October 11-2014 16:47:40 */public class flywithwings implements flybehavior {public void fly () {system. out. println ("I'm flying with my wings! ");}}

In this way, when the client is called:

Duck Mallard = new mallardduck (); <PRE name = "code" class = "Java"> Mallard. parse mfly (); // "I'm not fly !"
Mallard. setflybehavior (New flywithwings (); Mallard. parse mfly (); // It is "I'm flying with my wings !"
 


Iii. Summary


In fact, the policies in the previous design model were used together with the responsibility chain during the settlement of the data room charging system. Under the strong lineup of the responsibility chain, the strategy is always a little small, I thought it would be okay. I don't need to summarize it. It seems like I will. But in fact, when I use the Java language to write a new one, I feel that I have learned a lot.


The feeling is: When you have read it all over and think you will get it, you actually only get back to 20% to 30%. When you do it again, you actually get 50%; when you think and sum up, you will get between 70% and 80%. When you share your knowledge with others and continue to communicate with each other, you can say that you are a great guy.

Head first-Explanation of 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.