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 8865029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:24:16+00:00 2026-06-14T16:24:16+00:00

Refering to posted question here , could you please comment if this is good

  • 0

Refering to posted question here, could you please comment if this is good approach to solve OPTIONAL behaviour for extending class, by using composition, not inheritance. Plannable behaviour is here extended by Strategy pattern.

So, class Task could optionally have any combination of various behaviours. Plannable is just one of them, therefore inheritance does obviously not making sense here.

Question is what to do when Task does not have particular behaviour ? I see few possible approaches:

  1. Instantiate concrete strategy for each task, and implement “Dummy” strategy when Task is not Plannable (this alternative is shown below). There are weird nullable Start and Finish variables all over the code, in this case…
  2. Having nullable IPlanningStrategy variable in the case task won’t be planned, and instantiate with concrete Strategy only when it is “promoted” to be Plannable.

Alternative (1) should be something like this:

public class Task
{
    public string Title { get; set; }

    private IPlanningStrategy planningStrategy;

    public Task()
    {
        planningStrategy = new NoPlanStrategy();
    }

    public Task(IPlanningStrategy initialPlanningStrategy)
    {
        planningStrategy = initialPlanningStrategy;
    }

    public void SetPlanningStrategy(IPlanningStrategy newPlanningStrategy)
    {
        planningStrategy = newPlanningStrategy;
    }

    public DateTime? Start { get { return planningStrategy.Start; } }
    public DateTime? Finish { get { return planningStrategy.Finish; } }
}


public interface IPlanningStrategy
{
    public void CalculatePlan();
    public DateTime? Start { get; }
    public DateTime? Finish { get; }
}

// "Dummy" strategy, used when Task does not have Planning behaviour
//
public class NoPlanStrategy : IPlanningStrategy
{
    public void CalculatePlan() { }
    public DateTime? Start { get { return null; } }
    public DateTime? Finish { get { return null; } }
}



public class PlanStrategyA : IPlanningStrategy
{
    private int parameter1;
    private int parameter2;
    private DateTime? start;
    private DateTime? finish;

    public PlanStrategyA(int p1, int p2)
    {
        parameter1 = p1;
        parameter2 = p2;
        start = finish = null;
    }

    public void CalculatePlan()
    {
        // ... uses parameter1 & parameter2
        // ... to calculate start and finish
    }

    public DateTime? Start { get { return start; } }

    public DateTime? Finish { get { return finish; } }
}

public class PlanStrategyB : IPlanningStrategy
{
    public int parameter3;

    // ... the rest is similar to PlanningStrategyA

}

Now I see different ** VERY IMPORTANT** problem here. Each of my concrete Strategy class holds, beside algorithm, which is of course shared for all tasks implementing this algorithm, additional parameters, which belongs only to particular Task.

You can imagine parameter1 as Effort (or, say, number of remaining hours required to complete task), and say parameter2 as LastDate (representing constraint date, ie. last allowable date to finish task). This parameters naturally belongs to particular Task, but only if it implements this particular StrategyA.

It seems that instantiate strategy outside Task class does not make sense ? Or this should be job for some Factory method ?

  • 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-06-14T16:24:17+00:00Added an answer on June 14, 2026 at 4:24 pm

    IMO you are exposing too much information in your Task class. I would do as follows:

    public class Task
    {
        // what is title being used for?
        public string Title { get; set; }
    
        private IPlanningStrategy planningStrategy;
    
        public Task(IPlanningStrategy initialPlanningStrategy)
        {
            // Initialize it outside of constructor
            if(initialPlanningStrategy == null)
            {  
               throw new NullArgumentException(); // or return. 
            }
            planningStrategy = initialPlanningStrategy;
        }
    
        public void CalculatePlan(){
            // check null and return.
            planningStrategy.CalculatePlan();
        }
    
    }
    

    Your client shouldnt know start, finish, i dont see that as responsibility of a container for algorithms.

    Moreover, if your NoPlanStrategy not doing anything, why introduce it. removeit.

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

Sidebar

Related Questions

refering to this answer , the second code block. My question is: If I
Zend Framework 2 + Doctrine 2 Refering to this question, I have a simple
slightly related to this question here . My problem: I am writing a bit
By refering to Another Question About ActiveX freezes IE , I wrote an Active
I have tried to do this refering to following link. http://skypher.com/index.php/2008/07/28/function-list-for-php/ But no success.
I have 2 questions to ask here. I am refering http://www.broculos.net/en/article/android-101-how-create-stackview-widget to create a
I get this, clicking 'no' means the page displays flawlessly. it's refering to this
My co-worker posted a question on his blog . He is referring to rendering
I am refering to this article from Jimmy Bogard He is suggesting two projects
I had posted a question on DateTime to String conversion, I got many satisfying

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.