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

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
主站蜘蛛池模板: 瓮安县| 阳城县| 杭锦后旗| 贡山| 固镇县| 澄江县| 景德镇市| 岐山县| 云林县| 余江县| 蕉岭县| 泽库县| 陵水| 如东县| 闽侯县| 安义县| 都昌县| 永新县| 平江县| 平度市| 当涂县| 安达市| 江源县| 三穗县| 长宁县| 衡南县| 连云港市| 江源县| 晋城| 五华县| 东港市| 四川省| 大姚县| 娄底市| 台东市| 昌乐县| 辽阳县| 肇庆市| 涡阳县| 峨眉山市| 金华市|