feat: 桥接模式
This commit is contained in:
parent
1465f28d62
commit
c58b4b3628
Binary file not shown.
After Width: | Height: | Size: 23 KiB |
Binary file not shown.
After Width: | Height: | Size: 28 KiB |
|
@ -1517,6 +1517,119 @@ public class Proxy {
|
|||
|
||||
## 桥接模式---结构型
|
||||
|
||||
桥接模式通过“桥接”抽象和实现,使得两者可以独立地变化。它通常应用于那些有多个维度可扩展的系统中,这样可以避免类爆炸,减少子类的数量,提升代码的可维护性。
|
||||
|
||||
![image-20250205223403477](./images/设计模式-v2/image-20250205223403477.png)
|
||||
|
||||
**需要角色**
|
||||
|
||||
**Abstraction(抽象化角色)**:
|
||||
|
||||
- 定义了抽象部分的接口,并且保持一个指向实现部分的引用。
|
||||
|
||||
**RefinedAbstraction(扩充抽象化角色)**:
|
||||
|
||||
- 继承自 Abstraction,通常会扩展其接口。
|
||||
|
||||
**Implementor(实现者角色)**:
|
||||
|
||||
- 定义了实现类的接口,这个接口不会太复杂,可以和抽象角色分离。
|
||||
|
||||
**ConcreteImplementor(具体实现者角色)**:
|
||||
|
||||
- 实现了 Implementor 接口,定义了具体的实现方式。
|
||||
|
||||
### 简单示例
|
||||
|
||||
#### 示例代码
|
||||
|
||||
```java
|
||||
public class Bridge {
|
||||
public static void main(String[] args) {
|
||||
// 使用不同的绘制方式
|
||||
Shape circle1 = new Circle(5, 10, 2, new DrawingAPI1()); // 使用屏幕绘制
|
||||
Shape circle2 = new Circle(10, 20, 3, new DrawingAPI2()); // 使用打印机绘制
|
||||
|
||||
circle1.draw();
|
||||
circle2.draw();
|
||||
|
||||
// 调整圆形大小
|
||||
circle1.resize(1.5);
|
||||
circle2.resize(0.5);
|
||||
|
||||
circle1.draw();
|
||||
circle2.draw();
|
||||
}
|
||||
|
||||
// Implementor: 定义绘制图形的接口
|
||||
interface DrawingAPI {
|
||||
void drawCircle(double x, double y, double radius);
|
||||
}
|
||||
|
||||
// ConcreteImplementor: 屏幕绘制实现
|
||||
static class DrawingAPI1 implements DrawingAPI {
|
||||
@Override
|
||||
public void drawCircle(double x, double y, double radius) {
|
||||
System.out.println("Drawing Circle on Screen: (" + x + ", " + y + ") with radius " + radius);
|
||||
}
|
||||
}
|
||||
|
||||
// ConcreteImplementor: 打印机绘制实现
|
||||
static class DrawingAPI2 implements DrawingAPI {
|
||||
@Override
|
||||
public void drawCircle(double x, double y, double radius) {
|
||||
System.out.println("Drawing Circle on Printer: (" + x + ", " + y + ") with radius " + radius);
|
||||
}
|
||||
}
|
||||
|
||||
// Abstraction: 定义图形接口
|
||||
static abstract class Shape {
|
||||
protected DrawingAPI drawingAPI;
|
||||
|
||||
protected Shape(DrawingAPI drawingAPI) {
|
||||
this.drawingAPI = drawingAPI;
|
||||
}
|
||||
|
||||
public abstract void draw();
|
||||
|
||||
public abstract void resize(double factor);
|
||||
}
|
||||
|
||||
// RefinedAbstraction: 圆形
|
||||
static class Circle extends Shape {
|
||||
private final double x;
|
||||
private final double y;
|
||||
private double radius;
|
||||
|
||||
public Circle(double x, double y, double radius, DrawingAPI drawingAPI) {
|
||||
super(drawingAPI);
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw() {
|
||||
drawingAPI.drawCircle(x, y, radius);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(double factor) {
|
||||
radius *= factor;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 运行结果:
|
||||
|
||||
```java
|
||||
Drawing Circle on Screen: (5.0, 10.0) with radius 2.0
|
||||
Drawing Circle on Printer: (10.0, 20.0) with radius 3.0
|
||||
Drawing Circle on Screen: (5.0, 10.0) with radius 3.0
|
||||
Drawing Circle on Printer: (10.0, 20.0) with radius 1.5
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 策略模式---行为型
|
||||
|
@ -2716,13 +2829,197 @@ public class Iterator {
|
|||
|
||||
## 访问者模式---行为型
|
||||
|
||||
访问者模式(Visitor Pattern)是一种行为设计模式,它允许你在不修改元素类的前提下,定义新的操作。通过使用访问者模式,可以将操作和元素的结构分离,从而让你在不修改元素类的情况下,向其中添加新的操作。
|
||||
|
||||
![image-20250206163950552](./images/设计模式-v2/image-20250206163950552.png)
|
||||
|
||||
**需要角色**
|
||||
|
||||
**Element(元素)**:所有的元素类都需要实现此接口,它定义了`accept()`方法来接受访问者的访问。
|
||||
|
||||
**ConcreteElement(具体元素)**:这是实现Element接口的类,代表具体的对象,可以是任何你希望执行操作的对象。
|
||||
|
||||
**Visitor(访问者)**:这个接口定义了对所有元素类的访问方法。
|
||||
|
||||
**ConcreteVisitor(具体访问者)**:这是实现Visitor接口的类,具体执行对元素的操作。
|
||||
|
||||
**Object Structure(对象结构)**:一个包含所有元素对象的容器。它遍历所有元素对象并调用它们的`accept()`方法,传入访问者进行操作。
|
||||
|
||||
### 简单示例
|
||||
|
||||
#### 代码示例
|
||||
|
||||
```java
|
||||
public class VisitorDemo1 {
|
||||
public static void main(String[] args) {
|
||||
BusinessReport report = new BusinessReport();
|
||||
System.out.println("-------------------CEO-------------------");
|
||||
report.showReport(new CEOVisitor());
|
||||
|
||||
System.out.println("-------------------CTO-------------------");
|
||||
report.showReport(new CTOVisitor());
|
||||
}
|
||||
|
||||
// 访问者
|
||||
interface Visitor {
|
||||
/**
|
||||
* 访问者-工程师
|
||||
*/
|
||||
void visit(Engineer engineer);
|
||||
|
||||
/**
|
||||
* 访问者-经理
|
||||
*/
|
||||
void visit(Manger manger);
|
||||
}
|
||||
|
||||
// 具体访问者-CEO
|
||||
static class CEOVisitor implements Visitor {
|
||||
|
||||
/**
|
||||
* 访问者-工程师
|
||||
*
|
||||
* @param engineer 工程师
|
||||
*/
|
||||
@Override
|
||||
public void visit(Engineer engineer) {
|
||||
System.out.println("工程师:" + engineer.name + ",KPI:" + engineer.kpi);
|
||||
}
|
||||
|
||||
/**
|
||||
* 访问者-经理
|
||||
*
|
||||
* @param manger 经理
|
||||
*/
|
||||
@Override
|
||||
public void visit(Manger manger) {
|
||||
System.out.println("经理:" + manger.name + ",KPI:" + manger.kpi);
|
||||
}
|
||||
}
|
||||
|
||||
// 具体访问者-CTO
|
||||
static class CTOVisitor implements Visitor {
|
||||
|
||||
/**
|
||||
* 访问者-工程师
|
||||
*
|
||||
* @param engineer 工程师
|
||||
*/
|
||||
@Override
|
||||
public void visit(Engineer engineer) {
|
||||
System.out.println("工程师:" + engineer.name + ",代码行数:" + engineer.getCodeLines());
|
||||
}
|
||||
|
||||
/**
|
||||
* 访问者-经理
|
||||
*
|
||||
* @param manger 经理
|
||||
*/
|
||||
@Override
|
||||
public void visit(Manger manger) {
|
||||
System.out.println("经理:" + manger.name + ",产品数:" + manger.getProducts());
|
||||
}
|
||||
}
|
||||
|
||||
// 员工抽象
|
||||
static abstract class Employee {
|
||||
public String name;
|
||||
public int kpi;
|
||||
|
||||
public Employee(String name) {
|
||||
this.name = name;
|
||||
this.kpi = new Random().nextInt(10);
|
||||
}
|
||||
|
||||
// 接受访问者访问
|
||||
public abstract void accept(Visitor visitor);
|
||||
}
|
||||
|
||||
// 员工具体---工程师
|
||||
static class Engineer extends Employee {
|
||||
|
||||
public Engineer(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param visitor 访问者
|
||||
*/
|
||||
@Override
|
||||
public void accept(Visitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
|
||||
public int getCodeLines() {
|
||||
return new Random().nextInt(10000);
|
||||
}
|
||||
}
|
||||
|
||||
// 员工具体---经理
|
||||
static class Manger extends Employee {
|
||||
|
||||
public Manger(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param visitor 访问者
|
||||
*/
|
||||
@Override
|
||||
public void accept(Visitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
|
||||
public int getProducts() {
|
||||
return new Random().nextInt(10);
|
||||
}
|
||||
}
|
||||
|
||||
// 员工报表类
|
||||
static class BusinessReport {
|
||||
private final List<Employee> employeeList = new ArrayList<>();
|
||||
|
||||
public BusinessReport() {
|
||||
employeeList.add(new Manger("经理-A"));
|
||||
employeeList.add(new Manger("经理-B"));
|
||||
employeeList.add(new Manger("经理-C"));
|
||||
employeeList.add(new Manger("经理-D"));
|
||||
employeeList.add(new Manger("经理-E"));
|
||||
}
|
||||
|
||||
public void showReport(Visitor visitor) {
|
||||
for (Employee employee : employeeList) {
|
||||
employee.accept(visitor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 运行结果:
|
||||
|
||||
```java
|
||||
-------------------CEO-------------------
|
||||
经理:经理-A,KPI:9
|
||||
经理:经理-B,KPI:6
|
||||
经理:经理-C,KPI:7
|
||||
经理:经理-D,KPI:6
|
||||
经理:经理-E,KPI:9
|
||||
-------------------CTO-------------------
|
||||
经理:经理-A,产品数:7
|
||||
经理:经理-B,产品数:4
|
||||
经理:经理-C,产品数:9
|
||||
经理:经理-D,产品数:9
|
||||
经理:经理-E,产品数:2
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 解释器模式---行为型
|
||||
|
||||
---
|
||||
|
||||
## 责任链模式---行为型-未完成。。。
|
||||
## 责任链模式---行为型
|
||||
|
||||
主要有两个角色:抽象处理者(Handler)、具体处理者(Concrete Handler)
|
||||
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
package cn.bunny.pattern19;
|
||||
|
||||
public class Bridge {
|
||||
public static void main(String[] args) {
|
||||
// 使用不同的绘制方式
|
||||
Shape circle1 = new Circle(5, 10, 2, new DrawingAPI1()); // 使用屏幕绘制
|
||||
Shape circle2 = new Circle(10, 20, 3, new DrawingAPI2()); // 使用打印机绘制
|
||||
|
||||
circle1.draw();
|
||||
circle2.draw();
|
||||
|
||||
// 调整圆形大小
|
||||
circle1.resize(1.5);
|
||||
circle2.resize(0.5);
|
||||
|
||||
circle1.draw();
|
||||
circle2.draw();
|
||||
}
|
||||
|
||||
// Implementor: 定义绘制图形的接口
|
||||
interface DrawingAPI {
|
||||
void drawCircle(double x, double y, double radius);
|
||||
}
|
||||
|
||||
// ConcreteImplementor: 屏幕绘制实现
|
||||
static class DrawingAPI1 implements DrawingAPI {
|
||||
@Override
|
||||
public void drawCircle(double x, double y, double radius) {
|
||||
System.out.println("Drawing Circle on Screen: (" + x + ", " + y + ") with radius " + radius);
|
||||
}
|
||||
}
|
||||
|
||||
// ConcreteImplementor: 打印机绘制实现
|
||||
static class DrawingAPI2 implements DrawingAPI {
|
||||
@Override
|
||||
public void drawCircle(double x, double y, double radius) {
|
||||
System.out.println("Drawing Circle on Printer: (" + x + ", " + y + ") with radius " + radius);
|
||||
}
|
||||
}
|
||||
|
||||
// Abstraction: 定义图形接口
|
||||
static abstract class Shape {
|
||||
protected DrawingAPI drawingAPI;
|
||||
|
||||
protected Shape(DrawingAPI drawingAPI) {
|
||||
this.drawingAPI = drawingAPI;
|
||||
}
|
||||
|
||||
public abstract void draw();
|
||||
|
||||
public abstract void resize(double factor);
|
||||
}
|
||||
|
||||
// RefinedAbstraction: 圆形
|
||||
static class Circle extends Shape {
|
||||
private final double x;
|
||||
private final double y;
|
||||
private double radius;
|
||||
|
||||
public Circle(double x, double y, double radius, DrawingAPI drawingAPI) {
|
||||
super(drawingAPI);
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw() {
|
||||
drawingAPI.drawCircle(x, y, radius);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(double factor) {
|
||||
radius *= factor;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package cn.bunny.pattern20;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class VisitorDemo {
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
|
||||
// 访问者
|
||||
interface Visitor {
|
||||
void visit(Visitor visitor);
|
||||
}
|
||||
|
||||
// 员工抽象
|
||||
static abstract class Employee {
|
||||
public String name;
|
||||
public int kpi;
|
||||
|
||||
public Employee(String name) {
|
||||
this.name = name;
|
||||
this.kpi = new Random().nextInt(10);
|
||||
}
|
||||
|
||||
// 接受访问者访问
|
||||
public abstract void accept(Visitor visitor);
|
||||
}
|
||||
|
||||
static class Engineer extends Employee {
|
||||
|
||||
public Engineer(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param visitor 访问者
|
||||
*/
|
||||
@Override
|
||||
public void accept(Visitor visitor) {
|
||||
visitor.visit(visitor);
|
||||
}
|
||||
|
||||
public int getCodeLines() {
|
||||
return new Random().nextInt(10000);
|
||||
}
|
||||
}
|
||||
|
||||
static class Manger extends Employee {
|
||||
|
||||
public Manger(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param visitor 访问者
|
||||
*/
|
||||
@Override
|
||||
public void accept(Visitor visitor) {
|
||||
visitor.visit(visitor);
|
||||
}
|
||||
|
||||
public int getProducts() {
|
||||
return new Random().nextInt(10);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,150 @@
|
|||
package cn.bunny.pattern20;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class VisitorDemo1 {
|
||||
public static void main(String[] args) {
|
||||
BusinessReport report = new BusinessReport();
|
||||
System.out.println("-------------------CEO-------------------");
|
||||
report.showReport(new CEOVisitor());
|
||||
|
||||
System.out.println("-------------------CTO-------------------");
|
||||
report.showReport(new CTOVisitor());
|
||||
}
|
||||
|
||||
// 访问者
|
||||
interface Visitor {
|
||||
/**
|
||||
* 访问者-工程师
|
||||
*/
|
||||
void visit(Engineer engineer);
|
||||
|
||||
/**
|
||||
* 访问者-经理
|
||||
*/
|
||||
void visit(Manger manger);
|
||||
}
|
||||
|
||||
// 具体访问者-CEO
|
||||
static class CEOVisitor implements Visitor {
|
||||
|
||||
/**
|
||||
* 访问者-工程师
|
||||
*
|
||||
* @param engineer 工程师
|
||||
*/
|
||||
@Override
|
||||
public void visit(Engineer engineer) {
|
||||
System.out.println("工程师:" + engineer.name + ",KPI:" + engineer.kpi);
|
||||
}
|
||||
|
||||
/**
|
||||
* 访问者-经理
|
||||
*
|
||||
* @param manger 经理
|
||||
*/
|
||||
@Override
|
||||
public void visit(Manger manger) {
|
||||
System.out.println("经理:" + manger.name + ",KPI:" + manger.kpi);
|
||||
}
|
||||
}
|
||||
|
||||
// 具体访问者-CTO
|
||||
static class CTOVisitor implements Visitor {
|
||||
|
||||
/**
|
||||
* 访问者-工程师
|
||||
*
|
||||
* @param engineer 工程师
|
||||
*/
|
||||
@Override
|
||||
public void visit(Engineer engineer) {
|
||||
System.out.println("工程师:" + engineer.name + ",代码行数:" + engineer.getCodeLines());
|
||||
}
|
||||
|
||||
/**
|
||||
* 访问者-经理
|
||||
*
|
||||
* @param manger 经理
|
||||
*/
|
||||
@Override
|
||||
public void visit(Manger manger) {
|
||||
System.out.println("经理:" + manger.name + ",产品数:" + manger.getProducts());
|
||||
}
|
||||
}
|
||||
|
||||
// 员工抽象
|
||||
static abstract class Employee {
|
||||
public String name;
|
||||
public int kpi;
|
||||
|
||||
public Employee(String name) {
|
||||
this.name = name;
|
||||
this.kpi = new Random().nextInt(10);
|
||||
}
|
||||
|
||||
// 接受访问者访问
|
||||
public abstract void accept(Visitor visitor);
|
||||
}
|
||||
|
||||
// 员工具体---工程师
|
||||
static class Engineer extends Employee {
|
||||
|
||||
public Engineer(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param visitor 访问者
|
||||
*/
|
||||
@Override
|
||||
public void accept(Visitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
|
||||
public int getCodeLines() {
|
||||
return new Random().nextInt(10000);
|
||||
}
|
||||
}
|
||||
|
||||
// 员工具体---经理
|
||||
static class Manger extends Employee {
|
||||
|
||||
public Manger(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param visitor 访问者
|
||||
*/
|
||||
@Override
|
||||
public void accept(Visitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
|
||||
public int getProducts() {
|
||||
return new Random().nextInt(10);
|
||||
}
|
||||
}
|
||||
|
||||
// 员工报表类
|
||||
static class BusinessReport {
|
||||
private final List<Employee> employeeList = new ArrayList<>();
|
||||
|
||||
public BusinessReport() {
|
||||
employeeList.add(new Manger("经理-A"));
|
||||
employeeList.add(new Manger("经理-B"));
|
||||
employeeList.add(new Manger("经理-C"));
|
||||
employeeList.add(new Manger("经理-D"));
|
||||
employeeList.add(new Manger("经理-E"));
|
||||
}
|
||||
|
||||
public void showReport(Visitor visitor) {
|
||||
for (Employee employee : employeeList) {
|
||||
employee.accept(visitor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue