适配器模式 + 装饰器模式 + 享元模式

[Musings]

  1. 今天回看工厂模式和抽象工厂模式,发现了其实本质上所有button/textbox抽象类和实现类都是通用的,唯独的不同在于工厂的性质变了,一个注重垂类产品,一个注重品牌哈哈哈

  2. 开始结构型的设计模式了

Adapter Pattern

题目是不同国家的充电头电压适配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

//USBTarget.interface
public interface USBTarget {
void charge5V();
}

//AmericanCharger
public class AmericanCharger {
public void supply110V(){
System.out.println("American Charger, output 110V");
}
}

//ChineseCharger
public class ChineseCharger{
public void supply220V(){
System.out.println("Chinese Charger, output 220V");
}
}

//AmericanAdapter(核心)
public class AmericanAdapter implements USBTarget {
private AmericanCharger americanCharger;

public AmericanAdapter(AmericanCharger americanCharger) {
this.americanCharger = americanCharger;
}

@Override
public void charge5V() {
americanCharger.supply110V();
System.out.println("American Adapter, output 5V!");
}
}

//ChineseAdapter(核心)
public class ChineseAdapter implements USBTarget {
private ChineseCharger chineseCharger;

public ChineseAdapter(ChineseCharger chineseCharger) {
this.chineseCharger = chineseCharger;
}

@Override
public void charge5V() {
chineseCharger.supply220V();
System.out.println("Chinese Adapter, output 5V!");
}
}

//AdapterType
enum AdapterType {
CHINESE, AMERICAN
}

//AdapterFactory
public class AdapterFactory {
private AdapterFactory() {
}//私有化实例

public static USBTarget createChineseAdapter() {
return new ChineseAdapter(new ChineseCharger());
}

public static USBTarget createAmericanAdapter() {
return new AmericanAdapter(new AmericanCharger());
}
}

//Phone.class
public class Phone {
public static USBTarget withAdapter(AdapterType adapterType) {
return switch (adapterType) {
case CHINESE -> AdapterFactory.createChineseAdapter();
case AMERICAN -> AdapterFactory.createAmericanAdapter();
default -> throw new IllegalArgumentException();
};
}

public void charging(USBTarget usbTarget) {
System.out.println("Start Charging...");
usbTarget.charge5V();
}
}

//Main.class
public class Main {
public static void main(String[] args) {
Phone phone = new Phone();
phone.charging(Phone.withAdapter(AdapterType.CHINESE));
phone.charging(Phone.withAdapter(AdapterType.AMERICAN));
}
}

加了点工厂模式进去,为了方便实例化,本质的核心是Adapter,通过多封装了一层适配器,去解决,其实就是解耦,phone不需要去考虑对应的charger

装饰器模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//Weapon.interface
public interface Weapon {
int getAttack();
String getDescription();
}

//Arrow.class
public class Arrow implements Weapon{

@Override
public int getAttack() {
return 30;
}

@Override
public String getDescription() {
return "木弓";
}
}

//Sword.class
public class Sword implements Weapon {
@Override
public int getAttack() {
return 50;
}

@Override
public String getDescription() {
return "钢铁长剑";
}
}

//WeaponEnchantment.class
public abstract class WeaponEnchantment implements Weapon {
Weapon enchantmentWeapon;

public WeaponEnchantment(Weapon weapon) {
this.enchantmentWeapon = weapon;
}
}


//FireEnchantment.class 这是核心类,主要就是这里去读取旧攻击力和新的攻击力,就等于是附魔了
//这就是装饰器模式,可以给不同的武器附魔
public class FireEnchantment extends WeaponEnchantment {
private String enchantmentAttribute = "🔥";
private int enchantmentAttack = 5;

public FireEnchantment(Weapon weapon) {
super(weapon);
}

@Override
public int getAttack() {
int originAttack = this.enchantmentWeapon.getAttack();
int enchantment = originAttack + this.enchantmentAttack;
return enchantment;
}

@Override
public String getDescription() {
String originDesc = this.enchantmentWeapon.getDescription();
return originDesc + "[" + this.enchantmentAttribute + "附魔]";
}

}

//Main.class
public class Main {
public static void main(String[] args) {
FireEnchantment fireEnchantmentArrow = new FireEnchantment(new Arrow());
System.out.println(fireEnchantmentArrow.getDescription());
System.out.println(fireEnchantmentArrow.getAttack());

Weapon fireSword = new FireEnchantment(new Sword());
System.out.println(fireSword.getDescription()); // 钢铁长剑附🔥
System.out.println(fireSword.getAttack()); // 55
}
}

装饰器模式就是一个附魔的过程,需要去记忆的就是 抽象类去实现weapon接口,然后附魔类去给武器附不同属性的魔

🏭 工厂方法:一个产品,多种实现
🏭 抽象工厂:一套产品,整体替换
🔨 建造者:复杂对象,分步构建
📋 原型:已有对象,复制使用
👑 单例:全局唯一,严格控制
🎁 装饰器:相同接口,功能叠加(像俄罗斯套娃)
🔌 适配器:接口转换,兼容协作(像电源转接头)

享元模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//Particle.class
public interface Particle {
void render(int x, int y, int life);
}

//Particle.type
public enum ParticleType {
RED_SPARK,
BLUE_SPARK
}

//SparkParticle.class
public class SparkParticle implements Particle {
String color;
String texture;

public SparkParticle(String color, String texture) {
this.color = color;
this.texture = texture;
}

@Override
public void render(int x, int y, int life) {
System.out.println("渲染" + color + texture + "在位置(" + x + "," + y + "), 生命周期:" + life);
}
}

//ParticleFactory.class
public class ParticleFactory {
private final Map<ParticleType, Particle> pool = new HashMap<>();

public Particle getParticle(ParticleType type) {
// 如果存在则返回共享对象,否则创建并缓存
return pool.computeIfAbsent(type, this::createParticle);
}

public Particle createParticle(ParticleType type) {
return switch (type) {
case RED_SPARK -> new SparkParticle("red", "spark");
case BLUE_SPARK -> new SparkParticle("blue", "spark");
default -> throw new IllegalArgumentException();
};
}
}

//Main.class
public class Main {
public static void main(String[] args) {
ParticleFactory particleFactory = new ParticleFactory();
Particle particle = particleFactory.getParticle(ParticleType.RED_SPARK);
particle.render(1,2,3);
Particle particle2 = particleFactory.getParticle(ParticleType.RED_SPARK);
particle2.render(1,2,3);
}
}

享元模式的核心: 分清内部元素和外部元素,控制动与静,动静分离是核心

🏭 工厂方法:一个产品,多种实现
🏭 抽象工厂:一套产品,整体替换
🔨 建造者:复杂对象,分步构建
📋 原型:已有对象,复制使用
👑 单例:全局唯一,严格控制
🎁 装饰器:相同接口,功能叠加(像俄罗斯套娃)
🔌 适配器:接口转换,兼容协作(像电源转接头)
🏷️ 享元模式:共享内在,传递外在(像活字印刷)

拉闸!