. net design pattern (2): 1.2 abstract factory pattern

Time:2022-5-6

summary

Abstract factory pattern is the most abstract and general of all factory patterns. Abstract factory pattern refers to a factory pattern used when there are multiple Abstract roles. The abstract factory pattern can provide an interface for the client to create product objects in multiple product families without specifying the specific products.


definition

Abstract factory pattern provides an interface to create a series of related or interdependent objects without specifying their specific classes.


UML diagram

  • AbstractFactory: declares an operation interface for creating abstract product objects

  • ConcreteFactory: implements the operation of creating specific product objects

  • Abstractproduct: declare a kind of product object interface

  • Product

    • Define a product object created by the corresponding specific factory

    • Implement abstractproduct interface

  • Client: interface declared using AbstractFactory and abstractproduct classes

In the abstract factory mode, the creation of products is completed by ConcreteFactory. As can be seen from the structure diagram, ConcreteFactory in the abstract factory mode is not responsible for the creation of a specific product, but for the creation of a product family.


realization

explain:
1. Taking the blog system as an example, this implementation extracts two objects: users and articles.
2. The three-tier architecture is adopted. In order to save trouble, the business logic layer (BLL) is removed. I hope to understand.

Example project structure diagram

Sample project class diagram

  • Blog. Models: model layer

  • Blog. Idal: data access layer interface (class I product object interface [abstractproduct])

  • Blog. Dal: data access layer classification implementation (abstract product specific classification Implementation)

    • MSSQL: mssql implementation (abstract product specific classification implementation [producta1, producta2])

    • MySQL: MySQL implementation (abstract product specific classification implementation [productb1, productb2])

  • Blog. Factory: Abstract Factory layer

    • Lfactory: abstract factory interface ([AbstractFactory])

    • Mssqlfactory: mssql specific factory (operation of creating specific product object [concretefactory1])

    • MySQL factory: MySQL specific factory (operation of creating specific product object [concretefactory2])

Model layer (blog. Models)

  • UserModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractFactorySamlpe.Blog.Models
{
    public class UserModel
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
    }
}
  • PostModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractFactorySamlpe.Blog.Models
{
    public class PostModel
    {
        public int PostId { get; set; }
        public string PostTitle { get; set; }
        public string PostContent { get; set; }
        public DateTime PostTime{ get; set; }
        public int PostUser { get; set; }
    }
}

Data access layer interface (blog. Idal)

  • IUserDAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.Models;

namespace AbstractFactorySamlpe.Blog.IDAL
{
    public interface IUserDAL
    {
        bool Insert(UserModel model);

        UserModel GetById(int id);

        bool Update(UserModel model);

        bool Delete(int id);
    }
}
  • IPostDAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.Models;

namespace AbstractFactorySamlpe.Blog.IDAL
{
    public interface IPostDAL
    {
        bool Insert(PostModel model);

        PostModel GetById(int id);

        bool Update(PostModel model);

        bool Delete(int id);
    }
}

Specific implementation of data access layer classification (blog. DAL)

  • Blog.DAL.MsSql

    • MsSqlPostDAL

using AbstractFactorySamlpe.Blog.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.Models;

namespace AbstractFactorySamlpe.Blog.DAL.MsSql
{
    public class MsSqlPostDAL : IPostDAL
    {
        public bool Delete(int id)
        {
            return true;
        }

        public PostModel GetById(int id)
        {
            return new PostModel();
        }

        public bool Insert(PostModel model)
        {
            return true;
        }

        public bool Update(PostModel model)
        {
            return true;
        }
    }
}
  • MsSqlUserDAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.IDAL;
using AbstractFactorySamlpe.Blog.Models;

namespace AbstractFactorySamlpe.Blog.DAL.MsSql
{
    public class MsSqlUserDAL : IUserDAL
    {
        public bool Delete(int id)
        {
            return true;
        }

        public UserModel GetById(int id)
        {
            return new UserModel();
        }

        public bool Insert(UserModel model)
        {
            return true;
        }

        public bool Update(UserModel model)
        {
            return true;
        }
    }
}
  • Blog.DAL.MySql

    • MySqlPostDAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.IDAL;
using AbstractFactorySamlpe.Blog.Models;

namespace AbstractFactorySamlpe.Blog.DAL.MySql
{
    public class MySqlPostDAL : IPostDAL
    {
        public bool Delete(int id)
        {
            return true;
        }

        public PostModel GetById(int id)
        {
            return new PostModel();
        }

        public bool Insert(PostModel model)
        {
            return true;
        }

        public bool Update(PostModel model)
        {
            return true;
        }
    }
}
  • MySqlUserDAL

using AbstractFactorySamlpe.Blog.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.Models;

namespace AbstractFactorySamlpe.Blog.DAL.MySql
{
    public class MySqlUserDAL : IUserDAL
    {
        public bool Delete(int id)
        {
            return true;
        }

        public UserModel GetById(int id)
        {
            return new UserModel();
        }

        public bool Insert(UserModel model)
        {
            return true;
        }

        public bool Update(UserModel model)
        {
            return true;
        }
    }
}

Abstract factory (blog. Factory)

  • IFactory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.IDAL;

namespace AbstractFactorySamlpe.Blog.Factory
{
    public interface IFactory
    {

        IUserDAL CreateUser();

        IPostDAL CreatePost();
    }
}
  • MsSqlFactory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.DAL.MsSql;
using AbstractFactorySamlpe.Blog.IDAL;

namespace AbstractFactorySamlpe.Blog.Factory
{
    public class MsSqlFactory : IFactory
    {
        public IPostDAL CreatePost()
        {
            return new MsSqlPostDAL();
        }

        public IUserDAL CreateUser()
        {
            return new MsSqlUserDAL();
        }
    }
}
  • MySqlFactory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.DAL.MySql;
using AbstractFactorySamlpe.Blog.IDAL;

namespace AbstractFactorySamlpe.Blog.Factory
{
    public class MySqlFactory : IFactory
    {
        public IPostDAL CreatePost()
        {
            return new MySqlPostDAL();
        }

        public IUserDAL CreateUser()
        {
            return new MySqlUserDAL();
        }
    }
}

Call (client)

Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AbstractFactorySamlpe.Blog.Factory;
using AbstractFactorySamlpe.Blog.IDAL;

namespace AbstractFactorySamlpe
{
    class Program
    {
        static void Main(string[] args)
        {
            //MSSQL
            //IFactory factory = new MsSqlFactory();
            //IUserDAL UserDAL = factory.CreateUser();
            //IPostDAL PostDAL = factory.CreatePost();

            //MySQL
            IFactory factory = new MySqlFactory();
            IUserDAL UserDAL = factory.CreateUser();
            IPostDAL PostDAL = factory.CreatePost();
        }
    }
}

advantage:

1. Encapsulation, the implementation class of each product is not the concern of high-level modules.
2. Constraints within the product family are non-public.

Disadvantages:

Product family expansion is very difficult.

Usage scenario:

If an object family or a group of objects without any relationship have the same constraints, you can use the abstract factory pattern.

matters needing attention:
Product family expansion is difficult, and product grade expansion is very easy, that is, horizontal expansion is easy and vertical expansion is difficult.