📄 client.java
字号:
/*
* Created on 2005-5-11
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.test.pattern.struct.bridge;
/**
* @author Administrator
*
* 意图 将抽象部分与它的实现部分分离,
* 使它们都可以独立地变化。
* 适用性
* 你不希望在抽象和它的实现部分之间有一个
* 固定的绑定关系。例如这种情况可能是因为,
* 在程序运行时刻实现部分应可以被选择或者切换。
* 类的抽象以及它的实现都应该可以通过生成子类
* 的方法加以扩充。
* 这时B r i d g e模式使你可以对不同的抽象接口
* 和实现部分进行组合,并分别对它们进行扩充。
* 对一个抽象的实现部分的修改应对客户不产生影响,
* 即客户的代码不必重新编译。
* (C ++)你想对客户完全隐藏抽象的实现部分。
* 在C + +中,类的表示在类接口中是可见的。
* 有许多类要生成。这样一种类层次结构说明你必须
* 将一个对象分解成两个部分。
* R u m b a u g h 称这种类层次结构为“嵌套的普化”
* (nested generalizations )。 你想在多个对象
* 间共享实现(可能使用引用计数),但同时要求客户并
* 不知道这一点。一个简单的例子便是Coplien 的
* String 类[ Cop92 ],在这个类中多个对象可以共享
* 同一个字符串表示(StringRep )。
*/
class Abstraction {
protected Implementation impToUse;
public void SetImplementation(Implementation i) {
impToUse = i;
}
public void DumpString(String str) {
impToUse.DoStringOp(str);
}
}
class DerivedAbstraction_One extends Abstraction {
public void DumpString(String str) {
str += ".com";
impToUse.DoStringOp(str);
}
}
class Implementation {
public void DoStringOp(String str) {
System.out.println("Standard implementation - print string as is");
System.out.println("string = " + str);
}
}
class DerivedImplementation_One extends Implementation {
public void DoStringOp(String str) {
System.out.println("DerivedImplementation_One - don't print string");
}
}
class DerivedImplementation_Two extends Implementation {
public void DoStringOp(String str) {
System.out.println("DerivedImplementation_Two - print string twice");
System.out.println("string = " + str);
System.out.println("string = " + str);
}
}
/// <summary>
/// Summary description for Client.
/// </summary>
public class Client {
Abstraction SetupMyParticularAbstraction() {
// we localize to this method the decision which abstraction and
// which implementation to use. These need to be decided
// somewhere and we do it here. All teh rest of the client
// code can work against the abstraction object.
Abstraction a = new DerivedAbstraction_One();
a.SetImplementation(new DerivedImplementation_Two());
return a;
}
public static void main(String[] args) {
Client c = new Client();
Abstraction a = c.SetupMyParticularAbstraction();
// From here on client code thinks it is talking to the
// abstraction, and will not need to be changed as
// derived abstractions are changed.
// more client code using the abstraction goes here
// . . .
a.DumpString("Clipcode");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -