设计模式学习--工厂模式
1.概述
工厂模式根据提供给他的数据,返回一个可能类中的一个类的实例。
2.示例
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Reflection;
6
7namespace Factory
8{
9 class Program
10 {
11 public static readonly string DAL = "OracleDAL";
12
13 static void Main(string[] args)
14 {
15 IDAL dal = (IDAL)Assembly.Load("Factory").CreateInstance("Factory." + DAL);
16 dal.DataAccess();
17 Console.ReadKey();
18 }
19 }
20
21 public interface IDAL
22 {
23 void DataAccess();
24 }
25
26 public class SqlServerDAL : IDAL
27 {
28 public void DataAccess()
29 {
30 Console.WriteLine("SqlServerDAL");
31 }
32 }
33
34 public class OracleDAL : IDAL
35 {
36 public void DataAccess()
37 {
38 Console.WriteLine("OracleDAL");
39 }
40 }
41}
42
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Reflection;
6
7namespace Factory
8{
9 class Program
10 {
11 public static readonly string DAL = "OracleDAL";
12
13 static void Main(string[] args)
14 {
15 IDAL dal = (IDAL)Assembly.Load("Factory").CreateInstance("Factory." + DAL);
16 dal.DataAccess();
17 Console.ReadKey();
18 }
19 }
20
21 public interface IDAL
22 {
23 void DataAccess();
24 }
25
26 public class SqlServerDAL : IDAL
27 {
28 public void DataAccess()
29 {
30 Console.WriteLine("SqlServerDAL");
31 }
32 }
33
34 public class OracleDAL : IDAL
35 {
36 public void DataAccess()
37 {
38 Console.WriteLine("OracleDAL");
39 }
40 }
41}
42