0% found this document useful (0 votes)
1 views4 pages

Strategy

The document describes the implementation of the Strategy Pattern using a Duck Simulator. It defines interfaces for flying and quacking behaviors, along with concrete implementations for each behavior. The abstract Duck class and its concrete subclasses demonstrate how different ducks can exhibit various flying and quacking behaviors dynamically.

Uploaded by

rida.maryam1215
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Strategy

The document describes the implementation of the Strategy Pattern using a Duck Simulator. It defines interfaces for flying and quacking behaviors, along with concrete implementations for each behavior. The abstract Duck class and its concrete subclasses demonstrate how different ducks can exhibit various flying and quacking behaviors dynamically.

Uploaded by

rida.maryam1215
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// Strategy Pattern: Duck Simulator

// FlyBehavior interface

interface FlyBehavior {

void fly();

// Fly behavior implementations

class FlyWithWings implements FlyBehavior {

public void fly() {

[Link]("I'm flying!!");

class FlyNoWay implements FlyBehavior {

public void fly() {

[Link]("I can't fly");

class FlyRocketPowered implements FlyBehavior {

public void fly() {

[Link]("I'm flying with a rocket!");

// QuackBehavior interface

interface QuackBehavior {

void quack();

// Quack behavior implementations

class Quack implements QuackBehavior {

public void quack() {


[Link]("Quack");

class MuteQuack implements QuackBehavior {

public void quack() {

[Link]("<< Silence >>");

class Squeak implements QuackBehavior {

public void quack() {

[Link]("Squeak");

// Abstract Duck class

abstract class Duck {

FlyBehavior flyBehavior;

QuackBehavior quackBehavior;

public Duck() {}

public void performFly() {

[Link]();

public void performQuack() {

[Link]();
}

public void swim() {

[Link]("All ducks float, even decoys!");

public void setFlyBehavior(FlyBehavior fb) {

flyBehavior = fb;

public void setQuackBehavior(QuackBehavior qb) {

quackBehavior = qb;

public abstract void display();

// Concrete Duck types

class MallardDuck extends Duck {

public MallardDuck() {

flyBehavior = new FlyWithWings();

quackBehavior = new Quack();

public void display() {

[Link]("I'm a real Mallard duck");

}
class ModelDuck extends Duck {

public ModelDuck() {

flyBehavior = new FlyNoWay();

quackBehavior = new Quack();

public void display() {

[Link]("I'm a model duck");

// Test class

public class MiniDuckSimulator {

public static void main(String[] args) {

Duck mallard = new MallardDuck();

[Link]();

[Link]();

Duck model = new ModelDuck();

[Link]();

[Link](new FlyRocketPowered());

[Link]();

You might also like