java中工廠模式和單例模式詳解

如何將實例化具體類的代碼從應用中抽離或者封裝起來,使它們不會干擾應用的其他部分?


1:簡單工廠模式:其作用是實例化對象而不需要客戶瞭解這個對象屬於那個具體的子類。

using System;
using System.Collections;

public class MyClass
{
    public static voidMain()
    {
       //通過參數來實例化子類。
       IVehicle vehicle =FactoryVehicle.CreateVehicle("car");
       vehicle.go();
       Console.ReadLine();   
    }
    
}

public class FactoryVehicle
{
    public static IVehicleCreateVehicle(string VehicleName)
    {
       switch(VehicleName.ToLower())
       {
           case"car":
           return newCar();
           case"boat":
           return newBoat();
          default:
           return newCar();
          
       }
    }

}
public interface IVehicle
{
    void go();
}
public class Car:IVehicle
{
    public void go()
    {
       Console.WriteLine("car");
    }
}
public class Boat:IVehicle
{
    public void go()
    {
       Console.WriteLine("boat");
    }
}
2:抽象工廠:即將工廠方法也抽象出來,由具體的子類來實例化工廠。產品創建部分和簡單工廠模式相同。

using System;
using System.Collections;

public class MyClass
{
    public static voidMain()
    {
       //通過定義的工廠來實例化。弊端是當產品很多時需要增加很多的工廠。代碼重複。
       FactoryVehicle factory = new FactoryCar();
       IVehicle vehicle =factory.CreateVehicle();
       vehicle.go();
       Console.ReadLine();   
    }
    
}


public interface FactoryVehicle
{
    IVehicle CreateVehicle();

}

public class FactoryCar:FactoryVehicle
{
    public IVehicleCreateVehicle()
    {
       return new Car();
    }
}

public class FactoryBoat:FactoryVehicle
{
    public IVehicleCreateVehicle()
    {
       return new Boat();
    }
}

public interface IVehicle
{
    void go();
}

public class Car:IVehicle
{
    public void go()
    {
       Console.WriteLine("car");
    }
}

public class Boat:IVehicle
{
    public void go()
    {
       Console.WriteLine("boat");
    }
}3:反射工廠模式: 其實就是通過反射的方式來獲得具體實例化是那個類。

using System;
using System.Collections;
using System.Reflection;

public class MyClass
{
    public static voidMain()
    {
       //使用反射的方法獲得實例化的類
       IVehicle vechicle =ReflectFactory.CreateVehicleByReflect("Car");
       vechicle.go();
       Console.ReadLine();   
       
    }
    
}

public class ReflectFactory
{
    public static IVehicleCreateVehicleByReflect(string typeName)
    {
       Type type = Type.GetType(typeName);
       ConstructorInfo  ci =type.GetConstructor(System.Type.EmptyTypes);;
       return (IVehicle)ci.Invoke(null);
    }
}
public class FactoryBoat:FactoryVehicle
{
    public IVehicleCreateVehicle()
    {
       return new Boat();
    }
}

public class FactoryCar:FactoryVehicle
{
    public IVehicleCreateVehicle()
    {
       return new Car();
    }
}

public interface FactoryVehicle
{
    IVehicleCreateVehicle();

}
public interface IVehicle
{
    void go();
}

public class Car:IVehicle
{
    public void go()
    {
       Console.WriteLine("car");
    }
}

public class Boat:IVehicle
{
    public void go()
    {
       Console.WriteLine("boat");
    }
}

概念:
  java中單例模式是一種常見的設計模式,單例模式分三種:懶漢式單例、餓漢式單例、登記式單例三種。

  單例模式有一下特點:
  1、單例類只能有一個實例。
  2、單例類必須自己自己創建自己的唯一實例。
  3、單例類必須給所有其他對象提供這一實例。
  單例模式確保某個類只有一個實例,而且自行實例化並向整個系統提供這個實例。在計算機系統中,線程池、緩存、日誌對象、對話框、打印機、顯卡的驅動程序對象常被設計成單例。這些應用都或多或少具有資源管理器的功能。每臺計算機可以有若干個打印機,但只能有一個Printer Spooler,以避免兩個打印作業同時輸出到打印機中。每臺計算機可以有若干通信端口,系統應當集中管理這些通信端口,以避免一個通信端口同時被兩個請求同時調用。總之,選擇單例模式就是爲了避免不一致狀態,避免政出多頭。



單利模式:
package singleton;  

02    

03  

13  

16 class LockSingleton{  

17     private volatilestatic LockSingleton singleton;  

18     privateLockSingleton(){}  

19       

20    //詳見:http://www.ibm.com/developerworks/cn/java/j-dcl.html 

21     public staticLockSingleton getInstance(){  

22        if(singleton==null){  

23           synchronized(LockSingleton.class){  

24               if(singleton==null){ 

25                  singleton=new LockSingleton(); 

26               }  

27            } 

28        }  

29        return singleton;  

30     } 

31       

32 }  

33  

36 enum EnumSingleton{  

37     INSTANCE; 

38     public voiddoSomeThing(){  

39     } 

40 }  

41  

44 class InternalSingleton{  

45     private static classSingletonHolder{  

46        private final static InternalSingleton INSTANCE=newInternalSingleton();  

47     }    

48     privateInternalSingleton(){}  

49     public staticInternalSingleton getInstance(){  

50        return SingletonHolder.INSTANCE; 

51     } 

52 }  

53  

56 class HungrySingleton{  

57     private staticHungrySingleton singleton=new HungrySingleton(); 

58     privateHungrySingleton(){}  

59     public staticHungrySingleton getInstance(){  

60        return singleton;  

61     } 

62 }  

63  

66 class LazySingleton{  

67     private staticLazySingleton singleton;  

68     privateLazySingleton(){  

69     } 

70     public staticLazySingleton getInstance(){  

71        if(singleton==null){  

72           singleton=new LazySingleton();  

73        }  

74        return singleton;  

75     }    

76 } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章