0

There are many answers to this question here on SO, but I wish to add a small bit of twist in my question. Consider a parent class Parent and it's children Child1 and Child2. Now, let their structure be something like below:

public abstract class Parent {
    public abstract String eat();
    public abstract String sleep();

    public static Parent createInstance (int childType) {
       if (childType == 1) 
         return new Child1();
       else
         return new Child2();
    }
}
public class Child1 extends Parent {
    public String eat() {
        return "nom-nom";
    }

    public String sleep() {
        return "zzzz...";
    }
}
public class Child2 extends Parent {
    public String eat() {
        return "gulp-gulp";
    }

    public String sleep() {
        return "ZzZz...";
    }
}

Now, my questions are:

  • Am I doing something wrong from pure OOP standpoint, by calling the child classes from the parent class?
  • Can the above approach be called as an example for strategy design pattern, if I use it in the following way?
// In some random class:

public String showMeHowYouEat (int childType) {
    Parent foo = Parent.createInstance(childType);
    return foo.eat();
}

public String showMeHowYouSleep (int childType) {
    Parent foo = Parent.createInstance(childType);
    return foo.sleep();
}

Note : I've selected the above approach of creating child class instances from parent class is because I do not wish to write if-else every time I have to invoke the eat or sleep methods based on the childType.

My main intention behind asking this question was to simply understand if I'm breaking some rules of OOP paradigm by creating an instance of child from the parent class based on some input. And if I'm doing so, then what could be a better alternative to the above mentioned approach?

3
  • My question is what advantage you are trying to gain with the Parent.createInstance in the first place? Why would Parent foo = Parent.createInstance(1); be better than Parent foo = new Child1();? Commented Aug 30, 2021 at 12:28
  • To make it dynamic. I wish to create a child depending upon the childType Commented Aug 30, 2021 at 12:31
  • Factory Method pattern Commented Jun 4, 2024 at 8:48

1 Answer 1

1

You are definitely breaking SRP rule

A class should have only one reason to change.

Your Parent class has definitely more reasons to change because you need to modify it when

  1. API will change
  2. new child implementation will be delivered.

You should extract this part to some Factory class/method outside the Parent class

The second question about Strategy pattern cannot be answered because you are not showing the context in which you are using

int childType = 1;
Parent foo = Parent.createInstance(childType);

Maybe it is part of strategy implementation maybe it's not. It depends on how you are using this in the class containing random method

Sign up to request clarification or add additional context in comments.

11 Comments

I think, that's definitely not usage of the strategy pattern as there is no strategy injected; it's just hardcoded if/else, simply a normal function.
Strategy pattern does not require injection of strategy - simple if/else is poor and rough but still valid implementation of Strategy pattern
Sorry for the poor use case example. I've updated my example. Apart from that, as far the Parent class is considered, the only change that it will face is the addition of abstract methods and nothing more. Doing things in the above manner saves me the effort of writing if-else check of the childType in >1 place.
I strongly disagree with "Strategy pattern does not require injection of strategy - simple if/else is poor and rough but still valid implementation of Strategy pattern".
Definition of Strategy pattern is clear and I'm not really sure how you can disagree. Anyway if I'm not right I will be thankful if you will briefly describe why Strategy pattern does not allow you 'simple if/else implementation'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.