Java:设计模式

 1 java中的设计模式:
 2 总体来说设计模式分为三大类:
 3 创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
 4 结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
 5 行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
 6 
 7 单例模式:
 8 一个类只能有一个对象实例,多线程单例要用锁将实例锁起来
 9 //懒汉,线程安全
10 public class SingletonDemo{
11 private static SingletonDemo instance;
12 private SingletonDemo(){}
13 public static synchronized SingletonDemo getInstance(){
14 if(instance == null){
15 instance = new SingletonDemo();
16 }
17 return instance;
18 }
19 }
20 //双重校验锁
21 public class Singleton{
22 private static Singleton instance;
23 private Singleton(){}
24 public static Singleton getInstance(){
25 if(instance == null){
26 synchronized(Singleton.class){
27 if(instance == null){
28 instance = new Singleton();
29 }
30 }
31 }
32 return instance;
33 }
34 }

 

posted @ 2018-09-25 11:19  fanghuiX  阅读(158)  评论(0编辑  收藏  举报