auto commit
This commit is contained in:
parent
d361bc608a
commit
c41be7dfc8
142
notes/设计模式.md
142
notes/设计模式.md
|
@ -2349,6 +2349,148 @@ public class Client {
|
|||
|
||||
将抽象与实现分离开来,使它们可以独立变化。
|
||||
|
||||
### 类图
|
||||
|
||||
- Abstraction:定义抽象类的接口
|
||||
- Implementor:定义实现类接口
|
||||
|
||||
<div align="center"> <img src="../pics//c2cbf5d2-82af-4c78-bd43-495da5adf55f.png"/> </div><br>
|
||||
|
||||
### 实现
|
||||
|
||||
RemoteControl 表示遥控器,指代 Abstraction。
|
||||
|
||||
TV 表示电视,指代 Implementor。
|
||||
|
||||
桥接模式将遥控器和电视分离开来,从而可以独立改变遥控器或者电视的实现。
|
||||
|
||||
```java
|
||||
public abstract class TV {
|
||||
public abstract void on();
|
||||
|
||||
public abstract void off();
|
||||
|
||||
public abstract void tuneChannel();
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class Sony extends TV{
|
||||
@Override
|
||||
public void on() {
|
||||
System.out.println("Sony.on()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void off() {
|
||||
System.out.println("Sony.off()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tuneChannel() {
|
||||
System.out.println("Sony.tuneChannel()");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class RCA extends TV{
|
||||
@Override
|
||||
public void on() {
|
||||
System.out.println("RCA.on()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void off() {
|
||||
System.out.println("RCA.off()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tuneChannel() {
|
||||
System.out.println("RCA.tuneChannel()");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public abstract class RemoteControl {
|
||||
protected TV tv;
|
||||
|
||||
public RemoteControl(TV tv) {
|
||||
this.tv = tv;
|
||||
}
|
||||
|
||||
public abstract void on();
|
||||
|
||||
public abstract void off();
|
||||
|
||||
public abstract void tuneChannel();
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class ConcreteRemoteControl1 extends RemoteControl {
|
||||
public ConcreteRemoteControl1(TV tv) {
|
||||
super(tv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void on() {
|
||||
System.out.println("ConcreteRemoteControl1.on()");
|
||||
tv.on();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void off() {
|
||||
System.out.println("ConcreteRemoteControl1.off()");
|
||||
tv.off();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tuneChannel() {
|
||||
System.out.println("ConcreteRemoteControl1.tuneChannel()");
|
||||
tv.tuneChannel();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class ConcreteRemoteControl2 extends RemoteControl {
|
||||
public ConcreteRemoteControl2(TV tv) {
|
||||
super(tv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void on() {
|
||||
System.out.println("ConcreteRemoteControl2.on()");
|
||||
tv.on();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void off() {
|
||||
System.out.println("ConcreteRemoteControl2.off()");
|
||||
tv.off();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tuneChannel() {
|
||||
System.out.println("ConcreteRemoteControl2.tuneChannel()");
|
||||
tv.tuneChannel();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class Client {
|
||||
public static void main(String[] args) {
|
||||
RemoteControl remoteControl1 = new ConcreteRemoteControl1(new RCA());
|
||||
remoteControl1.on();
|
||||
remoteControl1.off();
|
||||
remoteControl1.tuneChannel();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### JDK
|
||||
|
||||
- AWT (It provides an abstraction layer which maps onto the native OS the windowing support.)
|
||||
|
|
BIN
pics/c2cbf5d2-82af-4c78-bd43-495da5adf55f.png
Normal file
BIN
pics/c2cbf5d2-82af-4c78-bd43-495da5adf55f.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
Loading…
Reference in New Issue
Block a user