
import (
"fmt"
)
//ProductA is abstract product A
type ProductA interface {
TestA()
}
//ProductB is abstract product B
type ProductB interface {
TestB()
}
//Factory is abstract factory
type Factory interface {
CreateA() ProductA
CreateB() ProductB
}
//ProductA1 is concrete product A1
type ProductA1 struct{}
//TestA is implementation of the ProductA interface method
func (p ProductA1) TestA() {
fmt.Println("test A1")
}
//ProductB1 is concrete product B1
type ProductB1 struct{}
//TestB is implementation of the ProductB interface method
func (p ProductB1) TestB() {
fmt.Println("test B1")
}
//ProductA2 is concrete product A2
type ProductA2 struct{}
//TestA is implementation of the ProductA interface method
func (p ProductA2) TestA() {
fmt.Println("test A2")
}
//ProductB2 is concrete product B2
type ProductB2 struct{}
//TestB is implementation of the ProductB interface method
func (p ProductB2) TestB() {
fmt.Println("test B2")
}
//Factory1 is concrete factory 1
type Factory1 struct{}
//CreateA is implementation of the Factory interface method
func (f Factory1) CreateA() ProductA {
return ProductA1{}
}
//CreateB is implementation of the Factory interface method
func (f Factory1) CreateB() ProductB {
return ProductB1{}
}
//Factory2 is concrete factory 2
type Factory2 struct{}
//CreateA is implementation of the Factory interface method
func (f Factory2) CreateA() ProductA {
return ProductA2{}
}
//CreateB is implementation of the Factory interface method
func (f Factory2) CreateB() ProductB {
return ProductB2{}
}
//client code:
//TestFactory creates and tests factories
func TestFactory(factory Factory) {
productA := factory.CreateA()
productB := factory.CreateB()
productA.TestA()
productB.TestB()
}
TestFactory(Factory1{})
//printed: test A1
// test B1
TestFactory(Factory2{})
//printed: test A2
// test B2
Provides an interface for creating families of objects whose interfaces are known but concrete classes are not.Read moreThe problem. A program draws its windows in two styles, light and dark. Each style needs its own button and its own menu. If the code creates them directly, it can mix a light button with a dark menu by mistake. A third style would also mean a search for every place where a button or a menu is created. The idea. An abstract factory is an interface with one creation method for each kind of product. Each concrete factory implements it for one family. One factory creates the light button and the light menu; another creates the dark ones. The client receives a factory and creates every product through it. It never names a concrete product, so the products it gets always belong to one family. To change the whole family, you give the client a different factory. How to read the example. There are two kinds of product, A and B. A concrete factory makes one family: one product of each kind, marked with the same number. The client receives a factory, asks it for one product of each kind and uses both. The client does not know which family it got, but the output shows it. Some languages declare no interfaces at all, but the roles stay the same. When to use it. Use Abstract Factory when products must work together and there are several families of them, such as visual themes or operating systems. Do not use it when there is only one family. It is also not needed when the products do not depend on each other: a constructor or a Factory Method is enough. A common mistake. Choosing this pattern while the kinds of product still change often. Each new kind adds a method to the abstract factory and to every concrete factory. |