你的位置:首页 > ASP.net教程

[ASP.net教程]设计模式笔记(一)之工厂模式、单例模式


一、工厂模式

(1)简单工厂模式:

1 public interface IProduct {2 3   public void saleProduct();4 5 }

创建一个产品接口,有一个卖产品的方法。

1 public class ProductA implements IProduct{2 3   public void saleProduct(){4     System.out.println("汉堡");5   }6 }

产品A实现产品接口,卖汉堡。

1 public class ProductB implements IProduct {2 3   @Override4   public void saleProduct() {5     // TODO Auto-generated method stub6     System.out.println("薯条");7   }8 9 }

产品B实现产品接口,卖薯条。

 1 public class Creator { 2  3   public IProduct createFactory(int i){ 4     switch(i){ 5       case 1: 6         return new ProductA(); 7       case 2: 8         return new ProductB(); 9       default:10         return null;11     }    12   }13   14 }

创造者是简单工厂模式的核心:返回值类型是IProduct接口,通过测试类传来的形参确定返回哪个对象。

1 public class TestProduct {2 3   public static void main(String[] args) {4     Creator c=new Creator();5     c.createFactory(2).saleProduct();6   }7 }

测试类:输出薯条。

(2)方法工厂模式:

(图片来自于网络)

1 public interface Light {2 3   public void TurnOn();4   public void TurnOff();5 }

创建一个产品接口。

 1 public class TubeLight implements Light { 2  3   @Override 4   public void TurnOn() { 5     // TODO Auto-generated method stub 6     System.out.println("rabbit1"); 7   } 8  9   @Override10   public void TurnOff() {11     // TODO Auto-generated method stub12 13   }14 15 }

 1 public class BulbLight implements Light { 2  3   @Override 4   public void TurnOn() { 5     // TODO Auto-generated method stub 6     System.out.println("rabbit2"); 7   } 8  9   @Override10   public void TurnOff() {11     // TODO Auto-generated method stub12 13   }14 15 }

分别有两个产品类实现这个接口。

1 public interface Creator {2 3   public Light factory();4   5 }

创建工厂接口,返回值是产品接口。

1 public class TubeCreator implements Creator{2 3   @Override4   public Light factory() {5     // TODO Auto-generated method stub6     return new TubeLight();7   }8 9 }

 

1 public class BulbCreator implements Creator {2 3   @Override4   public Light factory() {5     // TODO Auto-generated method stub6     return new BulbLight();7   }8 9 }

工厂类实现工厂接口,返回与该工厂对应产品的对象。

 1 public class Client { 2   public static void main(String[] args) { 3      4     Creator create=new BulbCreator(); 5     Light bulblight=create.factory(); 6     bulblight.TurnOn(); 7      8     Creator create1=new TubeCreator(); 9     Light tubelight=create1.factory();10     tubelight.TurnOn();11   }12   13   14 }

顾客来卖东西了:通过工厂类来生产产品。

(3)抽象工厂模式:

1 public abstract class AbstractProductA {2 3   public abstract void show();4 }

1 public abstract class AbstractProductB {2   public abstract void show();3 }

创建两个抽象产品A,B.

 1 public class ProductA extends AbstractProductA{ 2  3   @Override 4   public void show() { 5     // TODO Auto-generated method stub 6     System.out.println("A"); 7   } 8  9   10 }

1 public class ProductB extends AbstractProductB{2 3   @Override4   public void show() {5     // TODO Auto-generated method stub6     System.out.println("B");7   }8 9 }

两个具体的产品分别继承了它们。

1 public interface AbstractFactory {2 3   public AbstractProductA createProductA();4   5   public AbstractProductB createProductB();6 }

抽象工厂里有两个方法,返回值分别是抽象产品A,抽象产品B.

 1 public class ConcreteFactory implements AbstractFactory{ 2  3   @Override 4   public ProductA createProductA() { 5     // TODO Auto-generated method stub 6     return new ProductA(); 7   } 8  9   @Override10   public ProductB createProductB() {11     // TODO Auto-generated method stub12     return new ProductB();13   }14 15 }

具体的工厂实现了抽象工厂接口。注意:这里的返回值类型不是AbstractProductA 和AbstractProductB。而是它们的儿子ProductA 和 ProductB。 

 1 public class Test { 2  3   public static void main(String[] args) { 4     ConcreteFactory f1=new ConcreteFactory(); 5     ProductA a=f1.createProductA(); 6     a.show(); 7      8     ConcreteFactory f2=new ConcreteFactory(); 9     ProductB b=f2.createProductB();10     b.show();11   }12 }

测试类:工厂生产产品。

 

二、单例模式的四种写法:

(1)

 1 public class Single1 { 2 //不支持多线程 3   private Single1(){ 4      5   } 6   private static Single1 me; 7   public static Single1 getInStance(){ 8     if(me==null){ 9       me=new Single1();10     }11     return me;12   }13   14 }

 

(2)

 1 public class Single2 { 2 //支持多线程,效率低。 3   private Single2(){ 4      5   } 6   private static Single2 me;  7   public static synchronized Single2 getInStance(){ 8     if(me==null){ 9       me=new Single2();10     }  11     return me;12   }13   14 }

(3)

 1 public class Single3 { 2  3   private Single3(){ 4      5   } 6   private static class SingletonHolder{ 7     private static final Single3 me=new Single3(); 8   }  9   10   public static final Single3 getInStance(){11     12     return SingletonHolder.me;13   }  14 }

(4)

 1 public class Single4 { 2  3   private Single4(){ 4      5   } 6   private volatile static Single4 me; 7    8   public static Single4 getInStance(){ 9     if(me==null){10       synchronized(Single4.class){11         if(me==null){12           me=new Single4();13         }14       }15     }16     return me;17   }18   19 }

 

 本贴纯属个人的学习和理解,如有错误望各位大神指正。