0

I am hoping to use design patterns in my java project and for me factory pattern seems to be the most suitable since its less complex and I found a suitable scenario in my project to implement it.

In the scenario I have 3 classes which could be made to implement an interface. I learnt how to implement the factory pattern in java from the tutorialspoint

But the only problem limiting me is that the names of the methods are not the same as in the tutorial. Is there any way to apply the factory design pattern for this scenario?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Amal
  • 91
  • 1
  • 1
  • 10
  • 5
    What interface? What 3 classes? Please clarify your question. – Elliott Frisch Jul 04 '18 at 18:56
  • You'll have to elaborate on "this scenario". Sounds like you're hung up on a name, just use names that make sense to your app. – ChiefTwoPencils Jul 04 '18 at 18:56
  • 5
    You do know how to change the name of a method, right? I'm having trouble understand what the problem is, exactly. – Radiodef Jul 04 '18 at 18:57
  • There’s no problem in using your own names of classes and methods when you use a design pattern. Go ahead. :-) – Ole V.V. Jul 04 '18 at 18:58
  • @Radiodef I mean the 3 classes having different methods! But if I am using factory method I have to implement an interface which has the abstract methods! So it good to strore all the method names used in the 3 classes in the interface though methods of each class may not be used by another class? – Amal Jul 04 '18 at 19:00
  • @OleV.V. If I am to implement an interface don't I need to overried all the absttract methods? – Amal Jul 04 '18 at 19:01
  • 1
    You can read more on [java-design-patterns](https://github.com/iluwatar/java-design-patterns/tree/master/factory-method) repository. There are some good examples there. – Dennis Vash Jul 04 '18 at 19:01
  • 1
    You've been asked multiple times. Please [edit] your question to include your code – OneCricketeer Jul 04 '18 at 19:03
  • 2
    You should [edit your question](https://stackoverflow.com/posts/51179543/edit) to clarify instead of just posting in the comments. You should also add a short code example that shows the 3 classes you're trying to make implement the factory pattern. – Radiodef Jul 04 '18 at 19:03
  • 1
    Your factory produces concrete implementations of a shared interface. You must implement all interface methods in the types you return, regardless of them being used. But if you have methods that are completely different classes, Factory might be a bad pattern... But, going off the ShapeFactory example, a Circle class can have a getRadius method not implemented on other shapes – OneCricketeer Jul 04 '18 at 19:06
  • @Radiodef I havent coded yet but I will put tuorial code example and further explain my doubt :O – Amal Jul 04 '18 at 19:06
  • 1
    By the way https://stackoverflow.com/questions/29504945/the-best-way-to-implement-factory-without-if-switch – OneCricketeer Jul 04 '18 at 19:07
  • @cricket_007 Thanx you understood my problem :) So for such a scenario what would be the most suitable design pattern? – Amal Jul 04 '18 at 19:08
  • I cannot see your code, so I have no idea why an abstract factory isn't what you want – OneCricketeer Jul 04 '18 at 19:12
  • I can use an abstract factory but the issue is then only a single class implements an interface so it may be useless – Amal Jul 04 '18 at 19:13
  • 1
    Who keeps up voting this question -_- – d.j.brown Jul 04 '18 at 19:46

1 Answers1

3

If you are using Spring, there is a better way to achieve this. Although it can not be called as Factory Pattern. You can create @Bean for each of the implementations and use specific one whenever required. Or you can annotate each of the implementation class with @Service, @Component, @Repository as per the scenario with unique qualifier names and @Autowired them with @Qualifier("qualifier") anotation. Ex:

interface MyInterface {
    // method signature
}

@Component("myClass1")
class MyClass1 implements  MyInterface{
    // code
}

@Component("myClass2")
class MyClass2 implements  MyInterface{
    // code
}

public class Main{
    @Autowired
    @Qualifier("myClass1")
    MyInterface myClass1;

    @Autowired 
    @Qualifier("myClass2")
    MyInterface myClass2;

    public static void main(){
        myClass2.func1();
        myClass1.func2();
    }
}

If you don't want to use Spring dependency injection you can do so

interface MyInterface {
   public void invoke();
}

class MyClass1 implements  MyInterface{
    public void myMethod(){
    }
    public void invoke(){
       myMethod();
    }
}

class MyClass12 implements  MyInterface{
    public void someDifferentName(){
    }
    public void invoke(){
        someDifferentName();
    }
}

now call invoke() method.

raviiii1
  • 936
  • 8
  • 24