0

I have 2 classes Workflow1.java and Workflow2.java. At a class Selection.java I want to be able to choose between instantiating one of the 2 classes as a static member however I cannot implement the factory pattern as Workflow1 and Workflow2 cannot be subclasses since their methods are not the same. Although they achieve the same end result they do so by doing entirely different operations. Is there a design pattern for this scenario?

Example: If the classes were WalkHelper.java and DriveHelper.java, the methods you need in each are entirely different but what you are trying to achieve is the same - reach a destination. I haven't created walk() and drive() as methods as WalkHelper.java has existed in our code base and I'm adding DriveHelper.java to it.

  • 1
    If `Workflow1.java` and `Workflow2.java` have different sets of methods, how do you plan to abstract it from the client who is going to use any of these two classes? Can you please explain more about your requirements? – Indra Basak Nov 28 '17 at 04:38
  • @Indra has a very good point and that should be the route you go, however, if you have to be different, you could build an Object Factory then cast it every time you want to do anything with it. You should definitely consider Indra's comments – SynchroDynamic Nov 28 '17 at 04:42
  • @Indra The class that contains the condition check will be called to perform operations by the client. The client doesn't interact with Workflow1.java and Workflow2.java directly. They contain the logic to give the user the final result. – laksmi iragavarapu Nov 28 '17 at 04:46
  • @laksmiiragavarapu As people have already mentioned, you should use the adapter pattern along with the factory pattern. The adapter pattern converts a class into another interface/class that the clients expect. – Indra Basak Nov 28 '17 at 04:50
  • @IndraBasak I will try the solution and update – laksmi iragavarapu Nov 28 '17 at 05:00

1 Answers1

1

It sounds like you can still use a Factory pattern but you may have to use an Adaptor to make them equal... Without knowing more, it's a pretty difficult question to answer.

interface IFactory {
  void run();
  String getResult();
}

class Workflow1Adapter implements IFactory {
  Workflow1 wf1 = new Workflow1();

  public void run() {
    wf1.doSomething();
  }

  public String getResult() {
    wf1.doAnother();
  }
}

class Workflow2Adapter implements IFactory {
  Workflow2 wf2 = new Workflow2();

  public void run() {
    wf2.doThatThing();
  }

  public String getResult() {
    wf2.doReturn();
  }
}

class Workflow1 {
  public void doSomething() {}
  public String doAnother() {}
}

class Workflow2 {
  public void doThatThing() {}
  public String doReturn() {}
}
Christian
  • 3,708
  • 3
  • 39
  • 60
  • I thought about having wrappers (or as you say adapters) of the two classes which would implement an interface but I thought that was too complicated and maybe there were other patterns that combine a factory and a wrapper – laksmi iragavarapu Nov 28 '17 at 04:49
  • https://www.tutorialspoint.com/design_pattern/index.htm . Go through and make suggestions. Happy to comment. – Christian Nov 28 '17 at 04:51
  • You wanted design patterns, there isnt one called a Wrapper :) – Christian Nov 28 '17 at 04:52
  • I will go through the link and update. Thank you :) I saw this http://www.thecodedself.com/The-Difference-Between-an-Adapter-and-a-Wrapper/ and have read about adapters so I'm confused. And also https://stackoverflow.com/questions/350404/how-do-the-proxy-decorator-adapter-and-bridge-patterns-differ – laksmi iragavarapu Nov 28 '17 at 05:00
  • I've heard them called Wrappers before but a Wrapper can be a Decorator, Adapter or even a Proxy pattern – Christian Nov 28 '17 at 05:03
  • Check this dude and watch his Design Pattern videos :) https://www.youtube.com/channel/UCbF-4yQQAWw-UnuCd2Azfzg – Christian Nov 28 '17 at 05:05
  • Thank you. I will check it out. – laksmi iragavarapu Nov 28 '17 at 05:09