最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

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

来源:博客园


(资料图)

概述

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

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

描述需求

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

抽象工厂实现代码

///     /// 顶层抽象工厂    ///     public interface IFacatory    {        IDatabaseUser GetDatabaseUser();        IDatabaseDepartment GetDatabaseDepartment();    }    ///     /// 具体工厂    ///     public class SqlliteFacatory : IFacatory    {        public IDatabaseDepartment GetDatabaseDepartment()        {            return  new SqlliteDepartment();        }        public IDatabaseUser GetDatabaseUser()        {            return new SqliteUser();        }    }    ///     /// 具体工厂    ///     public class MysqlFacatory : IFacatory    {        public IDatabaseDepartment GetDatabaseDepartment()        {            return new MysqlDepartment();        }        public IDatabaseUser GetDatabaseUser()        {            return new MysqlUser();        }    }    ///     /// 具体工厂    ///     public class SqlserverFacatory : IFacatory    {        public IDatabaseDepartment GetDatabaseDepartment()        {            return new SqlServerDepartment();        }        public IDatabaseUser GetDatabaseUser()        {            return new SqlServerUser();        }    }
///     /// 用户类    ///     public class User    {        public string  Name { get; set; }        public int Id { get; set; }    }    ///     /// 抽象的产品    ///     public interface IDatabaseUser    {        void InsertUser(User user);        User GetUser(int id);    }    ///     /// 具体的产品    ///     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;        }    }    ///     /// 具体的产品    ///     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;        }    }    ///     /// 具体的产品    ///     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;        }    }
///     /// 部门类    ///     public class Department    {        public string Name { get; set; }        public int Id { get; set; }    }    ///     /// 抽象的产品    ///     public interface IDatabaseDepartment    {        void InsertDepartment(Department department);        Department GetDepartment(int id);    }    ///     /// 具体的产品    ///     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;        }    }    ///     /// 具体的产品    ///     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;        }    }    ///     /// 具体的产品    ///     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 //SqlliteFacatoryIDatabaseUser 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。

总结

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

关键词: 工厂模式 增加一个 解决方案