[Musings]
今天回看工厂模式和抽象工厂模式,发现了其实本质上所有button/textbox抽象类和实现类都是通用的,唯独的不同在于工厂的性质变了,一个注重垂类产品,一个注重品牌哈哈哈
开始结构型的设计模式了
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
|
public interface USBTarget { void charge5V(); }
public class AmericanCharger { public void supply110V(){ System.out.println("American Charger, output 110V"); } }
public class ChineseCharger{ public void supply220V(){ System.out.println("Chinese Charger, output 220V"); } }
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!"); } }
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!"); } }
enum AdapterType { CHINESE, AMERICAN }
public class AdapterFactory { private AdapterFactory() { }
public static USBTarget createChineseAdapter() { return new ChineseAdapter(new ChineseCharger()); }
public static USBTarget createAmericanAdapter() { return new AmericanAdapter(new AmericanCharger()); } }
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(); } }
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
| public interface Weapon { int getAttack(); String getDescription(); }
public class Arrow implements Weapon{
@Override public int getAttack() { return 30; }
@Override public String getDescription() { return "木弓"; } }
public class Sword implements Weapon { @Override public int getAttack() { return 50; }
@Override public String getDescription() { return "钢铁长剑"; } }
public abstract class WeaponEnchantment implements Weapon { Weapon enchantmentWeapon;
public WeaponEnchantment(Weapon weapon) { this.enchantmentWeapon = weapon; } }
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 + "附魔]"; }
}
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()); } }
|
装饰器模式就是一个附魔的过程,需要去记忆的就是 抽象类去实现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
| public interface Particle { void render(int x, int y, int life); }
public enum ParticleType { RED_SPARK, BLUE_SPARK }
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); } }
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(); }; } }
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); } }
|
享元模式的核心: 分清内部元素和外部元素,控制动与静,动静分离是核心
🏭 工厂方法:一个产品,多种实现
🏭 抽象工厂:一套产品,整体替换
🔨 建造者:复杂对象,分步构建
📋 原型:已有对象,复制使用
👑 单例:全局唯一,严格控制
🎁 装饰器:相同接口,功能叠加(像俄罗斯套娃)
🔌 适配器:接口转换,兼容协作(像电源转接头)
🏷️ 享元模式:共享内在,传递外在(像活字印刷)
拉闸!