Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7181853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:38:40+00:00 2026-05-28T17:38:40+00:00

I have tried to implement the Head First Duck problem with Startegy. I am

  • 0

I have tried to implement the Head First Duck problem with Startegy. I am trying to implement the Decoy Duck, which intially don’t have the facility to Quack or Fly is implemented by calling a a default constructor( I know this duck don’t have the ability to Fly or Quack). All other Ducks are intitialized by calling the overriden consturctor. In the book a duck with no Fly is implemented with a class FlyNoFly which implements the IFly interface.

For my solution, i am not using this class. Instead, i am checking in the base duck class the Fly propery whether a valid instance is passed for it or not (by if(Fly != null) . if the Fly has a valid reference, then only i am invloking the fly method on that. Else i am throwing a default message.

I want to know whether my implementation is violating any design principle / Whether this is a valid implementation.

Thanks
TutuMon

public abstract class Duck
{
    public IFlyable Fly { get; set; }
    public IQuackable Quack { get; set; }
    public void PerformQuack()
    {
        //Is this checking valid as per OO?
        if (Quack != null)
            Quack.Quack();
        else
            Console.WriteLine("Quack Operation Not supported");
    }

    public void PerformFly()
    {
         //Is this checking valid as per OO?
         if (Fly != null)
             Fly.Fly();
         else
             Console.WriteLine("Fly Operation not supported");
    }

    public abstract void Swim();
}

public class MallardDuck : Duck
{
    public MallardDuck()
    {
    }

    public MallardDuck(IFlyable fly, IQuackable quack)
    {
        Fly = fly;
        Quack = quack;
    }

    public override void Swim()
    {
        Console.WriteLine("Mallard Duck is Swimming");
    }
}


//No Fly and Quack behaviour by default
public class DecoyDuck : Duck
{
     public DecoyDuck()
     {
     }

     public DecoyDuck(IFlyable fly, IQuackable quack)
     {
         Fly = fly;
         Quack = quack;
     }

     public override void Swim()
     {
         Console.WriteLine("DecoyDuck Duck is Swimming");
     }
}

public interface IFlyable
{
     void Fly();
}

public class FlyWithWings : IFlyable
{
     public void Fly()
     {
         Console.WriteLine("You are flying with wings");
     }
}

public class RocketFly : IFlyable
{
    public void Fly()
    {
       Console.WriteLine("Rocket Powered Fly");
    }
}

public interface IQuackable
{
    void Quack();
}

public class RealQUack :IQuackable
{
   public void Quack()
   {
      Console.WriteLine("This is Real Quack");
   }
}

public class PowerQuack : IQuackable
{
    public void Quack()
    {
        Console.WriteLine("Powerful Quacking ");
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        Duck mallard = new MallardDuck
        {Fly=new FlyWithWings(),Quack=new RealQUack()}
        mallard.PerformQuack();
        mallard.PerformFly();

        // He can't Quack or Fly by default
        // So i am calling the default constructor.
        Duck decoy = new DecoyDuck();
        decoy.PerformQuack();
        decoy.PerformFly();

        // Adding behaviours on the fly

        decoy.Fly = new RocketFly();
        decoy.Quack = new PowerQuack();

        decoy.PerformQuack();
        decoy.PerformFly();   
        Console.Read();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:38:42+00:00Added an answer on May 28, 2026 at 5:38 pm

    No, this is not the correct implementation.

    The Strategy pattern is used to avoid the use of if (..) in Object Oriented Code. If you would use the FlyNoFly class you would avoid the if ( null ) check and you can use the same constructor for all Duck objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried to implement this scenario . I have created Code First model,
I have tried to implement the rectangle problem using OOHaskell. {-# LANGUAGE EmptyDataDecls, DeriveDataTypeable,
I have been looking around for solutions, and tried to implement what is often
I have tried to implement a picklist like the one in primeFaces with jQuery
I have tried to implement the ProgressBar in my application that gets displayed when
I have tried to implement the searchable dictionary sample app. Everything seems to be
I have tried to implement the google plus button into our site, but the
I have tried <ul id=contact_list> <li id=phone>Local 604-555-5555</li> <li id=i18l_phone>Toll-Free 1-800-555-5555</li> </ul> with #contact_list
I have tried this... Dim myMatches As String() = System.Text.RegularExpressions.Regex.Split(postRow.Item(Post), \b\#\b) But it is
I have tried what seems like everything - I've done similiar things many times

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.