设计模式一: 简单工厂模式 Simple Factory

  1. 总结 abstract
    通过调用工厂的公共接口, 以传递参数的低耦合方式创建实例.
    作用: 创建实例
    特点: 低耦合
    Create and return a concrete instance via calling the factory method in the client with one parameter or more, which is a lower coupling way than directly creating instances in client side.
  2. 模型图 UML diagram
    以 计算器 为例 Calculator example
  3. 关键: 解耦 critical: decoupling

高耦合例子, high coupling

//客户端承担了确认运算类型和创建实例两种任务, 且前端必须了解有多少种运算类, 以及类名等信息.
//In client side, when executing one operation, client side need to add a new line responsible for not only operation type but creating  instances, so clients must know info of possible operation class like their names.

Operation op = new AddOperation();
op.operate(1.3, 2.7);

Operation op = new SubOperation();
op.operate(1.3, 2.7);

低耦合, low coupling

//客户端承担了确认运算类型, 创建实例交给了公共的工厂方法, 客户端只需要知道工厂方法参数值.
//client chooses the operating type and leaves the instance creating job to the operation factory, it is enough for clients to know just value list of parameters.

Operation operation = OperationFactory.createOperation("+");
operation.operate(1.5, 2.7);

Operation operation = OperationFactory.createOperation("-");
operation.operate(1.5, 2.7);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值