官术网_书友最值得收藏!

Factory design pattern

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
- GOF Design Pattern

The Factory design pattern is a creational design pattern. The Factory design pattern is also known as the Factory method design pattern. According to this design pattern, you get an object of a class without exposing the underlying logic to the client. It assigns a new object to the caller by using a common interface or abstract class. This means that the design pattern hides the actual logic of the implementation of an object, how to create it, and which class to instantiate it in. So the client shouldn't worry about creating, managing, and destroying an object-the Factory pattern takes responsibility for these tasks. The Factory pattern is one of the most-used design patterns in Java.

Let's look at the benefits of the Factory pattern:

  • The Factory pattern promotes loose coupling between collaborating components or classes by using interfaces rather than binding application-specific classes into the application code
  • Using this pattern, you can get an implementation of an object of classes that implement an interface, at runtime
  • The object life cycle is managed by the factory implemented by this pattern

Now let's discuss some common problems where you should apply the Factory design pattern:

  • This pattern removes the burden on the developer to create and manage the objects
  • This pattern removes the tight coupling between collaboration components because a component doesn't know what subclasses it will be required to create
  • Avoid hard code to create an object of the class
主站蜘蛛池模板: 浠水县| 忻州市| 淳安县| 垫江县| 盘锦市| 克拉玛依市| 同仁县| 老河口市| 桐庐县| 察雅县| 新泰市| 吉隆县| 常山县| 蕲春县| 北碚区| 雷波县| 秦皇岛市| 双江| 崇礼县| 克山县| 襄樊市| 陕西省| 枣强县| 宁城县| 金湖县| 濉溪县| 临颍县| 仁寿县| 长岛县| 珲春市| 滨州市| 嵊泗县| 五华县| 乌兰察布市| 巨鹿县| 宜君县| 武穴市| 望都县| 广安市| 泰顺县| 弥勒县|