/// <summary>
/// Interface for Factory
/// </summary>
/// <remarks>author Louis Liu http://wintersun.cnblogs.com </remarks>
public interface IFactory
{
ISmsProvider CreateSms();
IConfig CreateConfig();
}
public interface ISmsProvider
{
void ReadConfig(IConfig config);
}
public interface IConfig
{
string ConfigInfo();
}
public class SGIPFactory : IFactory
{
public ISmsProvider CreateSms()
{
return new SGIP();
}
public IConfig CreateConfig()
{
return new SGIPConfig();
}
}
public class SGIP : ISmsProvider
{
public void ReadConfig(IConfig config)
{
Console.WriteLine("SGIP readconfig with " + config.ConfigInfo());
}
}
public class SGIPConfig : IConfig
{
public string ConfigInfo()
{
return "SGIPCONFIG";
}
}
public class CMPPFactory : IFactory
{
public ISmsProvider CreateSms()
{
return new CMPP();
}
public IConfig CreateConfig()
{
return new SGIPConfig();
}
}
public class CMPP : ISmsProvider
{
public void ReadConfig(IConfig config)
{
Console.WriteLine("CMPP readconfig with "+config.ConfigInfo());
}
}
public class CMPPConfig : IConfig
{
public string ConfigInfo()
{
return "CMPPConfig";
}
}
/// <summary>
/// SmsPlatform
/// </summary>
/// <remarks>author Louis Liu http://wintersun.cnblogs.com </remarks>
public class SmsPlatform
{
private ISmsProvider smsProvider;
private IConfig config;
private IFactory factory1,factory2;
// Constructors auto inject
public SmsPlatform(IFactory factory)
{
smsProvider = factory.CreateSms();
config = factory.CreateConfig();
}
// Methods
public void Run()
{
smsProvider.ReadConfig(config);
}
}
配置文件:
private static void AnotherWays2()
{
IUnityContainer container = new UnityContainer();
UnityConfigurationSection section =
(UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Containers.Default.Configure(container);
SmsPlatform smf = container.Resolve<SmsPlatform>();
smf.Run();
Console.ReadLine();
}