• 抽象工厂方法


     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     }
    View Code
     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     }
    View Code
     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     }
    View Code

     转自《大话设计模式》

  • 相关阅读:
    两台独立计算机共享一套键鼠
    【不要太监,成为笑话】拼搏百天,我要成为程序员!键盘敲烂,我要月薪过万!第三天
    【不要太监,成为笑话】拼搏百天,我要成为程序员!键盘敲烂,我要月薪过万!第二天
    【不要太监,成为笑话】拼搏百天,我要成为程序员!键盘敲烂,我要月薪过万!第一天
    javac编译时报错 (错误: 编码GBK的不可映射字符)
    JavaWeb学习1-springMVC
    理学和工学的区别
    各种证书
    问题
    字符编码的理解
  • 原文地址:https://www.cnblogs.com/yixiu868/p/6559114.html
Copyright © 2020-2023  润新知