Abstract Factory pattern

Abstract Factory pattern

1.Brief:

It defines an interface Factory for a family of products which can be considered as an abstract factory. There are concrete classes like xxxFactory implement Factory. Each xxxFactory provides operations of a specific type of family of products.

2.What participates in this design pattern?
  • AbstractFactory

    • declares an interface for operations that create abstract product objects.
  • ConcreteFactory

    • implements the operations to create concrete product objects.
  • AbstractProduct

    • declares an interface for a type of product object.
  • ConcreteProduct

    • defines a product object to be created by the corresponding concrete factory.
    • implements the AbstractProduct interface.
  • Client

    • uses only interfaces declared by AbstractFactory and AbstractProduct classes.
3.Example:

The following is composed of one interface GUI and two concrete classes WinGUI and MacGUI. GUI defines two method createButton and createCheckbox. Then,two concrete factories implement them in their onw way and return their own type of products.

在这里插入图片描述

The following pic denotes the family of products and their method

在这里插入图片描述

Then, create a Client

class Application{
    private Factory factory;
    private Button button;
    private Checkbox checkbox;
    
    public Application(Factory inputFactory){
        this.factory = inputFactory;
        this.button = inputFactory.createButton();
        this.checkbox = inputFactory.createCheckbox();
    }
}

Now if we want to create an Application

public static void main{
    Factory winfac = new WinFactory();
    Factory macfac = new MacFactory();
    
    // if we want to create a Win app
    Application winApp = new Application(winfac);
    
    // if we want to create a Mac app
    Application macApp = new Application(macfac);
}
4.Where can we apply this design pattern?

I pick the description from GoF as my experience can not help me summarize them well.

Summary:

  • a system should be independent of how its products are created, composed, and represented.
  • a system should be configured with one of multiple families of products.
  • a family of related product objects is designed to be used together, and you need to enforce this constraint.
  • you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.
5.What are the advantages and disadvantages of this design pattern?

Advantages

  • It isolates the concrete classes as the factory class encapsulates the responsibility and process of creation of products which will not appear in the client code
  • It makes exchanging products families easily as it can use different product configurations simply by changing the concrete factory
  • It promotes consistency among products as it makes sure than products all come from the same family

Disadvantages

  • Supporting new kinds of products is difficult which involves changing the AbstractFactory class and all of its subclasses
  • There are too many classes and interfaces

bstractFactory class and all of its subclasses

  • There are too many classes and interfaces
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值