Pattern Name
Synopsis:
Factory pattern is one of most used design pattern in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
Varying: subclass of object that is instantiated.
I would like to think about design patterns in terms of classes being as ‘people,’ and patterns are ways that people talk to each other.
So, to me the factory pattern is like a hiring agency. You’ve got someone that will need a variable number of workers. This person may know some info they need in the people they hire, but that’s it.
So, when they need a new employee, they call the hiring agency and tell them what they need. Now, to actually hire someone, you need to know a lot of stuff - benefits, eligibility verification, etc. But the person hiring doesn’t need to know any of this - the hiring agency handles all of that.
In the same way, using a Factory allows the consumer to create new objects without having to know the details of how they’re created, or what their dependencies are - they only have to give the information they actually want.
Intent:
In what situations can the pattern be used, we can consider it as pattern`s context.
- creates objects without exposing the instantiation logic to the
client - refers to the newly created object through a common interface
Structure
Diagram using the Object Modeling Technique (OMT)
Implementation
: Implementation details to consider
Applicability
Participants
Classes and objects in design
Related patterns
: A list of related patterns
Motivation
: A scenario that illustrates the problem
本文深入探讨了Java中广泛使用的工厂模式,一种创建模式,通过定义对象创建的接口并让子类决定实例化哪个类,使得消费者无需了解对象的具体创建细节。文章解释了工厂模式的原理、应用场景、结构图、实现细节、适用性、参与者及关联模式,并提供了一个实际场景来说明其应用。
204

被折叠的 条评论
为什么被折叠?



