乐清官方网站,html代码格式,网络公司经营范围可以加技,上海设计公司招聘信息九、Bridge模式#xff1a;将类的功能层次结构与实现层次结构分离
类的两个层次结构和作用
类的功能层次结构#xff1a;希望增加新功能时
父类有基本功能#xff0c;在子类中增加新功能 Something父类 …├─SomethingGood子类 想要再增加新功能 Something父类 …├─So…九、Bridge模式将类的功能层次结构与实现层次结构分离
类的两个层次结构和作用
类的功能层次结构希望增加新功能时
父类有基本功能在子类中增加新功能 Something父类 …├─SomethingGood子类 想要再增加新功能 Something父类 …├─SomethingGood子类 … …├─SomethingBetter子类 注通常类的层次结构关系不应过深
类的实现层次结构希望增加新的实现时
回顾 Template Method模式定义了抽象类有多个子类实现。
父类通过 声明抽象方法 来 定义 接口(API) 子类通过 实现具体方法 来 实现 接口(API) AbstractClass抽象类 …├─ConcreteClass具体实现类 … …├─AnotherConcreteClass具体实现类 当类的层次结构只有一层时功能层次结构与实现层次结构是混杂在一个层次结构中的。
这样很容易使类的层次结构变得复杂难理解。因为自己难确定应该在类的哪一个层次结构中去增加子类。
因此我们需要将“类的功能层次结构”与“类的实现层次结构”分离为两个独立的类层次结构。
如果只是简单地将它们分开两者之间必然会缺少联系。所以我们需要Bridge模式在它们之间搭建一座桥梁。
示例程序类图 Display
public class Display {private DisplayImpl impl;public Display(DisplayImpl impl) {this.impl impl;}// 注意这3个方法的实现都调用了impl字段的实现方法。// 这样Display的接口(API)就被转换成为了 DisplayImpl的接口(API)。public void open() {impl.rawOpen();}public void print() {impl.rawPrint();}public void close() {impl.rawClose();}// display方法调用 open、print、Close这3个Display类的接口(API)进行了“显示”处理。public final void display() {open();print();close();}
}CountDisplay
public class CountDisplay extends Display {public CountDisplay(DisplayImpl impl) {super(impl);}// 循环显示times次public void multiDisplay(int times) {open();for (int i 0; i times; i) {print();}close();}
}StringDisplayImpl
public class StringDisplayImpl extends DisplayImpl {private String string; // 要显示的字符串private int width; // 以字节单位计算出的字符串的宽度public StringDisplayImpl(String string) { // 构造函数接收要显示的字符串stringthis.string string; // 将它保存在字段中this.width string.getBytes().length; // 把字符串的宽度也保存在字段中以供使用。}public void rawOpen() {printLine();}public void rawPrint() {System.out.println(| string |); // 前后加上|并显示}public void rawClose() {printLine();}private void printLine() {System.out.print(); // 显示用来表示方框的角的for (int i 0; i width; i) { // 显示width个-System.out.print(-); // 将其用作方框的边框}System.out.println(); // 显示用来表示方框的角的}
}Main
public class Main {public static void main(String[] args) {// 虽然变量d1中保存的是Display类的实例而变量d2和d3中保存的是CountDisplay类的实例// 但它们内部都保存着StringDisplayImp1类的实例。Display d1 new Display(new StringDisplayImpl(Hello, China.));Display d2 new CountDisplay(new StringDisplayImpl(Hello, World.));CountDisplay d3 new CountDisplay(new StringDisplayImpl(Hello, Universe.));d1.display();d2.display();d3.display();d3.multiDisplay(5);}
}角色 Abstraction抽象化 位于“类的功能层次结构”的最上层。它使用Implementor角色的方法定义了基本的功能。该角色中保存了Implementor角色的实例。 示例中是Display类。 RefinedAbstraction改善后的抽象化 在 Abstraction角色的基础上增加了新功能的角色。 示例中是CountDisplay类。 Implementor实现者 位于“类的实现层次结构”的最上层。它定义了用于实现Abstraction角色的接口(API)的方法。 示例中是DisplayImpl类。 Concretelmplementor具体实现者 负责实现在Implementor角色中定义的接口(API)。 示例中是StringDisplayImpl类。
扩展思路的要点
分开后更容易扩展
Bridge 模式的特征将“类的功能层次结构”与“类的实现层次结构”分离开。
将类的这两个层次结构分离开有利于独立地对它们进行扩展。
当想要增加功能时只需要在“类的功能层次结构”一侧增加类不必对“类的实现层次结构”做任何修改。
而且增加后的功能可被“所有的实现”使用。
继承是强关联委托是弱关联
继承是强关联关系委托是弱关联关系。
虽然使用“继承”很容易扩展类但是类之间也形成了一种强关联关系可使用“委托”来代替“继承”关系。
示例程序的Display类中使用了“委托”Display类的impl字段保存了实现的实例类的任务就发生了转移。
调用open 方法会调用impl.rawOpen()方法 调用print方法会调用impl.rawPrint()方法 调用close方法会调用impl.rawClose()方法
也就是说当其他类要求 Display类“工作”的时候Display类并非自己工作而是将工作“交给impl”。这就是“委托”。
在Template Method模式第3章中也讨论了继承和委托的关系可以再回顾下。
相关的设计模式 Template Method模式第3章 在 Template Method 模式中使用了“类的实现层次结构”。父类调用抽象方法而子类实现抽象方法。 Abstract Factory 模式第8章 为了能根据需求设计出良好的Concretelmplementor角色有时我们会使用Abstract Factory 模式。 Adapter模式第2章 使用 Bridge模式可以将类的功能层次结构与类的实现层次结构分离并在此基础上使这些层次结构结合起来。 而使用 Adapter 模式则可以结合那些功能上相似但是接口(API)不同的类。
十、Strategy模式整体地替换算法
Strategy 的意思是“策略”指的是与敌军对垒时行军作战的方法。在编程中可以将它理解为“算法”。 使用Strategy模式可以整体地替换算法的实现部分。 能够整体地替换算法可以方便地以不同的算法去解决同一个问题。
示例程序的功能是让电脑玩“猜拳”游戏。
考虑了两种猜拳的策略。
第一种策略是“如果这局猜拳获胜那么下一局也出一样的手势”(WinningStrategy)这是一种稍微有些笨的策略; 第二种策略是“根据上一局的手势从概率上计算出下一局的手势”(ProbStrategy)。
示例程序类图 Hand
Hand表示猜拳游戏中的“手势”的类 虽然Hand类会被其他类(Player类、WinningStrategy类、Probstrategy类)使用 但它并非 Strategy 模式中的角色。
public class Hand {public static final int HANDVALUE_GUU 0; // 表示石头的值public static final int HANDVALUE_CHO 1; // 表示剪刀的值public static final int HANDVALUE_PAA 2; // 表示布的值public static final Hand[] hand { // 表示猜拳中3种手势的实例new Hand(HANDVALUE_GUU),new Hand(HANDVALUE_CHO),new Hand(HANDVALUE_PAA),};private static final String[] name { // 表示猜拳中手势所对应的字符串石头, 剪刀, 布,};private int handvalue; // 表示猜拳中出的手势的值private Hand(int handvalue) {this.handvalue handvalue;}public static Hand getHand(int handvalue) { // 根据手势的值获取其对应的实例return hand[handvalue];}public boolean isStrongerThan(Hand h) { // 如果this胜了h则返回truereturn fight(h) 1;}public boolean isWeakerThan(Hand h) { // 如果this输给了h则返回truereturn fight(h) -1;}private int fight(Hand h) { // 计分平0, 胜1, 负-1if (this h) {return 0;} else if ((this.handvalue 1) % 3 h.handvalue) {return 1;} else {return -1;}}public String toString() { // 转换为手势值所对应的字符串return name[handvalue];}
}Strategy
public interface Strategy {// 获取下一局要出的手势。调用该方法后实现了strategy接口的类会绞尽脑汁想出下一局出什么手势。public abstract Hand nextHand();// 学习“上一局的手势是否获胜了”Strategy接口的实现类就会根据参数改变自己的内部状态public abstract void study(boolean win);
}WinningStrategy
import java.util.Random;public class WinningStrategy implements Strategy {private Random random;private boolean won false;// 上一局出的手势private Hand prevHand;public WinningStrategy(int seed) {random new Random(seed);}public Hand nextHand() {if (!won) {prevHand Hand.getHand(random.nextInt(3));}return prevHand;}public void study(boolean win) {won win;}
}ProbStrategy
import java.util.Random;public class ProbStrategy implements Strategy {private Random random;private int prevHandValue 0;private int currentHandValue 0;// history[上一局出的手势][这一局所出的手势]值越大表示过去的胜率越高private int[][] history {{ 1, 1, 1, },{ 1, 1, 1, },{ 1, 1, 1, },};public ProbStrategy(int seed) {random new Random(seed);}public Hand nextHand() {int bet random.nextInt(getSum(currentHandValue));int handvalue 0;if (bet history[currentHandValue][0]) {handvalue 0;} else if (bet history[currentHandValue][0] history[currentHandValue][1]) {handvalue 1;} else {handvalue 2;}prevHandValue currentHandValue;currentHandValue handvalue;return Hand.getHand(handvalue);}private int getSum(int hv) {int sum 0;for (int i 0; i 3; i) {sum history[hv][i];}return sum;}// study方法会根据nextHand方法返回的手势的胜负结果来更新history字段中的值。public void study(boolean win) {if (win) {history[prevHandValue][currentHandValue];} else {history[prevHandValue][(currentHandValue 1) % 3];history[prevHandValue][(currentHandValue 2) % 3];}}
}Player
public class Player {private String name;private Strategy strategy;// wincount、losecount 和 gamecount 用于记录选手的猜拳结果。private int wincount;private int losecount;private int gamecount;public Player(String name, Strategy strategy) { // 赋予姓名和策略this.name name;this.strategy strategy;}// 获取下一局手势的方法不过实际上决定下一局手势的是各个策略。// nextHand方法将自己的工作委托给了 Strategy这就形成了一种委托关系。public Hand nextHand() { // 策略决定下一局要出的手势return strategy.nextHand();}// Player类会通过strategy字段调用 study方法然后study 方法会改变策略的内部状态。public void win() { // 胜strategy.study(true);wincount;gamecount;}public void lose() { // 负strategy.study(false);losecount;gamecount;}public void even() { // 平gamecount;}public String toString() {return [ name : gamecount games, wincount win, losecount lose ];}
}Main
public class Main {public static void main(String[] args) {if (args.length ! 2) {System.out.println(Usage: java Main randomseed1 randomseed2);System.out.println(Example: java Main 314 15);System.exit(0);}int seed1 Integer.parseInt(args[0]);int seed2 Integer.parseInt(args[1]);// 在生成Player类的实例时需要向其传递“姓名”和“策略”。Player player1 new Player(Taro, new WinningStrategy(seed1));Player player2 new Player(Hana, new ProbStrategy(seed2));for (int i 0; i 10000; i) {Hand nextHand1 player1.nextHand();Hand nextHand2 player2.nextHand();if (nextHand1.isStrongerThan(nextHand2)) {System.out.println(Winner: player1);player1.win();player2.lose();} else if (nextHand2.isStrongerThan(nextHand1)) {System.out.println(Winner: player2);player1.lose();player2.win();} else {System.out.println(Even...);player1.even();player2.even();}}System.out.println(Total result:);System.out.println(player1.toString());System.out.println(player2.toString());}
}角色 Strategy (策略) 负责决定实现策略所必需的接口(API)。 示例中是Strategy接口。 ConcreteStrategy (具体的策略) 负责实现 Strategy角色的接口(API)即负责实现具体的策略(战略、方向、方法和算法)。 示例中是WinningStrategy类、ProbStrategy类。 Context(上下文) 负责使用 Strategy角色。Context角色保存了ConcreteStrategy角色的实例并使用ConcreteStrategy角色去实现需求总之还是要调用 Strategy角色的接口(API)。 示例中是Player类。
拓展思路的要点
为什么需要特意编写 Strategy角色
当想通过改善算法来提高算法的处理速度时如果使用了 Strategy模式仅修改ConcreteStrategy角色即可就不必修改Strategy角色的接口(API)了。
而且使用委托这种弱关联关系可以很方便地整体替换算法。
例如如果想比较原来的算法与改进后的算法的处理速度有多大区别简单地替换下算法即可进行测试。
使用 Strategy模式编写象棋程序时可以方便地根据棋手的选择切换AI例程的水平。
程序运行中也可以切换策略
如果使用 Strategy模式在程序运行中也可以切换 ConcreteStrategy角色。
例如在内存容量少的运行环境中可以使用 SlowBut LessMemoryStrategy(速度慢但省内存的策略)而在内存容量多的运行环境中则可以使用 FastButMoreMemoryStrategy(速度快但耗内存的策略)。
此外还可以用某种算法去“验算”另外一种算法。
例如假设要在某个表格计算软件的开发版本中进行复杂的计算。这时我们可以准备两种算法即“高速但计算上可能有 Bug的算法”和“低速但计算准确的算法”然后让后者去验算前者的计算结果。
相关的设计模式 Flyweight 模式第20章 有时会使用 Flyweight模式让多个地方可以共用 ConcreteStrategy 角色。 Abstract Factory 模式第8章 使用 Strategy模式可以整体地替换算法。 使用 Abstract Factory 模式则可以整体地替换具体工厂、零件和产品。 State 模式第19章 使用 Strategy模式和 State模式都可以替换被委托对象而且它们的类之间的关系也很相似但是两种模式的目的不同。 在 Strategy模式中ConcreteStrategy角色是表示算法的类并且可以替换被委托对象的类非必要也可不替换。 而在 State 模式中ConcreteState角色是表示“状态”的类并且每次状态变化时被委托对象的类都必定会被替换。