01-01-01 Factory Design Pattern

本文深入探讨了Java中广泛使用的工厂模式,一种创建模式,通过定义对象创建的接口并让子类决定实例化哪个类,使得消费者无需了解对象的具体创建细节。文章解释了工厂模式的原理、应用场景、结构图、实现细节、适用性、参与者及关联模式,并提供了一个实际场景来说明其应用。

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.

  1. creates objects without exposing the instantiation logic to the
    client
  2. 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

: A list of related patterns

Motivation

: A scenario that illustrates the problem

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值