Design Patterns Explained (6) - Abstract Factory

博客介绍了在类结构管理困难时,构造‘工厂’负责条件判断的方法。利用继承于Cond的Condi产生Ai、Bi等,使用者只需知道Cond、A、B等。还指出在问题域有不同对象族且各家族使用情况不同时适用抽象工厂模式,在Java中Class类可实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当你有这样的类结构:

A1, A2,...,An 继承于 A
B1, B2,...,Bn 继承于 B
...
K1, K2,...,Kn 继承于 K

在 Condition_i 的情况下使用 Xi, 当n>=2的时候管理将会很困难, 所以构造这样的 '工厂' 来负责 Condition :

Cond1, Cond2,...,Condn 继承于 Cond

利用 Condi, 可以产生 Ai, Bi,...,Ki, 他们的使用者只需知道 Cond, A,B,...,K 即可, 而不用知道特定的i. 在这里 Cond 就是所谓的 Abstract Factory.

Switches may indicate a need for abstraction.

Intent: You want to have families or sets of objects for particular clients.

Problem: Families of related objects need to be instantiated

Using the Abstract Factory is indicated when the problem domain has different families of objects presentan each family is used under different circumstances.

If you use Java, the Class class will do the job.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值