feat: 适配器模式
This commit is contained in:
parent
36c484cde2
commit
99b9b86bfa
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
Binary file not shown.
After Width: | Height: | Size: 20 KiB |
Binary file not shown.
|
@ -664,6 +664,85 @@ public class BakeDemo {
|
|||
|
||||
## 工厂模式
|
||||
|
||||
### 工厂模式(Factory Pattern)介绍
|
||||
|
||||
工厂模式属于创建型设计模式,主要用于通过定义一个接口来创建对象,但让子类决定实例化哪一个类。通过这种方式,工厂模式将对象的创建与使用分离,从而解耦了客户端与具体类的依赖关系。
|
||||
|
||||
工厂模式的核心思想是:**让子类决定具体要创建哪一个产品,而不是直接在客户端代码中硬编码要使用哪个类**。
|
||||
|
||||
### 工厂模式的主要角色
|
||||
|
||||
1. **产品接口(Product)**:通常是一个接口或者抽象类,定义了具体产品的公共方法。
|
||||
2. **具体产品(ConcreteProduct)**:实现了产品接口的具体类,代表不同的产品。
|
||||
3. **工厂接口(Creator)**:通常是一个抽象类或接口,定义了一个工厂方法用于创建产品。
|
||||
4. **具体工厂(ConcreteCreator)**:实现了工厂接口的具体类,负责创建具体的产品对象。
|
||||
|
||||
### 工厂模式的类型
|
||||
|
||||
1. **简单工厂模式(Simple Factory)**:通过工厂类中的一个方法,根据传入的参数决定返回哪种具体的产品对象。
|
||||
2. **工厂方法模式(Factory Method)**:通过一个工厂接口或者抽象类来定义创建产品的工厂方法,让子类决定创建哪个具体产品。
|
||||
3. **抽象工厂模式(Abstract Factory)**:提供一个创建一系列相关或相互依赖对象的接口,而无需指定具体类。
|
||||
|
||||
### 简单工厂模式示例(Java)
|
||||
|
||||
```java
|
||||
// 产品接口
|
||||
public interface Product {
|
||||
void create();
|
||||
}
|
||||
|
||||
// 具体产品A
|
||||
public class ProductA implements Product {
|
||||
@Override
|
||||
public void create() {
|
||||
System.out.println("Product A created.");
|
||||
}
|
||||
}
|
||||
|
||||
// 具体产品B
|
||||
public class ProductB implements Product {
|
||||
@Override
|
||||
public void create() {
|
||||
System.out.println("Product B created.");
|
||||
}
|
||||
}
|
||||
|
||||
// 工厂类
|
||||
public class ProductFactory {
|
||||
// 简单工厂方法,根据输入参数创建不同的产品
|
||||
public static Product createProduct(String productType) {
|
||||
if ("A".equals(productType)) {
|
||||
return new ProductA();
|
||||
} else if ("B".equals(productType)) {
|
||||
return new ProductB();
|
||||
}
|
||||
throw new IllegalArgumentException("Unknown product type");
|
||||
}
|
||||
}
|
||||
|
||||
// 客户端代码
|
||||
public class Client {
|
||||
public static void main(String[] args) {
|
||||
Product productA = ProductFactory.createProduct("A");
|
||||
productA.create();
|
||||
|
||||
Product productB = ProductFactory.createProduct("B");
|
||||
productB.create();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 优点
|
||||
|
||||
1. **降低耦合度**:工厂模式将对象的创建过程与使用过程分开,客户端不需要了解具体的创建过程,从而降低了耦合度。
|
||||
2. **易于扩展**:可以通过新增具体产品类和工厂方法来扩展系统,不需要修改现有代码,符合开闭原则(OCP)。
|
||||
3. **提高代码的可维护性**:当需要修改创建产品的逻辑时,只需修改工厂类,不影响客户端代码。
|
||||
|
||||
### 缺点
|
||||
|
||||
1. **增加类的数量**:引入了工厂类、产品接口和多个具体产品类,可能导致类的数量增加,系统变得复杂。
|
||||
2. **难以理解和维护**:对于一些简单的应用程序,使用工厂模式可能会使得设计过于复杂,反而增加了理解和维护的难度。
|
||||
|
||||
---
|
||||
|
||||
## 单例模式
|
||||
|
@ -963,6 +1042,323 @@ CommandDemo.Computer(CPU=High CPU, RAM=High RAM, hardDrive=High Driver)
|
|||
|
||||
---
|
||||
|
||||
## 适配器模式
|
||||
|
||||
需要角色:抽象适配者角色(Abstract Adapter)、适配者角色(Adapter)、目标角色(Target)
|
||||
|
||||
在生活中,有时需要将380V三相电转成220V,这时候需要变压器,当然也有三相电转两相电接法,我们可以使用代码模拟这个操作 。
|
||||
|
||||
![](./images/设计模式-v2/v2-8efc399313ae3746932c63069aa103b0_1440w-1738507407103-4.avif)
|
||||
|
||||
### 类适配器
|
||||
|
||||
![image-20250202222740030](./images/设计模式-v2/image-20250202222740030.png)
|
||||
|
||||
#### 示例代码
|
||||
|
||||
```java
|
||||
public class Adapter1 {
|
||||
public static void main(String[] args) {
|
||||
PowerAdapter powerAdapter = new PowerAdapter();
|
||||
Integer integer = powerAdapter.output220V();
|
||||
|
||||
System.out.println("----------------");
|
||||
|
||||
PowerAdapter powerAdapter1 = new PowerAdapter();
|
||||
powerAdapter1.output220V();
|
||||
Integer i = powerAdapter1.output380V();
|
||||
}
|
||||
|
||||
// 电压输出
|
||||
interface PowerTarget {
|
||||
/**
|
||||
* 输出电压220V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
Integer output220V();
|
||||
}
|
||||
|
||||
static class PowerAdapter extends Power implements PowerTarget {
|
||||
|
||||
/**
|
||||
* 输出电压220V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
@Override
|
||||
public Integer output220V() {
|
||||
Integer output = super.getOutput();
|
||||
System.out.println("变压器交流" + output + "v转220v三相电变两项电。。。");
|
||||
|
||||
double adapter = new Random().nextDouble(1.64, 1.72);
|
||||
Integer newOutput = (int) (output / adapter);
|
||||
System.out.println("变压器转换完成:电压输出为:" + newOutput);
|
||||
|
||||
return newOutput;
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
static class Power {
|
||||
private Integer output = 380;
|
||||
|
||||
public Integer output380V() {
|
||||
System.out.println("输出电压:" + output);
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 运行结果:
|
||||
|
||||
```java
|
||||
变压器交流380v转220v三相电变两项电。。。
|
||||
变压器转换完成:电压输出为:224
|
||||
----------------
|
||||
变压器交流380v转220v三相电变两项电。。。
|
||||
变压器转换完成:电压输出为:224
|
||||
输出电压:380
|
||||
```
|
||||
|
||||
### 对象适配器
|
||||
|
||||
![image-20250202223220831](./images/设计模式-v2/image-20250202223220831.png)
|
||||
|
||||
#### 示例代码
|
||||
|
||||
```java
|
||||
public class Adapter2 {
|
||||
public static void main(String[] args) {
|
||||
Power power = new Power();
|
||||
PowerAdapter powerAdapter = new PowerAdapter(power);
|
||||
Integer integer = powerAdapter.output220V();
|
||||
}
|
||||
|
||||
// 电压输出
|
||||
interface PowerTarget {
|
||||
/**
|
||||
* 输出电压220V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
Integer output220V();
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
static class PowerAdapter implements PowerTarget {
|
||||
private final Power power;
|
||||
|
||||
public PowerAdapter(Power power) {
|
||||
this.power = power;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出电压220V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
@Override
|
||||
public Integer output220V() {
|
||||
Integer output = power.getOutput();
|
||||
System.out.println("变压器交流" + output + "v转220v三相电变两项电。。。");
|
||||
|
||||
double adapter = new Random().nextDouble(1.64, 1.72);
|
||||
Integer newOutput = (int) (output / adapter);
|
||||
System.out.println("变压器转换完成:电压输出为:" + newOutput);
|
||||
|
||||
return newOutput;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
static class Power {
|
||||
private Integer output = 380;
|
||||
|
||||
public Integer output380V() {
|
||||
System.out.println("输出电压:" + output);
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 运行结果 :
|
||||
|
||||
```java
|
||||
变压器交流380v转220v三相电变两项电。。。
|
||||
变压器转换完成:电压输出为:224
|
||||
```
|
||||
|
||||
### 缺省适配器
|
||||
|
||||
![image-20250202224211540](./images/设计模式-v2/image-20250202224211540.png)
|
||||
|
||||
#### 示例代码
|
||||
|
||||
```java
|
||||
public class Adapter3 {
|
||||
public static void main(String[] args) {
|
||||
Power power = new Power();
|
||||
PowerAdapter powerAdapter = new Power220VAdapter(power);
|
||||
Integer integer = powerAdapter.output220V();
|
||||
}
|
||||
|
||||
// 电压输出
|
||||
interface PowerTarget {
|
||||
/**
|
||||
* 输出电压24V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
Integer output24V();
|
||||
|
||||
/**
|
||||
* 输出电压110V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
Integer output110V();
|
||||
|
||||
/**
|
||||
* 输出电压220V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
Integer output220V();
|
||||
}
|
||||
|
||||
@Data
|
||||
static abstract class PowerAdapter implements PowerTarget {
|
||||
protected Power power;
|
||||
|
||||
public PowerAdapter(Power power) {
|
||||
this.power = power;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出电压24V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
@Override
|
||||
public Integer output24V() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出电压110V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
@Override
|
||||
public Integer output110V() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出电压220V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
@Override
|
||||
public Integer output220V() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 缺省适配器转换器
|
||||
static class Power220VAdapter extends PowerAdapter {
|
||||
public Power220VAdapter(Power power) {
|
||||
super(power);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出电压220V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
@Override
|
||||
public Integer output220V() {
|
||||
Integer output = power.getOutput();
|
||||
System.out.println("变压器开始转换:" + output + "v转220v三相电变两项电。。。");
|
||||
|
||||
double adapter = new Random().nextDouble(1.64, 1.72);
|
||||
Integer newOutput = (int) (output / adapter);
|
||||
System.out.println("变压器转换完成:电压输出为:" + newOutput);
|
||||
|
||||
return newOutput;
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
static class Power {
|
||||
private Integer output = 380;
|
||||
|
||||
public Integer output380V() {
|
||||
System.out.println("输出电压:" + output);
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 运行结果:
|
||||
|
||||
```java
|
||||
变压器开始转换:380v转220v三相电变两项电。。。
|
||||
变压器转换完成:电压输出为:224
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 外观模式
|
||||
|
||||
---
|
||||
|
||||
## 组合模式
|
||||
|
||||
---
|
||||
|
||||
## 状态模式
|
||||
|
||||
---
|
||||
|
||||
## 模板方法模式
|
||||
|
||||
---
|
||||
|
||||
## 备忘录模式
|
||||
|
||||
---
|
||||
|
||||
## 中介者模式
|
||||
|
||||
---
|
||||
|
||||
## 迭代器模式
|
||||
|
||||
---
|
||||
|
||||
## 访问者模式
|
||||
|
||||
---
|
||||
|
||||
## 解释器模式
|
||||
|
||||
---
|
||||
|
||||
## 享元模式
|
||||
|
||||
---
|
||||
|
||||
## 代理模式
|
||||
|
||||
---
|
||||
|
||||
## 原型模式
|
||||
|
||||
![image-20250202214232289](./images/设计模式-v2/image-20250202214232289.png)
|
||||
|
@ -1156,6 +1552,12 @@ false
|
|||
false
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 桥接模式
|
||||
|
||||
|
||||
|
||||
---
|
||||
|
||||
## 责任链模式-未完成。。。
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
package cn.bunny.pattern9;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Adapter1 {
|
||||
public static void main(String[] args) {
|
||||
PowerAdapter powerAdapter = new PowerAdapter();
|
||||
Integer integer = powerAdapter.output220V();
|
||||
|
||||
System.out.println("----------------");
|
||||
|
||||
PowerAdapter powerAdapter1 = new PowerAdapter();
|
||||
powerAdapter1.output220V();
|
||||
Integer i = powerAdapter1.output380V();
|
||||
}
|
||||
|
||||
// 电压输出
|
||||
interface PowerTarget {
|
||||
/**
|
||||
* 输出电压220V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
Integer output220V();
|
||||
}
|
||||
|
||||
static class PowerAdapter extends Power implements PowerTarget {
|
||||
|
||||
/**
|
||||
* 输出电压220V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
@Override
|
||||
public Integer output220V() {
|
||||
Integer output = super.getOutput();
|
||||
System.out.println("变压器交流" + output + "v转220v三相电变两项电。。。");
|
||||
|
||||
double adapter = new Random().nextDouble(1.64, 1.72);
|
||||
Integer newOutput = (int) (output / adapter);
|
||||
System.out.println("变压器转换完成:电压输出为:" + newOutput);
|
||||
|
||||
return newOutput;
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
static class Power {
|
||||
private Integer output = 380;
|
||||
|
||||
public Integer output380V() {
|
||||
System.out.println("输出电压:" + output);
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package cn.bunny.pattern9;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Adapter2 {
|
||||
public static void main(String[] args) {
|
||||
Power power = new Power();
|
||||
PowerAdapter powerAdapter = new PowerAdapter(power);
|
||||
Integer integer = powerAdapter.output220V();
|
||||
}
|
||||
|
||||
// 电压输出
|
||||
interface PowerTarget {
|
||||
/**
|
||||
* 输出电压220V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
Integer output220V();
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
static class PowerAdapter implements PowerTarget {
|
||||
private final Power power;
|
||||
|
||||
public PowerAdapter(Power power) {
|
||||
this.power = power;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出电压220V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
@Override
|
||||
public Integer output220V() {
|
||||
Integer output = power.getOutput();
|
||||
System.out.println("变压器交流" + output + "v转220v三相电变两项电。。。");
|
||||
|
||||
double adapter = new Random().nextDouble(1.64, 1.72);
|
||||
Integer newOutput = (int) (output / adapter);
|
||||
System.out.println("变压器转换完成:电压输出为:" + newOutput);
|
||||
|
||||
return newOutput;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
static class Power {
|
||||
private Integer output = 380;
|
||||
|
||||
public Integer output380V() {
|
||||
System.out.println("输出电压:" + output);
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
package cn.bunny.pattern9;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Adapter3 {
|
||||
public static void main(String[] args) {
|
||||
Power power = new Power();
|
||||
PowerAdapter powerAdapter = new Power220VAdapter(power);
|
||||
Integer integer = powerAdapter.output220V();
|
||||
}
|
||||
|
||||
// 电压输出
|
||||
interface PowerTarget {
|
||||
/**
|
||||
* 输出电压24V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
Integer output24V();
|
||||
|
||||
/**
|
||||
* 输出电压110V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
Integer output110V();
|
||||
|
||||
/**
|
||||
* 输出电压220V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
Integer output220V();
|
||||
}
|
||||
|
||||
@Data
|
||||
static abstract class PowerAdapter implements PowerTarget {
|
||||
protected Power power;
|
||||
|
||||
public PowerAdapter(Power power) {
|
||||
this.power = power;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出电压24V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
@Override
|
||||
public Integer output24V() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出电压110V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
@Override
|
||||
public Integer output110V() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出电压220V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
@Override
|
||||
public Integer output220V() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 缺省适配器转换器
|
||||
static class Power220VAdapter extends PowerAdapter {
|
||||
public Power220VAdapter(Power power) {
|
||||
super(power);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出电压220V
|
||||
*
|
||||
* @return 输出电压
|
||||
*/
|
||||
@Override
|
||||
public Integer output220V() {
|
||||
Integer output = power.getOutput();
|
||||
System.out.println("变压器开始转换:" + output + "v转220v三相电变两项电。。。");
|
||||
|
||||
double adapter = new Random().nextDouble(1.64, 1.72);
|
||||
Integer newOutput = (int) (output / adapter);
|
||||
System.out.println("变压器转换完成:电压输出为:" + newOutput);
|
||||
|
||||
return newOutput;
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
static class Power {
|
||||
private Integer output = 380;
|
||||
|
||||
public Integer output380V() {
|
||||
System.out.println("输出电压:" + output);
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package cn.bunny.pattern9;
|
||||
|
||||
public class PowerAdapterImpl extends Adapter3.PowerAdapter {
|
||||
public PowerAdapterImpl(Adapter3.Power power) {
|
||||
super(power);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue