设计模式的分类
创建型
单例模式、简单工厂模式、抽象工厂模式、创建者模式、原型模式
结构型
代理模式、适配器模式、装饰者模式、桥接模式、门面模式、享元模式、组合模式
行为型
观察者模式、状态模式、策略模式、模版方法模式、责任链模式、迭代器模式、访问者模式、备忘录模式、命令模式、解释器模式、中介模式
通常设计模式可以通过组合使用,来提供满足设计原则的实现
常用设计模式举例
单例模式
通常 java 中的单例模式有 5 种实现方式, 而不是老师课件中的 2 种
五种方式分别是:饿汉式、懒汉式、双重检查(懒汉式)、静态内部类和枚举
通常更推荐饿汉式、静态内部类和枚举这些无锁的实现
饿汉式
/** * 饿汉式 */public class Singleton_01 { private Singleton_01(){} private final static Singleton_01 INSTANCE = new Singleton_01(); public static Singleton_01 getInstance(){ return INSTANCE; }}
复制代码
懒汉式
/** * 懒汉式:并发有性能消耗 */public class Singleton_02 { private Singleton_02(){} private static Singleton_02 instance; public static synchronized Singleton_02 getInstance(){ if (instance == null){ instance = new Singleton_02(); } return instance; }}
复制代码
双重检查懒汉式
/** * 双重检查:可过滤一部分入锁操作,性能优于懒汉式 */public class Singleton_03 { private Singleton_03(){} private static Singleton_03 instance;
public static Singleton_03 getInstance(){ if (instance == null){ synchronized (Singleton_03.class){ if (instance == null){ instance = new Singleton_03(); } } } return instance; }}
复制代码
静态内部类
/** * 静态内部类,具备延时加载特性 */public class Singleton_04 { private Singleton_04(){} private static class SingletonInstance { private static final Singleton_04 INSTANCE = new Singleton_04(); } public static Singleton_04 getInstance(){ return SingletonInstance.INSTANCE; }}
复制代码
枚举
/** * 枚举 */public enum Singleton_05 { INSTANCE;}
复制代码
适配器模式
适配器模式有 2 种实现方式。
类适配器:继承
对象适配器:组合+委托
类适配器类图
demo
usb c 适配到 usb a
public interface UsbC { void charge();}public class UsbA { public void chargeA(){ System.out.println("usb a 充电"); }}public class UsbAAdaptor extends UsbA implements UsbC { @Override public void charge() { System.out.println("转换 成usb a 充电"); this.chargeA(); }}
复制代码
对象适配器类图
demo
usb c 适配到 usb b
public interface UsbC { void charge();}public class UsbB { public void chargeB(){ System.out.println("usb b 充电"); }}public class UsbBAdaptor implements UsbC { private UsbB usbB; public UsbBAdaptor(UsbB usbB){ this.usbB = usbB; } @Override public void charge() { System.out.println("转换 成usb b 充电"); usbB.chargeB(); }}
复制代码
模版方法模式
模板方法模式在一个方法中定义一个算法骨架,并将某些步骤推迟到子类中实现。模板方法模式可以让子类在不改变算法整体结构的情况下,重新定义算法中的某些步骤。
作用:复用和扩展
典型应用:junit ,servlet
策略模式
定义一族算法类,将每个算法分别封装起来,让它们可以互相替换。策略模式可以使算法的变化独立于使用它们的客户端(这里的客户端代指使用算法的代码)
策略模式的类图
demo
public interface NotificationStrategy { void send();}public class EmailStrategy implements NotificationStrategy{ @Override public void send() { System.out.println("send email"); }}public class SMSStrategy implements NotificationStrategy{ @Override public void send() { System.out.println("send sms"); }}import java.util.HashMap;import java.util.Map;public class NotificationStrategyFactory { private static Map<String,NotificationStrategy> strategys = new HashMap<>(); static { strategys.put("sms",new SMSStrategy()); strategys.put("email",new EmailStrategy()); } public static NotificationStrategy createStrategy(String type){ return strategys.get(type); }}public class Test { public static void main(String[] args) { NotificationStrategy sms = NotificationStrategyFactory.createStrategy("sms"); sms.send(); }}
复制代码
组合模式
将一组对象组织(Compose)成树形结构,以表示一种“部分 - 整体”的层次结构。组合让客户端(在很多设计模式书籍中,“客户端”代指代码的使用者。)可以统一单个对象和组合对象的处理逻辑。
使用组合模式的前提在于,你的业务场景必须能够表示成树形结构
demo
组合模式的demo
装饰器模式
类图
demo
public class Hairdryer { public void blow(){ System.out.println("吹风 ..."); }}public abstract class HairdryerDecorator extends Hairdryer { protected Hairdryer hairdryer; public HairdryerDecorator(Hairdryer hairdryer){ this.hairdryer = hairdryer; }}public class HotHairdryerDecorator extends HairdryerDecorator { public HotHairdryerDecorator(Hairdryer hairdryer) { super(hairdryer); } private void hot(){ System.out.println("加热 ..."); } @Override public void blow() { hot(); super.blow(); }}public class CoolHairdryerDecorator extends HairdryerDecorator { public CoolHairdryerDecorator(Hairdryer hairdryer) { super(hairdryer); } private void cool(){ System.out.println("制冷 ..."); } @Override public void blow() { cool(); super.blow(); }}public class Test { public static void main(String[] args) { Hairdryer hairdryer = new HotHairdryerDecorator(new Hairdryer()); hairdryer.blow(); }}
复制代码
设计模式在项目中的应用
junit 中用的设计模式
模版方法模式,策略模式,组合模式,装饰者模式
spring 种的设计模式或原则
ioc 容器:依赖注入(通常注入有种方式:构造器注入和 setter 注入)
单例模式:spring 的 beans factory 默认创建单例对象
mvc 分层模式
评论