The schema definition

Provides an interface to create a series of related or interdependent objects without specifying their specific classes

The class diagram

Application scenarios

The abstract factory pattern can be used when a program needs to deal with a different family of related products, but you don’t want it to depend on specific classes for those products

advantages

1. Make sure that the products you get from the factory are compatible with each other;

2. Avoid tight coupling between product specific and client code;

3. Comply with the principle of single responsibility;

4. Comply with the Open Closed Principle

The point to summarize

  • There is no need to use the Abstract Factory pattern if there is no need to address the changing requirements of “multi-family object construction,” and a simple Factory is perfectly fine
  • “Series of objects” refers to the interdependent or interacting relationship between objects in a particular series, and objects in different series cannot depend on each other
  • The Abstract Factory pattern is mainly designed to cope with changes in the requirements of “new series”, but its disadvantage is that it is difficult to cope with changes in the requirements of “new objects”

Go language code implementation

Project directory

DaoFactory.go

Package AbstractFactory // Type OrderMainDAO interface {SaveOrderMain() //SearchOrderMain()} // Type OrderDetailDao interface {SaveOrderDetail()} // ODAOFactory interface = OTAOFactory interface = OTAOFactory interface = OTAOFactory interface = CreateOrderMainDao() OrderMainDao CreateOrderDetailDao() OrderDetailDao }

MYSQLDetailDaoFactory.go

package AbstractFactory

import "fmt"

type MYSQLMainDao struct {

}

func (*MYSQLMainDao) SaveOrderMain() {
   fmt.Println("MYSQL main save")
}

type MYSQLDetailDao struct {

}

func (*MYSQLDetailDao) SaveOrderDetail() {
   fmt.Println("MYSQL detail save")
}

MYSQLFactory.go

Package abstractFactory // type mysqlFactory struct {} func (* mysqlFactory) createOrderMainDAO () OrderMainDAO { return &MYSQLMainDao{} } func (*MYSQLFactory) CreateOrderDetailDao() OrderDetailDao { return &MYSQLDetailDao{} }

OracleFactory.go

Package AbstractFactory type oracleFactory struct {} func (* oracleFactory) createOrderMainDAO () OrderMainDAO { return &OracleMainDao{} } func (*OracleFactory) CreateOrderDetailDao() OrderDetailDao { return &OracleDetailDao{} }

OracleDetailDaoFactory.go

package AbstractFactory

import "fmt"

type OracleMainDao struct {

}

func (*OracleMainDao) SaveOrderMain() {
   fmt.Println("Oracle main save")
}

type OracleDetailDao struct {

}

func (*OracleDetailDao) SaveOrderDetail() {
   fmt.Println("Oracle detail save")
}

abstractFactory_test.go

package AbstractFactory import "testing" func TestMySqlFactory_CreateOrderMainDao(t *testing.T) { var factory DaoFactory  factory = &MYSQLFactory{} factory.CreateOrderMainDao().SaveOrderMain() factory.CreateOrderDetailDao().SaveOrderDetail()  factory = &OracleFactory{} factory.CreateOrderDetailDao().SaveOrderDetail() factory.CreateOrderMainDao().SaveOrderMain() }