浅析设计模式

1.设计思想

a.编程的本质?

接口,类,方法

b.设计模式的作用?

指导,规定如何写接口,类和方法

c.为什么要用设计模式?

偷懒,以不变应万变

易扩展,易维护

2.设计原则:

a.找出变化,隔离变化,封装变化

b.面向接口编程:使用者使用接口,提供者实现接口

c.依赖倒置原则:依赖抽象,不依赖具体类

d.对修改闭合,对扩展开放

e.多用组合,少用继承(灵活变化)

f.单一职能原则

3.设计模式:

1)创建型模式

提供了一种在创建对象的同时隐藏创建逻辑的方式

•工厂模式 Factory Pattern
•抽象工厂模式 Abstract Factory Pattern
•单例模式 Singleton Pattern
•建造者模式 Builder Pattern
•原型模式 Prototype Pattern

2)结构型模式

关注类和对象的组合。继承的概念被用来组合接口和定义组合对象获得新功能的方式。

•适配器模式 Adapter Pattern
•桥接模式 Bridge Pattern
•组合模式 Composite Pattern
•装饰者模式 Decorator Pattern
•外观模式 Facade Pattern
•享元模式 Flyweight Pattern
•代理模式 Proxy Pattern

3)行为型模式

关注对象之间的通信

•责任链模式 Chain of Responsibility Pattern
•命令模式 Command Pattern
•解释器模式 Interpreter Pattern
•迭代器模式 Iterator Pattern
•中介者模式 Mediator Pattern
•备忘录模式 Memento Pattern
•观察者模式 Observer Pattern
•状态模式 State Pattern
•空对象模式 Null Object Pattern
•策略模式 Strategy Pattern
•模板模式 Template Pattern
•访问者模式 Visitor Pattern

4.几种模式的示例

a,策略模式、面向接口

@Service
public class OrderService {
	public Order prepareOrder(Order order, String promotion) {
			switch (promotion) {
			case "promotion-1":
			// 促销1的算法
			return new Promotion1Calculation().calculate(order);
			case "promotion-2":
			// 促销2的算法
			return new Promotion2Calculation().calculate(order);
			case "promotion-3":
			// 促销3的算法
			return new Promotion3Calculation().calculate(order);
			......
		}
	}
}

b.工厂模式

@Service
public class OrderService {
	@Autowired
	private PromotionCalculationFactory promotionCalculationFactory;
	
	public Order prepareOrder(Order order, String promotion) {
		return promotionCalculationFactory
		.getPromotionCalculation(promotion).calculate(order);
	}
}
@Component
public class PromotionCalculationFactory {
    public PromotionCalculation getPromotionCalculation(String promotion) {
		switch (promotion) {
			case "promotion-1":
			// 促销1的算法
			return new Promotion1Calculation();
			case "promotion-2":
			// 促销2的算法
			return new Promotion2Calculation();
			case "promotion-3":
			// 促销3的算法
			return new Promotion3Calculation();
			......
		}
	}
}

c.装饰者模式

不改变具体类代码,动态叠加增强行为功能

比继承更有弹性的扩展替代方案

d.代理模式

代理中控制使用者对目标对象的访问,以及进行功能增强。

静态代理:

由开发者创建或特定工具自动生成代理类源码,再对其编译。在程序运行之前,代理的.class文件就已经存在了。

public class Tony implements Girl {
	private Girl girl;
	public Girl getGirl() {
		return girl;
	}
	public void setGirl(Girl girl) {
		this.girl = girl;
	}
	public boolean dating(float length) {
		// 前置增强
		doSomethingBefore();
		boolean res = this.girl.dating(length);
		// 后置增强
		doSomethingAfter();
		return res;
	}
	
	private void doSomethingBefore() {
		System.out.println("老板,这个我试过了,很不错,推荐给你!");
	}
	
	private void doSomethingAfter() {
		System.out.println("老板,你觉得怎样,欢迎下次再约!");
	}
}



public class PlayGame { 
    public static void main(String[] args) { 
        TuHao th = new TuHao(1.7F); Girl tc = new TeacherCang(); 
        Tony tony = new Tony(); 
        tony.setGirl(tc); 
        th.dating(tony); 
    } 
}

缺点:扩展差,可维护性能差

JDK动态代理:只对接口创建代理

public class MyInvationHandler implements InvocationHandler { 
	private Object target; 
	public MyInvationHandler(Object target) { 
		this.target = target; 
	} 
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 
		// 前置增强 
		doSomethingBefore(); 
		// 调用被代理对象的方法 
		Object res = method.invoke(target, args); 
		// 后置增强 
		doSomethingAfter(); 
		return res; 
	} 
	private void doSomethingAfter() { 
        System.out.println("老板,你觉得怎样,欢迎下次再约!"); 
    } 
	private void doSomethingBefore() { 
        System.out.println("老板,这个我试过了,很不错,推荐给你!");
    }
	
}



public class TonyCompany {
	public static Object proxy(Object target) {
		return Proxy.newProxyInstance(
			target.getClass().getClassLoader(),
			target.getClass().getInterfaces(),
			new MyInvationHandler(target));
}


public class PlayGame {
	public static void main(String[] args) {
		TuHao th = new TuHao(1.7F);
		Girl tc = new TeacherCang();
		Girl tony1 = (Girl) TonyCompany.proxy(tc);
		th.dating(tony1);
		Boy tcc = new TeacherChen();
		Boy tony2 = (Boy) TonyCompany.proxy(tcc);
		tony2.dating('E');
		tony2.show();
	}
}




cglib动态代理:可对接口,类创建代理

https://github.com/cglib/cglib/wiki
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>3.2.6</version>
</dependency>

e.单例模式

饥汉模式:还没用到之前对象就已经初始化了

public class Singleton {
	private final static Singleton INSTANCE = new Singleton();
	private Singleton(){}
	public static Singleton getInstance(){
		return INSTANCE;
	}
}

懒汉模式:用到的时候再初始化对象

双重检查: volatile关键字

public class Singleton {
	private static volatile Singleton singleton;
	private Singleton() {}
	public static Singleton getInstance() {
		if (singleton == null) {
			synchronized (Singleton.class) {
				if (singleton == null) {
					singleton = new Singleton();
				}
			}
		}
		return singleton;
	}
}

利用静态内部类懒加载的特性,类的静态属性只会在第一次加载类的时候初始化。在这里JVM帮助我们保证了线程的安全性,在类进行初始化时,别的线程是无法进入的。

public class Singleton {
	private Singleton() {}
	private static class SingletonInstance {
		private static final Singleton INSTANCE = new Singleton();
	}
	public static Singleton getInstance() {
		return SingletonInstance.INSTANCE;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值