Is there "more" real world example of Abstract Factory pattern? [closed]
Asked Answered
S

1

1

I am currently going through the design patterns of OPP. Just to give you some background. This is not my first time encountering the patterns. I've been programming for a while (decade or so) and I am pretty familiar with many programming principles, especially SOLID. I've been mostly doing web applications development, so maybe I am missing something that could've been learnt in different programming areas.

As the title suggests, I am struggling with grasping deeper the abstract factory pattern. I do understand the definition and also the "when to use the pattern" parts, but I am still missing the point. Especially when adding the Open closed principle from SOLID to the equation.

What do I mean?

enter image description here

The image above is taken from the Design Patterns book from GOF.

To make this post really a question of sort, there are two things that I myself can not find answer to:

  1. When you study Open closed principle it stays that classes should stay closed for modifications. The implementation it results into is something approximating Strategy pattern. When you look at the method structure of AbstractFactory, it lists all the products it creates as separate methods. This means when we want to add new product to the family, we need to modify the AbstractFactory and create new method. Is this not a violation of Open closed principle? Maybe there is not better way to do this, is it?

  2. Secondly, could anyone provide me with some real world example besides the ones used in the books (WidgetFactory - GOF, PizzaStore - HeadFirst)? Do you have any common implementations where you can say "Yes, this is something I usually implement with Abstract factory"? When is Abstract factory truly useful? Or am I understanding the pattern well, but it's just not that common in web development?

I hope I somehow managed to express my uncertainties regarding the pattern. In any case feel free to ask follow up questions, I'll be happy to provide more details.

Thank you in advance!

Dan

Scribe answered 21/3, 2020 at 22:55 Comment(2)
You’ve seen Balusc’s answer here https://mcmap.net/q/16129/-examples-of-gof-design-patterns-in-java-39-s-core-libraries ? That should give you some real world examples?Hasseman
I have not. I will take a look, thank you.Scribe
H
3

The implementation [OCP] results into is something approximating Strategy pattern.

Possibly. There are multiple ways to fulfill the OCP. Bertrand Meyer originally touted inheritance when he defined the principle.

...when we want to add new product to the family, we need to modify the AbstractFactory and create new method. Is this not a violation...

Possibly. It depends on how you implement the pattern, but the GoF book does acknowledge this problem on page 90.

Supporting new kinds of products is difficult. Extending abstract factories to produce new kinds of Products isn't easy. That's because the AbstractFactory interface fixes the set of products that can be created. Supporting new kinds of products requires extending the factory interface, which involves changing the AbstractFactory class and all of its subclasses. We discuss one solution to this problem in the Implementation section.

The solution mentioned in the GoF book is to parameterize the AbstractFactory method, which results in its own set of problems.

...it's just not that common in web development?

No, it's not. Dependency Injection was not a common pattern when the GoF book was written, but today it has all but replaced the need for Abstract Factories. I would almost be willing to go so far as to call Abstract Factory an anti-pattern today; but I remain open-minded that there could be a problem better suited to AF than DI. I just haven't seen one yet.

Hearthstone answered 22/3, 2020 at 2:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.