转载

简单工厂模式,工厂方法模式,抽象工厂模式

以前都是以记录笔记的方式写很短的随笔;然而这一次比较长,由于时间,表达能力,理解能力有限,肯定有很多不完整和错误的地方,请各位多多包含,后期会不断完善和修改;谢谢!

面向对象方法设计就是用封装,继承,多态来实现代码的可维护,可扩展,可复用,灵活性好。

面向过程编程:

从以前面向过程编程开始,很多代码都写在一起,比如对一用户,具备获取与新增它的功能。

static void Main(string[] args) {  try  {   Console.Write("请输入数字Get or Insert:");   string strOperation=Console.ReadLine();   switch(strOperation)   {    case "Get": 
Console.WriteLine("在SQL Server中给Department表增加一条记录");
break; case "Insert":
Console.WriteLine("在SQL Server中根据ID得到给Department表一条记录");
break; } } catch(Exception e) { Console.WriteLine("程序出现异常:"+e.Message); } }

但以上代码修改时会牵一发而动全身,很可能牵扯到其他不应该修改的代码,如果代码长的话进行维护时甚至自己都不认识,而且代码的复用性也不高。这时面向对象编程就起到了作用。所以我们需要对代码进行封装。

面向对象编程,对界面与业务的分离

public class Department {     public static string GetResult(string strOperation)   {    string result=string.Empty;    switch(strOperation)    {      case "Insert": result = "在SQL Server中给Department表增加一条记录"; 
break; case "Get": result = "在SQL Server中根据ID得到给Department表一条记录";
break; } return result; } } public class Program { static void Main(string[] args) { try { Console.Write("请输入数字Insert or Get:"); string strOperation=Console.ReadLine(); string strResult = Department.GetResult(strOperation); Console.WriteLine(strResult); } catch(Exception e) { Console.WriteLine("程序出现异常:"+e.Message); } } }

但是,这时,如果我们需要对业务中新增其他功能,如删除,那就会对修改开放了,违背了“开放-封闭”原则:只能对扩张开放,而不能对修改开放。所以我们需要对操作进行分离和封装。

public class Department  {   public string strResult = string.Empty;   public virtual string GetResult()   {             return strResult;   }  }  class InsertSqlserverDepartment : Department  {    public override string GetResult()    {     strResult = "在SQL Server中给Department表增加一条记录";     return strResult;    }  }  class GetSqlserverDepartment : Department  {    public override string GetResult()    {     strResult = "在SQL Server中根据ID得到给Department表一条记录";     return strResult;    }  }  public class Program   {     static void Main(string[] args)     {      //Console.WriteLine("请输入你所要的操作,Insert or Get");      //string strOperation = Console.ReadLine();      Department oper;      oper = new GetSqlserverDepartment();      string strResult = oper.GetResult();      Console.WriteLine(strResult);      Console.ReadLine();     }   }    
但是我想

1.简单工程模式

public class Department {  public string strResult = string.Empty;  public virtual string GetResult()  {            return strResult;  } } class InsertSqlserverDepartment : Department {   public override string GetResult()   {    strResult = "在SQL Server中给Department表增加一条记录";    return strResult;   } } class GetSqlserverDepartment : Department {   public override string GetResult()   {    strResult = "在SQL Server中根据ID得到给Department表一条记录";    return strResult;   } } public class DepartmentFactory {  public static Department createDepartmentOperation(string type)  {   Department dept = null;   switch (type)   {    case "Insert": dept = new InsertSqlserverDepartment(); break;    case "Get": dept = new GetSqlserverDepartment(); break;   }   return dept;  } } public class Program  {    static void Main(string[] args)    {     Console.WriteLine("请输入你所要的操作,Insert or Get");     string strOperation = Console.ReadLine();     Department oper;     oper =DepartmentFactory.createDepartmentOperation(strOperation);     string strResult = oper.GetResult();     Console.WriteLine(strResult);     Console.ReadLine();    }  }   

但是,我们可能操作不同数据库的同一个产品,此时就要添加另一种数据操作类了,以下的模式就派上用场了。

2.工厂方法模式

class Department    {     public int ID{get;set;}     public int DeptName{get;set;}    }    interface IDepartment    {     void Insert(Department dept);     Department GetDepartment(int id);    }    class SqlserverDepartment : IDepartment    {     public void Insert(Department department)     {      Console.WriteLine("在SQL Server中给Department表增加一条记录");     }     public void GetDepartment(int id)     {      Console.WriteLine("在SQL Server中根据ID得到给Department表一条记录");     }    }    class OracleDepartment : IDepartment    {     public void Insert(Department department)     {      Console.WriteLine("在Oracle中给Department表增加一条记录");     }     public void GetDepartment(int id)     {      Console.WriteLine("在Oracle中根据ID得到给Department表一条记录");     }    }    interface IFactory    {     IDepartment CreateDepartment();    }    class SqlServerFactory : IFactory    {     public IDepartment CreateDepartment()     {      return new SqlserverDepartment();     }    }    class OracleFactory : IFactory    {     public IDepartment CreateDepartment()     {      return new OracleDepartment();     }    }    public class Program     {    static void Main(string[] args)    {        Department dept = new Department();     //IFactory factory = new SqlServerFactory();     IFactory factory = new OracleFactory();     IDepartment idept = factory.CreateDepartment();     idept.Insert(dept);     idept.GetDepartment(1);     Console.ReadLine();    } }    

但是我们一般操作数据库,不只是操作一张表,很可能还会有用户表,那我们继续添加,这里就有了以下的设计模式

3.抽象工厂模式

