Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is factory method a class pattern, while an abstract factory an object pattern?

From GOF book:

Class patterns deal with relationships between classes and their subclasses. These relationships are established through inheritance, so they are static-fixed at compile-time. Object patterns deal with object relationships, which can be changed at run-time and are more dynamic. Almost all patterns use inheritance to some extent. So the only patterns labeled "class patterns" are those that focus on class relationships.

Why is factory method a class pattern, and abstract factory an object pattern, given that they seem to be very similar patterns?

Thanks.

like image 403
Tim Avatar asked Sep 04 '17 16:09

Tim


People also ask

What is the difference between Factory Pattern and Abstract Factory pattern?

Java. The main difference between a “factory method” and an “abstract factory” is that the factory method is a single method, and an abstract factory is an object. The factory method is just a method, it can be overridden in a subclass, whereas the abstract factory is an object that has multiple factory methods on it.

Why would a Factory Method design pattern be a more appropriate design than an Abstract Factory design pattern?

The idea behind the Factory Method pattern is that it allows for the case where a client doesn't know what concrete classes it will be required to create at runtime, but just wants to get a class that will do the job while Abstract Factory pattern is best utilized when your system has to create multiple families of ...

What is the purpose of the factory class in the factory design pattern?

Factory pattern removes the instantiation of actual implementation classes from client code. Factory pattern makes our code more robust, less coupled and easy to extend. For example, we can easily change PC class implementation because client program is unaware of this.

Is Abstract Factory a factory of factories?

Abstract Factory patterns work around a super-factory which creates other factories. This factory is also called as factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.


3 Answers

The GOF book says

Intent

Define an interface for creating an object, but let subclasses decide which class to instantiate.

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

Design patterns, GOF - Factory Method

In the example a framework defines the Application interface to let others implement it. This means that I can implement e.g. a MyApplication or MyOtherApplication like this:

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


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

When the framework starts it might choose one of these implementations depending on what it finds on the classpath.

But it means that after the framework has instantiated either MyApplication or MyOtherApplication the way a document is created is fix. The way a document is created can not be changed anymore at runtime for the Application instance. There is no setter or anything else that you can use to change the way the Document is created. Thus it is also called a virtual constructor and thus it is a class pattern.

Abstract Factory

In contrast to the factory method an abstract factory can be changed at runtime and thus the way the objects it creates. That's why they say it is an object pattern.

A abstract factory is also responsible for creating

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

This is also a difference to the factory method aka. virtual constructor.

like image 128
René Link Avatar answered Oct 16 '22 22:10

René Link


Factory patterns are probably better to place in its own category. But logic behind object/class division maybe quite simple. Factory method in its minimal form is static (not configurable), just like classes are. But abstract factory result (object they produce) class depends on some input data, and since it is dynamic effect it should be put into object pattern category.

like image 38
Mikhail Karakulov Avatar answered Oct 17 '22 00:10

Mikhail Karakulov


Factory Method and Abstract Factory are similar in intent, but wildly different in implementation (you might even say opposite). In other words, these patterns represent different ways of solving the same problem (instantiation).

The GoF says,

We classify design patterns by two criteria. The first criterion, called purpose, reflects what a pattern does.

Because their intent is similar (i.e. they have the same purpose) these two patterns are both classified as creational.

The GoF goes on to say,

The second criterion, called scope, specifies whether the pattern applies primarily to classes or to objects.

This leads into the quote from the OP, where class and object scope are each defined. Because Factory Method's implementation focuses on inheritance (a class relationship) while Abstract Factory's implementation focuses on composition (an object relationship) these two patterns are classified under opposing scopes.

The definitions and implementations of these two patterns can be found in numerous other SO threads, so I will not repeat them here. I have also touched on the composition vs. inheritance question in these two patterns elsewhere.

like image 45
jaco0646 Avatar answered Oct 16 '22 23:10

jaco0646