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 } 

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