C# design pattern --- abstract factory pattern

Abstract Factory pattern (Abstract Factory)

Abstract Factory Pattern (Abstract Factory Pattern) is a super factory to create other factories. The super factory is also known as the factory of other factories. This type of design pattern is a creational pattern, which provides an optimal way to create objects. The factory method pattern is designed to overcome the shortcomings of the simple factory pattern. The factory class of the simple factory pattern requires additional code to increase the product class, while the factory method pattern only completes the creation of a single instance for each specific factory class. It has good scalability. But in real life, a factory only creates a single product, and the factories are diversified. A factory creates a series of products. If we want to design such a system, the factory method pattern is not suitable, and the abstract factory pattern is used. But it can be solved very well.

using System;
namespace ConsoleApplication
{
    //一般每个接口或类都写在单独的.cs文件中
    //本示例为了执行查看方便才写在一起
    public interface IDatabase
    {
        void Delete();
        void Insert();
    }
    public interface IORM
    {
        void Delete();
        void Insert();
    }
    //Sqlserver的增删
    public class Sqlserver : IDatabase
    {
        public void Delete()
        {
            Console.WriteLine("delete data from sqlserver");
        }
        public void Insert()
        {
            Console.WriteLine("insert data to sqlserver");
        }
    }
    //Mysql的增删
    public class Mysql : IDatabase
    {
        public void Delete()
        {
            Console.WriteLine("delete data from Mysql");
        }
        public void Insert()
        {
            Console.WriteLine("insert data to Mysql");
        }
    }
  //Sqlserver的增删
    public class SqlserverORM : IORM
    {
        public void Delete()
        {
            Console.WriteLine("delete data from sqlserver by ORM");
        }
        public void Insert()
        {
            Console.WriteLine("insert data to sqlserver by ORM");
        }
    }
    //Mysql的增删
    public class MysqlORM : IORM
    {
        public void Delete()
        {
            Console.WriteLine("delete data from Mysql by ORM");
        }
        public void Insert()
        {
            Console.WriteLine("insert data to Mysql by ORM");
        }
    }
    public interface ICreator
    {
        IDatabase CreateDatabase();
        IORM CreateORM();
    }
    public class MysqlFactory:ICreator
    {
        /// 
        /// 负责创建Mysql
        /// 
        /// 
        public IDatabase CreateDatabase()
        {
            return new Mysql();
        }
       /// 
        /// 负责创建MysqlORM
        /// 
        /// 
        public IORM CreateORM()
        {
            return new MysqlORM();
        }
    }
    public class SqlserverFactory:ICreator
    {
        /// 
        /// 负责创建Sqlserver
        /// 
        /// 
        public IDatabase CreateDatabase()
        {
            return new Sqlserver();
        }
        /// 
        /// 负责创建SqlserverORM
        /// 
        /// 
        public IORM CreateORM()
        {
            return new SqlserverORM();
        }
    }
    /*public static class IFactory
    {
        //根据需求创建
        public static Database CreateDatabase(string dbType)
        {
            Database db = null;
            switch (dbType)
            {
                case "Sqlserver":
                    db = new Sqlserver();
                    break;
                case "Mysql":
                    db = new Mysql();
                    break;
                default:
                    break;
            }
            return db;
        }
    }*/
    class Program
    {
        static void Main(string[] args)
        {
            IDatabase db1 = new SqlserverFactory().CreateDatabase();
            db1.Delete();
            db1.Insert();
            IDatabase db2 = new MysqlFactory().CreateDatabase();
            db2.Delete();
            db2.Insert();
            IORM db3 = new SqlserverFactory().CreateORM();
            db3.Delete();
            db3.Insert();
            IORM db4 = new MysqlFactory().CreateORM();
            db4.Delete();
            db4.Insert();
            //添加新数据库直接添加对应的类即可
            Console.ReadKey();
        }
    }
}

 

Guess you like

Origin blog.csdn.net/lwf3115841/article/details/132057189