再有人问你抽象工厂设计模式,把这篇文章丢给他

概述

抽象工厂模式为创建一组对象提供了一种解决方案。与工厂方法模式相比,抽象工厂模式中的具体工厂不只是创建一种产品,它负责创建一组产品。抽象工厂模式定义如下:

抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。抽象工厂模式又称为Kit模式,它是一种对象创建型模式。

描述需求

分别往SQLserver、mysql、sqliter数据库中添加用户和部门数据。

抽象工厂实现代码

   /// <summary>
    /// 顶层抽象工厂
    /// </summary>
    public interface IFacatory
    {
        IDatabaseUser GetDatabaseUser();
        IDatabaseDepartment GetDatabaseDepartment();
    }
    /// <summary>
    /// 具体工厂
    /// </summary>
    public class SqlliteFacatory : IFacatory
    {
        public IDatabaseDepartment GetDatabaseDepartment()
        {
            return  new SqlliteDepartment();
        }
        public IDatabaseUser GetDatabaseUser()
        {
            return new SqliteUser();
        }
    }
    /// <summary>
    /// 具体工厂
    /// </summary>
    public class MysqlFacatory : IFacatory
    {
        public IDatabaseDepartment GetDatabaseDepartment()
        {
            return new MysqlDepartment();
        }
        public IDatabaseUser GetDatabaseUser()
        {
            return new MysqlUser();
        }
    }
    /// <summary>
    /// 具体工厂
    /// </summary>
    public class SqlserverFacatory : IFacatory
    {
        public IDatabaseDepartment GetDatabaseDepartment()
        {
            return new SqlServerDepartment();
        }
        public IDatabaseUser GetDatabaseUser()
        {
            return new SqlServerUser();
        }
    }
    /// <summary>
    /// 用户类
    /// </summary>
    public class User
    {
        public string  Name { get; set; }
        public int Id { get; set; }
    }
    /// <summary>
    /// 抽象的产品
    /// </summary>
    public interface IDatabaseUser
    {
        void InsertUser(User user);
        User GetUser(int id);
    }
    /// <summary>
    /// 具体的产品
    /// </summary>
    public class SqlServerUser: IDatabaseUser
    {
        public void InsertUser(User user)
        {
            Console.WriteLine($"插入了SqlServerUser:{user.Name}");
        }
        public User GetUser(int id)
        {
            Console.WriteLine($"获取了Id:{id}的用户");
            return null;
        }
    }
    /// <summary>
    /// 具体的产品
    /// </summary>
    public class MysqlUser : IDatabaseUser
    {
        public void InsertUser(User user)
        {
            Console.WriteLine($"插入了MysqlUser:{user.Name}");
        }
        public User GetUser(int id)
        {
            Console.WriteLine($"获取了Id:{id}的用户");
            return null;
        }
    }
    /// <summary>
    /// 具体的产品
    /// </summary>
    public class SqliteUser : IDatabaseUser
    {
        public void InsertUser(User user)
        {
            Console.WriteLine($"插入了SqliteUser:{user.Name}");
        }
        public User GetUser(int id)
        {
            Console.WriteLine($"获取了Id:{id}的用户");
            return null;
        }
    }
   /// <summary>
    /// 部门类
    /// </summary>
    public class Department
    {
        public string Name { get; set; }
        public int Id { get; set; }
    }
    /// <summary>
    /// 抽象的产品
    /// </summary>
    public interface IDatabaseDepartment
    {
        void InsertDepartment(Department department);
        Department GetDepartment(int id);
    }
    /// <summary>
    /// 具体的产品
    /// </summary>
    public class SqlServerDepartment : IDatabaseDepartment
    {
        public void InsertDepartment(Department  department)
        {
            Console.WriteLine($"插入了SqlServerDepartment部门:{department.Name}");
        }
        public Department GetDepartment(int id)
        {
            Console.WriteLine($"获取了Id:{id}的部门");
            return null;
        }
    }
    /// <summary>
    /// 具体的产品
    /// </summary>
    public class MysqlDepartment : IDatabaseDepartment
    {
        public void InsertDepartment(Department department)
        {
            Console.WriteLine($"插入了MysqlDepartment部门:{department.Name}");
        }
        public Department GetDepartment(int id)
        {
            Console.WriteLine($"获取了Id:{id}的部门");
            return null;
        }
    }
    /// <summary>
    /// 具体的产品
    /// </summary>
    public class SqlliteDepartment : IDatabaseDepartment
    {
        public void InsertDepartment(Department department)
        {
            Console.WriteLine($"插入了SqlliteDepartment部门:{department.Name}");
        }
        public Department GetDepartment(int id)
        {
            Console.WriteLine($"获取了Id:{id}的部门");
            return null;
        }
    }
//C#控制台调用
Console.WriteLine("抽象工厂");
User user = new();
user.Name = "mhg";
user.Id = 1;
//变化点
IFacatory facatory=new SqlserverFacatory(); //MysqlFacatory //SqlliteFacatory
IDatabaseUser databaseUser = facatory.GetDatabaseUser();
databaseUser.InsertUser(user);
databaseUser?.GetUser(user.Id);
Department department = new Department();
department.Name = "研发部";
department.Id = 2;
IDatabaseDepartment databaseDepartment=  facatory.GetDatabaseDepartment();
databaseDepartment.InsertDepartment(department);    
databaseDepartment?.GetDepartment(department.Id);

以上就用抽象工厂实现的全部代码,该代码中IFacatory做为抽象工厂,SqlserverFacatory、MysqlFacatory、SqliteFacatory作为具体的产品工厂来生产产品;IDatabaseUser和IDatabaseDepartment为抽象的产品分别生产具体的产品SqlServerUser、MysqlUser、SqliteUser和SqlServerDepartment、MysqlDepartment、SqlliteDepartment。

总结

这个需求也可以用工厂模式实现,但每增加一个产品,就必须增加一个工厂类,而用抽象工厂可以缩减创建子类工厂的数量,不必给每一个产品增加工厂类,可以对产品进行分组,每组中的不同产品由同一个工厂类的不同方法来创建。

posted @ 2023-02-16 08:43  realyrare  阅读(226)  评论(0编辑  收藏  举报