简单的说,就是通过工厂创建对象,然后客户端进行调用
1.声明接口
namespace ConsoleApplication1
{
public interface IRace
{
void ShowKing();
}
}
2.实现接口
namespace ConsoleApplication1
{
public class Human : IRace
{
public void ShowKing()
{
Console.WriteLine(" this is Human ShowKing");
}
}
}
namespace ConsoleApplication1
{
public class NE : IRace
{
public void ShowKing()
{
Console.WriteLine(" this is NE ShowKing");
}
}
}
namespace ConsoleApplication1
{
public class Orc : IRace
{
public void ShowKing()
{
Console.WriteLine(" this is Orc ShowKing");
}
}
}
3.Factory ,三种创建对象的方式
namespace ConsoleApplication1
{
public class Factory
{
/// <summary>
/// 第一种创建
/// </summary>
/// <param name="raceType"></param>
/// <returns></returns>
public static IRace CreateInstance(RaceType raceType)
{
switch (raceType)
{
case RaceType.Human:
return new Human();
case RaceType.OrC:
return new Orc();
case RaceType.NE:
return new NE();
default:
throw new Exception("创建失败!");
}
}
private static string RaceTypeConfig = System.Configuration.ConfigurationManager.AppSettings["RaceTypeConfig"];
/// <summary>
/// 第二种通过配置文件创建实例
/// </summary>
/// <param name="raceType"></param>
/// <returns></returns>
public static IRace CreateInstanceConfig()
{
RaceType raceType = (RaceType)Enum.Parse(typeof(RaceType), RaceTypeConfig);
switch (raceType)
{
case RaceType.Human:
return new Human();
case RaceType.OrC:
return new Orc();
case RaceType.NE:
return new NE();
default:
throw new Exception("创建失败!");
}
}
private static string RaceTypeReflect = System.Configuration.ConfigurationManager.AppSettings["RaceTypeReflect"];
/// <summary>
/// 第三种通过反射创建实例,可扩展性极强
/// </summary>
/// <param name="raceType"></param>
/// <returns></returns>
public static IRace CreateInstanceReflect()
{
string assemblyName = RaceTypeReflect.Split(',')[0];
string typeName = RaceTypeReflect.Split(',')[1];
return (IRace)Activator.CreateInstance(assemblyName, typeName).Unwrap();
}
}
public enum RaceType
{
Human,
OrC,
NE
}
}
4.App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<appSettings>
<!--靠配置文件改变生成的类 value 的值是具体的类 -->
<add key="RaceTypeConfig" value="Human"/>
<!--靠反射生成的对应的类 value 的值是具体的类 -->
<!--<add key="RaceTypeReflect" value="ConsoleApplication1,ConsoleApplication1.NE"/>-->
<!--靠反射生成的对应的类 value 的值是具体的类 -->
<add key="RaceTypeReflect" value="TheFive,TheFive.Five"/>
</appSettings>
</configuration>
5.client 调用
public class Program
{
static void Main(string[] args)
{
IRace irace1 = Factory.CreateInstance(RaceType.Human);
irace1.ShowKing();
IRace irace2 = Factory.CreateInstance(RaceType.OrC);
irace2.ShowKing();
IRace irace3 = Factory.CreateInstance(RaceType.NE);
irace3.ShowKing();
Console.WriteLine("***********CreateInstanceConfig******************");
IRace irace4 = Factory.CreateInstanceConfig();
irace4.ShowKing();
Console.WriteLine("***********CreateInstanceReflect******************");
IRace irace5 = Factory.CreateInstanceReflect();
irace5.ShowKing();
Console.ReadKey();
}