- Design Patterns and Best Practices in Java
- Kamalmeet Singh Adrian Ianculescu LUCIAN PAUL TORJE
- 115字
- 2021-06-25 20:52:33
Simple factory pattern
The factory pattern is used to encapsulate the logic to instantiate objects referred through a common interface. New classes can be added with minimal changes.
The implementation of a simple factory is described in the following class diagram:

The SimpleFactory class implements the code to instantiate ConcreteProduct1 and ConcreteProduct2. When the client needs an object, it calls the createProduct() method of the SimpleFactory with the parameter indicating the type of object it requires. SimpleFactory instantiates the corresponding concrete product and returns it. The returned product is cast to the base class type so the client will handle any Product in the same way, regardless of whether it is a ConcreteProduct1 or ConcreteProduct2.
推薦閱讀
- HornetQ Messaging Developer’s Guide
- 程序員修煉之道:程序設(shè)計(jì)入門30講
- Java多線程編程實(shí)戰(zhàn)指南:設(shè)計(jì)模式篇(第2版)
- 算法基礎(chǔ):打開程序設(shè)計(jì)之門
- 精通搜索分析
- QTP自動(dòng)化測(cè)試進(jìn)階
- Visual Basic程序設(shè)計(jì)
- Create React App 2 Quick Start Guide
- Android應(yīng)用開發(fā)深入學(xué)習(xí)實(shí)錄
- 大學(xué)計(jì)算機(jī)應(yīng)用基礎(chǔ)(Windows 7+Office 2010)(IC3)
- 軟件測(cè)試(慕課版)
- Java EE實(shí)用教程
- Developing Java Applications with Spring and Spring Boot
- Head First Kotlin程序設(shè)計(jì)
- Bitcoin Essentials