[关闭]
@linux1s1s 2019-02-18T10:58:28.000000Z 字数 2258 阅读 1216

Base Time-Design Patterns-Factory Method

Base 2017-01


需求

面向过程

这个需求很简单,我们随手写个类,里面给个方法即可。

Calculator.java

  1. public class Calculator {
  2. public float execute(float x, float y, String operation) {
  3. if (TextUtils.isEmpty(operation))
  4. return 0;
  5. float result;
  6. if ("+".equals(operation)) {
  7. result = x + y;
  8. } else if ("-".equals(operation)) {
  9. result = x - y;
  10. } else if ("*".equals(operation)) {
  11. result = x * y;
  12. } else if ("/".equals(operation)) {
  13. if (y == 0)
  14. result = 0;
  15. else
  16. result = x / y;
  17. } else {
  18. result = 0;
  19. }
  20. return result;
  21. }
  22. }

上面的程序将计算器的代码封装到一个方法中,供客户端调用,这样如果存在多个客户端,只需要调用这个方法即可,实现了代码的可复用。那么现在我们把这个工具类编译后,其他人就可以使用了,假如说现在需要添加一个新算法,求A的B次方,我们就需要修改这个类的源代码,在getResult中加入新的分支,然后重新编译,供客户端使用,难扩展

静态工厂方法

为了解决上面的问题,我们尝试用静态工厂方法去重构。

UML类图:

此处输入图片的描述

源代码:

接口Operation.java

  1. public interface Operation {
  2. float execute(float x, float y);
  3. }

实现Operation.java的具体实现子类
AddOperation.java

  1. public class AddOperation implements Operation {
  2. @Override
  3. public float execute(float x, float y) {
  4. return x + y;
  5. }
  6. }

SubOperation.java

  1. public class SubOperation implements Operation {
  2. @Override
  3. public float execute(float x, float y) {
  4. return x - y;
  5. }
  6. }

MultipleOperation.java

  1. public class MultipleOperation implements Operation {
  2. @Override
  3. public float execute(float x, float y) {
  4. return x * y;
  5. }
  6. }

DivOperation.java

  1. public class DivOperation implements Operation {
  2. @Override
  3. public float execute(float x, float y) {
  4. return x / y;
  5. }
  6. }

如果以后想增加中算法,那么只新增算法实现类,并继承接口Operation.java即可,无须修改其他的算法类。

接下来就是静态工程类的实现。
OperationFactory.java

  1. public class OperationFactory {
  2. public static Operation createOperation(String op) {
  3. if (TextUtils.isEmpty(op))
  4. return null;
  5. Operation operation;
  6. if ("+".equals(op)) {
  7. operation = new AddOperation();
  8. } else if ("-".equals(op)) {
  9. operation = new SubOperation();
  10. } else if ("*".equals(op)) {
  11. operation = new MultipleOperation();
  12. } else if ("/".equals(op)) {
  13. operation = new DivOperation();
  14. } else {
  15. operation = null;
  16. }
  17. return operation;
  18. }
  19. }

最后看一下TestCase.java

  1. public class TestCase {
  2. public static void main(String[] args) {
  3. float x = 3;
  4. float y = 4;
  5. String operation = "-";
  6. Operation o = OperationFactory.createOperation(operation);
  7. if (o != null) {
  8. o.execute(x, y);
  9. } else {
  10. Log.e("factory", "Operation illegal");
  11. }
  12. }
  13. }

小结

将创建对象的工作交给工厂负责,这样调用者不再和算法直接接触,有效的将使用和实现隔离开,并做到了可复用、可维护、可扩展。

我们知道面向对象有10大设计原则,其中之一是开闭原则

即对扩展开放,对修改关闭。这是另一种非常棒的设计原则,可以防止其他人更改已经测试好的代码。理论上,可以在不修改原有的模块的基础上,扩展功能。这也是开闭原则的宗旨。

如果现在有个需求,增加一种算法实现,那么不得不修改静态工厂类,并在createOperation()方法处新增一种算法的实例,很显然违背了上面的开闭原则,那么应该怎么解决这个问题呢,我们来看下面一篇博文Base Time-Design Patterns-Abstract Factory Method

参考博文:
简单工厂模式 参考和部分修改,特此说明。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注