http://www.cnblogs.com/Terrylee/archive/2006/01/04/310716.aspx轉載
2/// 日誌記錄類
3/// </summary>
4public class Log
5 {
6
7 public void WriteEvent()
8 {
9 Console.WriteLine("EventLog Success!");
10 }
11
12 public void WriteFile()
13 {
14 Console.WriteLine("FileLog Success!");
15 }
16
17 public void Write(string LogType)
18 {
19 switch(LogType.ToLower())
20 {
21 case "event":
22 WriteEvent();
23 break;
24
25 case "file":
26 WriteFile();
27 break;
28
29 default:
30 break;
31 }
32 }
33 }
34
2/// EventLog類
3/// </summary>
4public class EventLog
5{
6 public void Write()
7 {
8 Console.WriteLine("EventLog Write Success!");
9 }
10}
11
12/**//// <summary>
13/// FileLog類
14/// </summary>
15public class FileLog
16{
17 public void Write()
18 {
19 Console.WriteLine("FileLog Write Success!");
20 }
21}
22
2/// Log類
3/// </summary>
4public abstract class Log
5{
6 public abstract void Write();
7}
8
2/// EventLog類
3/// </summary>
4public class EventLog:Log
5{
6 public override void Write()
7 {
8 Console.WriteLine("EventLog Write Success!");
9 }
10}
11/**//// <summary>
12/// FileLog類
13/// </summary>
14public class FileLog:Log
15{
16 public override void Write()
17 {
18 Console.WriteLine("FileLog Write Success!");
19 }
20}
21
eventlog.Write();
2/// EventFactory類
3/// </summary>
4public class EventFactory
5{
6 public EventLog Create()
7 {
8 return new EventLog();
9 }
10}
11/**//// <summary>
12/// FileFactory類
13/// </summary>
14public class FileFactory
15{
16 public FileLog Create()
17 {
18 return new FileLog();
19 }
20}
21
2/// LogFactory類
3/// </summary>
4public abstract class LogFactory
5{
6 public abstract Log Create();
7}
8
2/// EventFactory類
3/// </summary>
4public class EventFactory:LogFactory
5{
6 public override EventLog Create()
7 {
8 return new EventLog();
9 }
10}
11/**//// <summary>
12/// FileFactory類
13/// </summary>
14public class FileFactory:LogFactory
15{
16 public override FileLog Create()
17 {
18 return new FileLog();
19 }
20}
21
2/// App類
3/// </summary>
4public class App
5{
6 public static void Main(string[] args)
7 {
8 LogFactory factory = new EventFactory();
9
10 Log log = factory.Create();
11
12 log.Write();
13 }
14}
15
2 <add key="factoryName" value="EventFactory"></add>
3</appSettings>
4
2/// App類
3/// </summary>
4public class App
5{
6 public static void Main(string[] args)
7 {
8 string strfactoryName = ConfigurationSettings.AppSettings["factoryName"];
9
10 LogFactory factory;
11 factory = (LogFactory)Assembly.Load("FactoryMethod").CreateInstance("FactoryMethod." + strfactoryName);
12
13 Log log = factory.Create();
14 log.Write();
15 }
16}
17
System.Web.IHttpHandlerFactory
2{
3 if (HttpApplicationFactory._customApplication != null)
4 {
5 return HttpApplicationFactory._customApplication;
6 }
7 if (HttpDebugHandler.IsDebuggingRequest(context))
8 {
9 return new HttpDebugHandler();
10 }
11 if (!HttpApplicationFactory._theApplicationFactory._inited)
12 {
13 lock (HttpApplicationFactory._theApplicationFactory)
14 {
15 if (!HttpApplicationFactory._theApplicationFactory._inited)
16 {
17 HttpApplicationFactory._theApplicationFactory.Init(context);
18 HttpApplicationFactory._theApplicationFactory._inited = true;
19 }
20 }
21 }
22 return HttpApplicationFactory._theApplicationFactory.GetNormalApplicationInstance(context);
23}
24
<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory" />
<add verb="*" path="*.ashx" type="System.Web.UI.SimpleHandlerFactory" />
</httpHandlers>
IHttpHandlerFactory.GetHandler是一個工廠方法模式的典型例子,在這個應用中,各個角色的設置如下:
2{
3 // Methods
4 IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated);
5 void ReleaseHandler(IHttpHandler handler);
6}
7
2
3 IHttpHandler IHttpHandlerFactory.GetHandler(
4 HttpContext context, String requestType,
5 String url, String pathTranslated ) {
6
7 return new HttpHandlerImpl();
8
9 }//IHttpHandlerFactory.GetHandler
10
11 void IHttpHandlerFactory.ReleaseHandler(
12 IHttpHandler handler) { /**//*no-op*/ }
13
14}//HttpHandlerFactoryImpl
15
16public class HttpHandlerImpl:IHttpHandler {
17
18 void IHttpHandler.ProcessRequest(HttpContext context) {
19
20 context.Response.Write("sample handler invoked");
21
22 }//ProcessRequest
23
24 bool IHttpHandler.IsReusable { get { return false; } }
25
26}//HttpHandlerImpl
27
<add verb="*" path="*.sample"
type="HttpHandlerFactoryImpl,SampleHandler" />
</httpHandlers>