说到设计模式,要先了解设计模式的原则:java
了解了以上设计模式的设计原则,在去理解和学习设计模式就很简单了,在咱们开发中设计模式一共23种,这里简单介绍经常使用的几种:面试
//饿汉式
public class HSingleton{ pravite static HSingleton hurgry = new HSingletion(); private HSingleton(){} public static HSingleton getSingletonHurgry(){ return hurgry; } }
//懒汉式
public class LSingleton{
private static LSingleton instence = null;
private LSingleton(){}
public static LSingleton getInstence(){
if(instence == null){
instence = new LSingleton();
}
return instence;
}
}
3.代理模式:为其余大对象提供一个代理,以控制对当前对象的访问,或者减小太大资源的消耗。例如spring AOP的实现,mybatis中的dao层,能够直接调用接口的方法,接口中的方法就是代理方法。算法
4.策略模式:设计一个顶级接口,用不一样的算法方法去实现相同的功能,就是策略模式。spring
5.模板模式:定义一个流程,在实现某个模块功能的时候,按照这个流程开发。应用:定义一个抽象父类定义流程,或者经常使用的方法和常量,子类实现父类,实现具体细节方法。编程
6.装饰模式:对一个对象作一些装饰,使对象具有新的功能,并且是动态的。要求装饰对象和被装饰对象实现同一个接口设计模式
23种设计模式种以上几种比较经常使用,有兴趣的能够在网上了解一下其余几种:总结以下性能优化