- Java EE 8 Design Patterns and Best Practices
- Rhuan Rocha Jo?o Purifica??o
- 384字
- 2021-07-23 16:54:50
Explaining Abstract Factory
Sometimes, we need to create a family of objects in a project. Imagine that we have an e-commerce and we have various kinds of products such as cell phones, notebooks, and tablets. These products are objects of the same family. If we create objects throughout a software, we will face problems if we then need to modify the initialization process of this object.
Using Abstract Factory will help us to solve problems including a system which should be independent of how its products are created, a system that should use one of the multiple families of products, and a system that should work with objects which are designed to be used together. Using this pattern will be beneficial as it isolates concrete classes. This means that with this pattern, we can control which class of objects that can be initiated on software. Furthermore, it permits the exchange of products easily and provides consistency among products.
The Abstract Factory pattern creates a single point of creation for objects and if we need to change the algorithm of object creation, we need only modify the concrete factory. In the following diagram, you can see the structure of Abstract Factory and how it is designed:

In our example, the Abstract Factory's structure has three main classes—AbstractFactory, Product, and Sale. The concrete classes of AbstractFactory are CellPhoneFactory, NotebookFactory, and TabletFactory. CellPhoneFactory is a concrete class responsible for creating the concrete classes CellphoneProduct and CellphoneSale, NotebookFactory is a concrete class responsible for creating the concrete classes NotebookProduct and NotebookSale, and the TabletFactory is a concrete class responsible for creating the concrete classes TabletProduct and TabletSale. A Client is a class responsible for using AbstractFactory to create AbstractProduct and AbstractSale. The concrete factory is created at runtime and it then creates the concrete product and sale.
The Abstract Factory pattern is sometimes used with another pattern such as Singleton, which we described earlier. Abstract Factory is a single point of creation, and often we need only one instance of it in an entire system. With this, using a Singleton pattern can help us create a design better and more efficiently.
This pattern is often used in frameworks and APIs that have a difficult creation process for an object, such as connections or sessions.
- Citrix XenApp Performance Essentials
- Linux實戰
- Ansible權威指南
- Linux網絡操作系統與實訓(第三版)
- Haskell Financial Data Modeling and Predictive Analytics
- 嵌入式應用程序設計綜合教程(微課版)
- Linux操作系統應用編程
- Windows Server 2012網絡操作系統企業應用案例詳解
- INSTANT Migration from Windows Server 2008 and 2008 R2 to 2012 How-to
- NetDevOps入門與實踐
- 跟老男孩學Linux運維:Shell編程實戰
- 新編電腦辦公(Windows 10+ Office 2013版)從入門到精通
- CentOS 6 Linux Server Cookbook
- 嵌入式微系統
- Learn OpenShift