Why is the factory method a class template, and the abstract factory object template?

From the GOF book:

Class templates relate to the relationships between classes and their subclasses. These relationships are established by inheritance, so they are statically fixed at compile time. Object templates with object relationships that can be changed at run time and are more dynamic. Almost all templates use inheritance to some extent. Thus, the only patterns designated as “class templates” are those that focus on the class.

Why is the factory method a class template and an abstract factory object template, given that they look very similar to templates?

Thanks.

+8
oop design-patterns abstract-factory factory-method
source share
3 answers

The GOF book says:

Intention

Define an interface for creating an object, but let the subclasses determine which class should be created.

What does it mean? Let's take a look at the example that the book shows.

Design Patterns, GOF - Factory Method

In this example, the environment defines the Application interface to allow others to implement it. This means that I can implement, for example. a MyApplication or MyOtherApplication as follows:

 public class MyApplication extends Application { protected Document createDocument() { return new MyDocument(); } } public class MyOtherApplication extends Application { protected Document createDocument() { return new MyOtherDocument(); } } 

When the environment begins, she can choose one of these implementations depending on what she finds on the way to the classes.

But this means that after the framework creates an instance of MyApplication or MyOtherApplication , a way to create a document is created. The way a document is created can no longer be changed at run time for an Application instance. There is no customization or anything else that you can use to change the way you create a Document . Thus, it is also called a virtual constructor and, therefore, is a class template .

Factory Abstract

Unlike the Factory method, the abstract Factory can be modified at runtime and, therefore, as the objects it creates. That is why they say it is an object template .

Abstract Factory is also responsible for creating

... families of related or dependent objects ...

This is also a difference from the factory method . virtual constructor .

+1
source share

Factory The method and abstract of Factory are similar in intentions, but completely different in implementation (you can even say the opposite). In other words, these patterns represent different ways to solve the same problem (instantiation).

GoF says

We classify design patterns according to two criteria. The first criterion, called the goal , reflects what the template does.

Since their intention is similar (i.e., they have the same goal), these two patterns are classified as creation.

GoF keeps saying

The second criterion, called scope , indicates whether the template is primarily applied to classes or objects.

This leads to a quote from the OP, where each object and region of the object are defined. Since the implementation of the Factory method focuses on inheritance (class relation), while the abstract Factory implementation focuses on composition (object relation), these two patterns are classified in opposite areas.

The definitions and implementations of these two patterns can be found in many other SO threads, so I will not repeat them. I also raised the issue of composition and inheritance in these two templates elsewhere .

+1
source share

Factory templates are probably best placed in their own category. But the logic of separating objects / classes can be quite simple. Factory method in its minimal form is static (not custom), like classes. But the abstract Factory result (the object they create) the class depends on some input data, and since it is a dynamic effect, it should be placed in the category of the object template.

0
source share

All Articles