  class Department {  public int ID{get;set;}  public int DeptName{get;set;} } class User {  public int ID { get; set; }  public int UserName { get; set; } } interface IDepartment {  void Insert(Department department);  Department GetDepartment(int id); } interface IUser {  void Insert(User user);  User GetUser(int id); } class SqlserverDepartment : IDepartment {  public void Insert(Department department)  {   Console.WriteLine("在SQL Server中给Department表增加一条记录");  }  public void GetDepartment(int id)  {   Console.WriteLine("在SQL Server中根据ID得到给Department表一条记录");  } } class OracleDepartment : IDepartment {  public void Insert(Department department)  {   Console.WriteLine("在Oracle中给Department表增加一条记录");  }  public void GetDepartment(int id)  {   Console.WriteLine("在Oracle中根据ID得到给Department表一条记录");  } } class SqlserverUser : IUser {  public void Insert(User user)  {   Console.WriteLine("在SQL Server中给User表增加一条记录");  }  public void GetDUser(int id)  {   Console.WriteLine("在SQL Server中根据ID得到给User表一条记录");  } } class OracleUser : IUser {  public void Insert(User user)  {   Console.WriteLine("在Oracle中给User表增加一条记录");  }  public void GetUser(int id)  {   Console.WriteLine("在Oracle中根据ID得到给User表一条记录");  } } interface IFactory {  IUser CreateUser();  IDepartment CreateDepartment(); } class SqlServerFactory : IFactory {  public IUser CreateUser()  {   return new SqlserverUser();  }  public IDepartment CreateDepartment()  {   return new SqlserverDepartment();  } } class OracleFactory : IFactory {  public IUser CreateUser()  {   return new OracleUser();  }  public IDepartment CreateDepartment()  {   return new OracleDepartment();  } } class Program {  static void Main(string[] args)  {   User user = new User();   Department dept = new Department();   //IFactory factory = new SqlServerFactory();   IFactory factory = new OracleFactory();   IUser iu = factory.CreateUser();   iu.Insert(user);   iu.GetUser(1);   IDepartment idept = factory.CreateDepartment();   idept.Insert(dept);   idept.GetDepartment(1);   Console.ReadLine();  } } 

总结:

简单工厂模式:他的最大优点就是在于工厂类包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖,当算法比较稳定,一般不会对它进行新增,或删除等就适合用词模式;否则就会违背了“开放-封闭”原则

工厂方法模式:它定义了用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类。

抽象方法模式:当产品有不同的系列,而不同的系列又有不同的创建方式,此时就适合用此模式

正文到此结束
Loading...