1

Does the Factory Method only follow the Dependency inversion principle and interface segregation principle in the SOLID? I found that on another website but couldn't really find any arguments why it implement the interface segregation principle. It follow DIP because it depend on an interface(abstraction) and not concrete classes.

2
  • Not sure I have understood the question , Could you please elaborate . It seems little confusing "Does the Factory Method only follow the Dependency inversion principle and interface segregation principle in the SOLID?" and at the end "It follow DIP because it depend on an interface(abstraction) and not concrete classes." Oct 2, 2017 at 7:02
  • 3
    'I found that on another website' link so we can read ourselves?
    – weston
    Oct 2, 2017 at 11:13

2 Answers 2

2

Firstly factory method is a standalone design pattern to create objects without exposing the instantiation logic to the client. The factory method doesn't follow any principle but it is meant to separate the responsibility of object creation to a different class to deal with the problem of creating objects without having to specify the exact class of the object that will be created. As it is a creation pattern it's responsibility is just creation of the object. It has nothing to do with interface segregation principle as well. Interface segregation principle is mainly focusing on not creating fat interfaces but make it lean and group in logically in such a way that no client should be forced to implement the method it doesn't require.

Please don't get confused with SOLID principles and design pattern. SOLID principles are just a set of principles to achieve clean code. Few design pattern helps to achieve SOLID principles. For example, you move the responsibility of object creation to a factory, then factory's whole responsibility would be object creation (single responsibility principles) and so on.

4
  • This answer appears to contradict itself. Firstly, "factory method doesn't follow any principle"; but secondly, "design pattern helps to achieve SOLID principles". So does the pattern follow the principles or not?
    – jaco0646
    Oct 3, 2017 at 15:32
  • @jaco0646 Design Pattern not equal to SOLID principles. But in certain cases, the design pattern will help to achieve SOLID principles. It doesn't mean design patterns are SOLID principles. The main idea behind design patterns is to solve object-oriented problems. Am I clear enough? Oct 3, 2017 at 15:49
  • @jaco0646 yes, your understanding is correct. SOLID principles are a set of rules/principles which make the code clean. Design patterns are meant to solve problems in object-oriented programming Oct 22, 2021 at 12:19
  • i agree, SOLID and design pattern are different, but usually when we use factory design, we will also apply the SOLID principles, becoz in low level module, you still following SOLID principles
    – user192344
    Oct 7, 2022 at 7:47
0

You cannot confirm that the Factory Method pattern will follow the Interface Segregation Principle per se, without the rest of the interfaces. That principle means essentially that a class will most likely use the whole interface of another class, so the factories would probably follow this principle if they only provide a method or two to construct the instances to be used by the client (this comes tightly related to the Single Responsibility Principle in this case). However, that doesn't mean the Client will follow that principle.

As for the Dependency Inversion Principle, it's easy to identify if you compare the design with and without this pattern. Without the FM pattern, your client has to decide which subtype of a constructable object to instantiate and use. Thus, these would be the dependencies (separate components delimited by parenthesis; arrow direction means "depends on"):

(Client) -+-> (ConstructableA)
          \-> (ConstructableB)

When you apply the Factory Method design pattern, the dependencies between components are:

(Client -> IConstructable) <-+- (ConstructableA)
                             \- (ConstructableB)

So, effectively, the dependencies between components are now inverted. In the former case, the Client component had to change (likely) whenever either constructable component changed. In the later, the Client component can be reused in other places, and the Constructable* components have to change whenever IConstructable does.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.