I. Strategy pattern -- 1

Source: Internet
Author: User
Tags scream

Start with the duck application. A duck simulation game contains various ducks. We need to use OO technology to implement this system.

1. Define a duck superclass and define the characteristics of ducks. Then different ducks are used as subclasses to inherit the duck superclass.

1. superclass: Duck. Java

/*** Parent class of all ducks, which defines the characteristics of ducks */public class duck {public void quack () {system. out. println ("I can scream");} public void swim () {system. out. println ("I can swim");} public void display (){}}

2. Different ducks inherit from it

Green Duck: mallardduck. Java

/*** The green-headed duck can scream and swim, but its head is green, so it must overwrite the display () method */public class mallardduck extends duck {@ overridepublic void display () {system. out. println ("My header is green! ");}}

Braised Duck: readheadduck. Java

/*** Duck with a red head, can scream, can swim, but the head is red, so overwrite the display () method */public class readheadduck extends duck {@ overridepublic void display () {system. out. println ("My head is red! ");}}

3. Test class testclass. Java

// Test the duck's characteristics. whether it meets the actual public class testclass {public static void main (string [] ARGs) {mallardduck = new mallardduck (); // green duck readheadduck = new readheadduck (); // red duck mallardduck. display (); mallardduck. quack (); mallardduck. swim (); system. out. println ("-------------------"); readheadduck. display (); readheadduck. quack (); readheadduck. swim ();}}

4. Test results:

I'm a green duck! I can scream, I can swim ----------------- I am a duck! I can swim

2. Based on the above program, we need to add the flying feature to the duck, So we add all the flying Methods fly () to the duck. Java superclass ()

1. superclass: Duck. Java

/*** Parent class of all ducks, which defines the characteristics of ducks */public class duck {public void quack () {system. out. println ("I can scream");} public void swim () {system. out. println ("I can swim");} public void display () {} public void fly () {system. out. println ("I Can Fly ");}}

2. Different ducks inherit from it. Green-headed ducks and red-headed ducks do not need to be changed. Add a new rubber duck and bait duck

Rubber Duck: rubberduck. Java

/*** The Rubber Duck can be called but cannot fly, so in order to implement these features, method of the parent class */public class rubberduck extends duck {@ overridepublic void display () {system. out. println ("I'm a rubber duck! ") ;}@ Overridepublic void fly () {// No Fly} @ overridepublic void quack () {system. Out. println (" I can call it ");}}

Bait Duck: decoyduck. Java

/*** Bait duck, no fly, no call */public class decoyduck extends duck {@ overridepublic void display () {system. Out. println ("I'm a bait duck! ") ;}@ Overridepublic void quack () {// not called} @ overridepublic void fly () {// No Fly }}

3. Test class

Public class testclass {public static void main (string [] ARGs) {mallardduck = new mallardduck (); // readheadduck = new readheadduck (); // red duck rubberduck = new rubberduck (); // Rubber Duck decoyduck = new decoyduck (); // bait duck mallardduck. display (); mallardduck. quack (); mallardduck. swim (); mallardduck. fly (); system. out. println ("-------------------"); readheadduck. display (); readheadduck. quack (); readheadduck. swim (); readheadduck. fly (); system. out. println ("-------------------"); rubberduck. display (); rubberduck. quack (); rubberduck. swim (); rubberduck. fly (); system. out. println ("-------------------"); decoyduck. display (); decoyduck. quack (); decoyduck. swim (); decoyduck. fly ();}}

4. Test Results

I'm a green duck! I can scream, I can swim, I can fly ----------------- I am a duck! I can scream, I can swim, I can fly ----------------- I am a rubber duck! I can say that I can swim ----------------- I am a bait duck! I can swim

We can see that in order to implement these features of the Rubber Duck and bait duck, We have to rewrite many methods of the duck parent class and provide empty implementations. If some special ducks are added later, we have to overwrite the methods of the parent class. It may cause many disadvantages.
 
Code is repeated among multiple child classes. It is difficult to know the full behavior of all ducks. The behavior of ducks is not easy to change, and changes will trigger the whole body, causing changes that other ducks do not want.
 
 

3. Therefore, we can understand that the above method inherited by the superclass is unavailable. Next let's try the interface.
 
Because not all ducks can fly and not all ducks can call it, take the fly () method out as the flyable interface, take the quack () method out, and use it as the quackable interface.
 
1. superclass: Duck. Java

/*** Parent class of all ducks, which defines the characteristics of ducks */public class duck {public void swim () {system. out. println ("I can swim");} public void display (){}}

2. flyable interface and quackable Interface

/*** Fly ducks implement this interface */public interface flyable {void fly ();} quackable interface: quackable. java/*** calls the duck to implement this interface */public interface quackable {void quack ();}

3. All subclasses can selectively implement these two interfaces based on their own characteristics.

Green Duck:

/*** The green-headed duck, flying, will call, implement these two interfaces */public class mallardduck extends duck implements flyable, quackable {@ overridepublic void display () {system. out. println ("I'm a green duck! ") ;}@ Overridepublic void quack () {system. out. println ("I will scream");} @ overridepublic void fly () {system. out. println ("I will fly ");}}

Braised Duck:

/*** Duck, flying, called, implements two interfaces */public class readheadduck extends duck implements flyable, quackable {@ overridepublic void display () {system. out. println ("I'm a duck! ") ;}@ Overridepublic void quack () {system. out. println ("I will scream");} @ overridepublic void fly () {system. out. println ("I will fly ");}}

Rubber Duck:

/*** The Rubber Duck is squeaking and does not fly, so the quackable interface */public class rubberduck extends duck implements quackable {@ overridepublic void display () {system. out. println ("I'm a rubber duck! ") ;}@ Overridepublic void quack () {system. Out. println (" I can call it ");}}

Bait Duck:

/*** Bait duck, no fly, no call, no interface required */public class decoyduck extends duck {@ overridepublic void display () {system. out. println ("I'm a bait duck! ");}}

3. Test class:

Public class testclass {public static void main (string [] ARGs) {mallardduck = new mallardduck (); // readheadduck = new readheadduck (); // red duck rubberduck = new rubberduck (); // Rubber Duck decoyduck = new decoyduck (); // bait duck mallardduck. display (); mallardduck. quack (); mallardduck. swim (); mallardduck. fly (); system. out. println ("-------------------"); readheadduck. display (); readheadduck. quack (); readheadduck. swim (); readheadduck. fly (); system. out. println ("-------------------"); rubberduck. display (); rubberduck. quack (); rubberduck. swim (); system. out. println ("-------------------"); decoyduck. display (); decoyduck. swim ();}}

4. Results

I'm a green duck! I will scream, I can swim, I can fly ----------------- I'm a duck! I will scream, I can swim, I can fly ----------------- I'm a rubber duck! I can say that I can swim ----------------- I am a bait duck! I can swim

We can find that we can implement these functions to correctly represent duck features. However, code duplication becomes more and cannot be reused. If there are more types of ducks, We have to modify the methods in many ducks. Therefore, the interface method cannot be used. Next, we should consider using our policyholder 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.