1 interface IUser 2 { 3 void Insert(User user); 4 User GetUser(int id); 5 } 6 7 class User 8 { 9 private int _id; 10 private string _name; 11 12 public int ID 13 { 14 get { return _id; } 15 set { _id = value; } 16 } 17 18 public string Name 19 { 20 get { return _name; } 21 set { _name = value; } 22 } 23 } 24 25 class SqlServerUser : IUser 26 { 27 public void Insert(User user) 28 { 29 Console.WriteLine("在SQL Server中给User表增加一条记录"); 30 } 31 32 public User GetUser(int id) 33 { 34 Console.WriteLine("在SQL Server中更加ID得到User表一条记录"); 35 return null; 36 } 37 } 38 39 class AccessUser : IUser 40 { 41 public void Insert(User user) 42 { 43 Console.WriteLine("在Access中给User表添加一条记录"); 44 } 45 46 public User GetUser(int id) 47 { 48 Console.WriteLine("在Access中根据ID得到User表一条记录"); 49 return null; 50 } 51 }
1 interface IDepartment 2 { 3 void Insert(Department department); 4 Department GetDepartment(int id); 5 } 6 7 class Department 8 { 9 private int _id; 10 private string _name; 11 12 public int ID 13 { 14 get { return _id; } 15 set { _id = value; } 16 } 17 18 public string Name 19 { 20 get { return _name; } 21 set { _name = value; } 22 } 23 } 24 25 class SqlServerDepartment : IDepartment 26 { 27 public void Insert(Department department) 28 { 29 Console.WriteLine("在SQL Server中给Department表添加一条记录"); 30 } 31 32 public Department GetDepartment(int id) 33 { 34 Console.WriteLine("在SQL Server中根据ID得到Department表一条记录"); 35 36 return null; 37 } 38 } 39 40 class AccessDepartment : IDepartment 41 { 42 public void Insert(Department department) 43 { 44 Console.WriteLine("在Access中给Department表增加一条记录"); 45 } 46 47 public Department GetDepartment(int id) 48 { 49 Console.WriteLine("在Access中根据ID得到Department表一条记录"); 50 51 return null; 52 } 53 }
1 interface IFactory 2 { 3 IUser CreateUser(); 4 IDepartment CreateDepartment(); 5 } 6 7 class SqlServerFactory : IFactory 8 { 9 public IUser CreateUser() 10 { 11 return new SqlServerUser(); 12 } 13 14 public IDepartment CreateDepartment() 15 { 16 return new SqlServerDepartment(); 17 } 18 } 19 20 class AccessFactory : IFactory 21 { 22 public IUser CreateUser() 23 { 24 return new AccessUser(); 25 } 26 27 public IDepartment CreateDepartment() 28 { 29 return new AccessDepartment(); 30 } 31 }
转自《大话设计模式》