单例模式
1 //最简单、有效的单例实现 2 private static class SingleWithHolder { 3 private static class Holder{ 4 private static SingleWithHolder instance = new SingleWithHolder(); 5 } 6 //通过SingleWithHolder.instance()得到单例 7 private static SingleWithHolder instance() { 8 return Holder.instance; 9 } 10 }
1 //使用enum实现单例 2 private enum SingleWithEnum{ 3 //通过SingleWithEnum.INSTANCE.instance()得到单例 4 INSTANCE{ 5 @Override 6 public SingleTest instance() { 7 return instance; 8 } 9 }; 10 protected SingleTest instance; 11 //enum的构造器必须为private 12 private SingleWithEnum() { 13 instance = new SingleTest(); 14 } 15 //Enum为抽象类 16 public abstract SingleTest instance(); 17 }
1 //经典的双重检查锁 2 private static class SingleWithDoubleCheck{ 3 private static volatile SingleSample instance; 4 public static SingleSample instance() { 5 if (instance == null) { 6 synchronized (SingleWithDoubleCheck.class) { 7 if (instance == null) { 8 //如果不将instance声明为volatile,这里就会出现问题,instance = new SingleSample();的执行逻辑大致分三步: 9 // 1分配对象空间、2赋初值、3返回对象引用, 10 // 如果经过指令重排序后执行顺序变成了1、3、2,就会出现可以获得非空对象,但对象内字段为零值的情况,而volatile可以禁止指令重排序。 11 instance = new SingleSample(); 12 } 13 } 14 } 15 return instance; 16 } 17 }
工厂模式
工厂模式可以屏蔽产品生产的细节(工厂生产产品本身是很复杂的,可能又会涉及其他产品的生产,这些细节都由工厂来保证),同时将产品的具体实现与客户解耦,工厂是用来生产产品的,如何提升工厂的扩展性(方便的加入新工厂、新产品)是工厂模式要注意的。
简单工厂模式
1 //产品抽象 2 abstract class BMW { 3 public BMW(){ 4 5 } 6 } 7 //具体产品 8 public class BMW320 extends BMW { 9 public BMW320() { 10 System.out.println("制造-->BMW320"); 11 } 12 } 13 public class BMW523 extends BMW{ 14 public BMW523(){ 15 System.out.println("制造-->BMW523"); 16 } 17 } 18 //产品工厂,负责生产所有产品 19 public class Factory { 20 public BMW createBMW(int type) { 21 switch (type) { 22 23 case 320: 24 return new BMW320(); 25 26 case 523: 27 return new BMW523(); 28 29 default: 30 break; 31 } 32 return null; 33 } 34 } 35 //客户 36 public class Customer { 37 public static void main(String[] args) { 38 Factory factory = new Factory(); 39 //类似spring中BeanFactory的getBean 40 BMW bmw320 = factory.createBMW(320); 41 BMW bmw523 = factory.createBMW(523); 42 } 43 }
工厂方法模式
1 //产品抽象 2 abstract class BMW { 3 public BMW(){ 4 5 } 6 } 7 //具体产品 8 public class BMW320 extends BMW { 9 public BMW320() { 10 System.out.println("制造-->BMW320"); 11 } 12 } 13 public class BMW523 extends BMW{ 14 public BMW523(){ 15 System.out.println("制造-->BMW523"); 16 } 17 } 18 //工厂抽象 19 interface FactoryBMW { 20 BMW createBMW(); 21 } 22 //具体工厂 23 public class FactoryBMW320 implements FactoryBMW{ 24 25 @Override 26 public BMW320 createBMW() { 27 28 return new BMW320(); 29 } 30 31 } 32 public class FactoryBMW523 implements FactoryBMW { 33 @Override 34 public BMW523 createBMW() { 35 36 return new BMW523(); 37 } 38 } 39 //客户 40 public class Customer { 41 public static void main(String[] args) { 42 //类似spring中ClassPathXmlApplicationContext和FileSystemXmlApplicationContext都继承了抽象类AbstractXmlApplicationContext,二者的差异仅在于路径解析 43 FactoryBMW320 factoryBMW320 = new FactoryBMW320(); 44 BMW320 bmw320 = factoryBMW320.createBMW(); 45 46 FactoryBMW523 factoryBMW523 = new FactoryBMW523(); 47 BMW523 bmw523 = factoryBMW523.createBMW(); 48 } 49 }
抽象工厂模式
现在要求为每个车型生产配件
1 //发动机以及型号 2 public interface Engine { 3 4 } 5 public class EngineA extends Engine{ 6 public EngineA(){ 7 System.out.println("制造-->EngineA"); 8 } 9 } 10 public class EngineBextends Engine{ 11 public EngineB(){ 12 System.out.println("制造-->EngineB"); 13 } 14 } 15 16 //空调以及型号 17 public interface Aircondition { 18 19 } 20 public class AirconditionA extends Aircondition{ 21 public AirconditionA(){ 22 System.out.println("制造-->AirconditionA"); 23 } 24 } 25 public class AirconditionB extends Aircondition{ 26 public AirconditionB(){ 27 System.out.println("制造-->AirconditionB"); 28 } 29 } 30 //工厂抽象 31 public interface AbstractFactory { 32 //制造发动机 33 public Engine createEngine(); 34 //制造空调 35 public Aircondition createAircondition(); 36 } 37 38 39 //宝马320系列配件工厂 40 public class FactoryBMW320 implements AbstractFactory{ 41 42 @Override 43 public EngineA createEngine() { 44 return new EngineA(); 45 } 46 @Override 47 public AirconditionA createAircondition() { 48 return new AirconditionA(); 49 } 50 } 51 //宝马523系列配件工厂 52 public class FactoryBMW523 implements AbstractFactory { 53 54 @Override 55 public EngineB createEngine() { 56 return new EngineB(); 57 } 58 @Override 59 public AirconditionB createAircondition() { 60 return new AirconditionB(); 61 } 62 } 63 //客户 64 public class Customer { 65 public static void main(String[] args){ 66 //生产宝马320系列配件 67 FactoryBMW320 factoryBMW320 = new FactoryBMW320(); 68 factoryBMW320.createEngine(); 69 factoryBMW320.createAircondition(); 70 71 //生产宝马523系列配件 72 FactoryBMW523 factoryBMW523 = new FactoryBMW523(); 73 factoryBMW320.createEngine(); 74 factoryBMW320.createAircondition(); 75 } 76 }
工厂方法只能生产一种产品,而抽象工厂可以生产多种产品。
参考:http://blog.csdn.net/jason0539/article/category/3092021