Differentiating between Factory Method and Abstract Factory

I’ve been confused for a while about the differences between the patterns Factory Method and Abstract Factory. Been doing a lot of research, still confused.

I have one question:

Is the only difference between the two patterns, is that Factory Method produces one object, and Abstract Factory produces a family of objects? Or are there more differences?

I read a lot on this site and the web about this, only confused me more. I know how to use the patterns, but am having trouble differentiating between the two.

A factory method is essentially just a constructor with another name. This can be useful e.g. to guarantee correct initialization, registration, or to supply default values without having to subclass the product:

private List<WeakReference<Product>> products = ...;

public Product makeProduct(Param x) {
    Product instance = new Product(x, "default value");
    instance.initialize(42);
    products.add(new WeakReference<>(instance));
    return instance;
}

An abstract factory pattern is usually implemented in terms of factory methods, but here the aim is to leave the concrete type unspecified until runtime. So we have a set of products:

interface Product {}
class ConcreteProduct1 implements Product {}
class ConcreteProduct2 implements Product {}

And we have an abstract factory:

interface Factory {
    public Product makeProduct();
}

Such a factory might be used by client code: factory.makeProduct(). Which concrete product is used is delayed to runtime.

class ConcreteFactory1 implements Factory {
    public Product makeProduct() { return new ConcreteProduct1(); }
}

class ConcreteFactory2 implements Factory {
    public Product makeProduct() { return new ConcreteProduct2(); }
}

Typically, an abstract factory will have multiple factory methods for a set of related types, you can think of a factory instance as a “theme”. Abstract factories are also useful for dependency injection.

The factory method pattern is a combination of factory methods with the strategy pattern. Here the idea is that our class needs to create some instance, but will let subclasses override the choice of the instance. In other words, the client and the factory are the same.

class DomainSpecificStuff {
    public doDomainSpecificStuff() {
        Product p = this.makeProduct();
        p.frobnicate();
    }

    protected makeProduct() {
        return new  ConcreteProduct1();
    }
}

class OtherDomainSpecificStuff {
    @Override
    protected makeProduct() {
        return new ConcreteProduct2();
    }
}

So the strategy pattern allows for limited dependency injection through the use of inheritance when used as the FMP.

So what are the useful distinctions between the factory method pattern and abstract factory pattern?

  • Client: The client of the factory method in the FMP is the factory itself – it’s just a spin on the strategy pattern, after all. The client of the AFP is some other class.

  • Number of Objects: Typically, the FMP will only have a single factory method, whereas the AFP can be used to produce multiple related objects. It is not the case that an AFP must have at least two factory methods, although the creation of related, interdependent objects is where the AFP shines.

  • Intent: The AFP is a collection of related factory methods which can be passed around in client code. The FMP allows subclasses to swap out a dependency.

7

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật