Design patterns in the real life: abstract factory and factory method

By | April 13, 2016

There are two design patterns which are called “factory” patterns: factory method and abstract factory. They are called factories because they produce something. The interesting thing is that there are other creational design patters which are called differenctly, but they still produce something, so, just following this logic, all those patterns could be called factories.

The devil is, as always, in the details. Singleton is focused on the ability to reuse the same object. Prototype is focused on the ability to create a copy of an object. Factory patterns are focused on the mass production of new objects.

Still, what would be the real-life analogy for the abstract factory?

We are talking about the facility that can produce families of objects. It seems, this is what restaurants do.. Restaurants can produce breakfasts, launches, and dinners. What exactly you get for launch depends on which restaurant you go to. Those are the keys in this pattern:

– There is a client (you)
– The client is provided with a concrete implementation of the abstract factory (that particular restaurant around the corner)
– The client knows that that particular factory can produce objects of specific type (you can order breakfast, lunch, or dinner in that restaurant)

What’s the difference from the abstract method pattern? There seem to be quite a bit of overlap in these two, however, where abstract factory is less specialized and can create many different things, abstract method is all about creating different versions of the same. In the example with the restaurants above, we would be talking about those restaurants that can serve lunch only. So, we would come in and say “I want launch special”. And, depending on the restaurant, we would get a particular version of that lunch.

Leave a Reply

Your email address will not be published. Required fields are marked *