using System.Reflection;
using System.Configuration;
namespace C15_4
{
//宣告一個介面,該介面有一個顯示 "問候" 的 功能
public interface IHello
{
void ShowHello(string name);
}
//實作介面,顯示中文問候
public class ChineseHello : IHello
{
public void ShowHello(string name)
{
Console.WriteLine("你好!{0}", name);
}
}
//實作介面,顯示英文問候
public class EnglishHello : IHello
{
public void ShowHello(string name)
{
Console.WriteLine("Hello!{0}", name);
}
}
//抽象工廠
public class HelloFactory
{
public static IHello GetHello()
{
string oname = "";
//從配置檔App.config中獲取該顯示問候類型
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["1"]))
{
oname = ConfigurationManager.AppSettings["1"];
}
IHello name = (IHello)Assembly.Load("C15_4").CreateInstance(oname);
return name;
}
public class Test
{
public static void Main()
{
IHello name = HelloFactory.GetHello();
name.ShowHello("小張張");
}
}
}
}
/*
配置檔:App.config
*/
沒有留言:
張貼留言