作者:拍友2502869293 | 来源:互联网 | 2023-10-13 10:37
p01导论设计模式7大原则开闭原则里氏替换原则依赖倒装原则单一职责原则接口隔离原则迪米特法则合成复用原则p02创建型模式-为什么使用创建型模式p03单例模式***佟老师只能有一
p01导论
设计模式7大原则
开闭原则
里氏替换原则
依赖倒装原则
单一职责原则
接口隔离原则
迪米特法则
合成复用原则
p02 创建型模式-为什么使用创建型模式
p03单例模式
public class Person {private String name;private String age;private volatile static Person instance; private Person() {System.out.println("创建了Person");}public static Person guiguBoss() {if (instance == null) {synchronized (Person.class) {if(instance == null){Person person = new Person();instance = person;}}}return instance;}}
04 单例的应用场景
p05 创建型模式-原型模式
p06 工厂模式-简单工厂模式
抽象产品
public abstract class AbstractCar {String engine;public abstract void run();
}
具体产品
public class MiniCar extends AbstractCar{public MiniCar(){this.engine = "四缸水平对置发动机";}@Overridepublic void run() {System.out.println(engine+"--> 嘟嘟嘟...");}
}
public class VanCar extends AbstractCar{public VanCar(){this.engine = "单杠柴油机";}@Overridepublic void run() {System.out.println(engine+"--》嗒嗒嗒....");}
}
简单工厂在这里插入代码片
public class WuLinSimpleFactory {public AbstractCar newCar(String type){if("van".equals(type)){return new VanCar();}else if("mini".equals(type)){return new MiniCar();}return null;}
}
测试类
public class MainTest {public static void main(String[] args) {WuLinSimpleFactory factory = new WuLinSimpleFactory();AbstractCar van = factory.newCar("van");AbstractCar mini = factory.newCar("mini");AbstractCar zz = factory.newCar("zz");van.run();mini.run();}
}
p07工厂方法模式
p08 抽象工厂模式
多种产品
抽象工厂 生产汽车与口罩的抽象方法
public abstract class WuliFactory {abstract AbstractMask newMask();abstract AbstartCar newCar();
}
抽象子工厂 抽象生产车的方法
public abstract class WuliCarFactory extends WuliFactory {@OverrideAbstractMask newMask() {return null;}@Overridepublic abstract AbstartCar newCar();
}
具体子工厂,生产赛车
public class WuliRaceCarFactory extends WuliCarFactory {@Overridepublic AbstartCar newCar() {return new RaceCar();}
}
具体子工厂,生产卡车
public class WuliTructFactory extends WuliCarFactory {@Overridepublic AbstartCar newCar() {return new Truck();}
}
抽象产品
public abstract class AbstartCar {String engine;public abstract void run();
}
具体产品
public class RaceCar extends AbstartCar {public RaceCar() {this.engine="V8发动机";}@Overridepublic void run() {System.out.println(engine+"嗖嗖---");}
}
public class Truck extends AbstartCar {public Truck() {this.engine="柴油发动机";}@Overridepublic void run() {System.out.println(engine+"哒哒哒---");}
}
p09
略
p10 创建型模式-建造者模式
产品角色
@Data
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Phone {protected String cpu;protected String memory;protected String disk;protected String camera;}
抽象构建者
public abstract class AbstractPhoneBuilder {Phone phone;abstract AbstractPhoneBuilder customCpu(String cpu);abstract AbstractPhoneBuilder customDisk(String disk);abstract AbstractPhoneBuilder customMemory(String memory);abstract AbstractPhoneBuilder customCamera(String customCamera);public Phone getProduct(){return phone;}
}
具体构建者
public class XiaomiBuilder extends AbstractPhoneBuilder {public XiaomiBuilder() {this.phone=new Phone();}@OverrideAbstractPhoneBuilder customCpu(String cpu) {this.phone.cpu=cpu;return this;}@OverrideAbstractPhoneBuilder customDisk(String disk) {this.phone.disk=disk;return this;}@OverrideAbstractPhoneBuilder customMemory(String memory) {this.phone.memory=memory;return this;}@OverrideAbstractPhoneBuilder customCamera(String customCamera) {this.phone.camera=customCamera;return this;}
}
public static void main(String[] args) {AbstractPhoneBuilder abstractPhoneBuilder=new XiaomiBuilder();Phone phone = abstractPhoneBuilder.customCpu("骁龙888").customDisk("1T").customMemory("16G").customCamera("1亿像素").getProduct();System.out.println(phone);}