1 概述
工厂方法类定义产品对象创建接口,但由子类实现具体产品对象的创建。
2.1 角色
- Product(抽象产品):它是具体产品的抽象类,可以是结构体,也可以是接口
- ConcreteProduct(具体产品):它实现了抽象产品接口,对应了一种具体产品
- Factroy(抽象工厂):在抽象工厂类中声明了工厂方法,用于返回一个产品。
- ConcreteFactory(具体工厂):实现了在抽象工厂中声明的工厂方法,并可由客户端调用,返回一个具体产品类的实例。
2.2 类图
classDiagram
class Product{
<<interface>>
+Get()
}
class ConcreteProductA{
+Get()
}
class ConcreteProductB{
+Get()
}
Product <|.. ConcreteProductA
Product <|.. ConcreteProductB
class Factroy{
<<interface>>
+CreateProduct()
}
class ConcreteFactroyA{
+CreateProduct() Product
}
class ConcreteFactroyB{
+CreateProduct() Product
}
ConcreteFactroyA ..|> Factroy
ConcreteFactroyB ..|> Factroy
ConcreteProductA <.. ConcreteFactroyA
ConcreteProductB <.. ConcreteFactroyB
class Client {
}
Product <-- Client
Client --> Factroy
2 代码示例
2. 1 设计
2.2 代码
package main
import (
"fmt"
)
type Product interface {
Get()
}
type ConcreteProductA struct {
Name string
Kind string
}
func (c *ConcreteProductA) Get() {
fmt.Printf("%+v",c)
}
type ConcreteProductB struct {
Name string
Kind string
}
func (c *ConcreteProductB) Get() {
fmt.Printf("%+v",c)
}
type ConcreteProductC struct {
Name string
Kind string
}
func (c *ConcreteProductC) Get() {
fmt.Printf("%+v",c)
}
type Factroy interface {
CreateProduct(name string) Product
}
type ConcreteFactoryA struct {
}
func (f *ConcreteFactoryA) CreateProduct(name string) Product {
p := &ConcreteProductA{
Name: name,
Kind:"A",
}
return p
}
type ConcreteFactoryB struct {
}
func (f *ConcreteFactoryB) CreateProduct(name string) Product {
p := &ConcreteProductB{
Name: name,
Kind: "B",
}
return p
}
type ConcreteFactoryC struct {
}
func (f *ConcreteFactoryC) CreateProduct(name string) Product {
p := &ConcreteProductC{
Name: name,
Kind: "C",
}
return p
}
func CreateProduct(myType int64) Factroy {
switch myType {
case 1:
return &ConcreteFactoryA{}
case 2:
return &ConcreteFactoryB{}
case 3:
return &ConcreteFactoryC{}
}
return nil
}
func main() {
factory := CreateProduct(1)
product := factory.CreateProduct("nginx")
product.Get()
}
- 输出
&{Name:nginx Kind:A}
2.3 类图
classDiagram
class Product{
<<interface>>
+Get()
}
class ConcreteProductA{
+Name:string
+Kind:string
+Get()
}
class ConcreteProductB{
+Name:string
+Kind:string
+Get()
}
class ConcreteProductC{
+Name:string
+Kind:string
+Get()
}
Product <|.. ConcreteProductA
Product <|.. ConcreteProductB
Product <|.. ConcreteProductC
class Factroy{
<<interface>>
+CreateProduct()
}
class ConcreteFactroyA{
+CreateProduct() Product
}
class ConcreteFactroyB{
+CreateProduct() Product
}
class ConcreteFactroyC{
+CreateProduct() Product
}
ConcreteFactroyA ..|> Factroy
ConcreteFactroyB ..|> Factroy
ConcreteFactroyC ..|> Factroy
ConcreteProductA <.. ConcreteFactroyA
ConcreteProductB <.. ConcreteFactroyB
ConcreteProductC <.. ConcreteFactroyC
3. 简单工厂
在产品结构简单的情况下,我们可以把工厂模式简化成一个简单工厂
3.1 角色
- Product(抽象产品):它是具体产品的抽象类,可以是结构体,也可以是接口
- ConcreteProduct(具体产品):它实现了抽象产品接口,对应了一种具体产品
- Factroy(简单工厂):根据一个条件用于返回一个产品
去掉了具体工厂角色,产品由简单工厂直接返回。
3.2 类图
classDiagram
Product <|.. ConcreteProductA
Product <|.. ConcreteProductB
class ConcreteProductA{
+Create()
}
class ConcreteProductB{
+Create()
}
class Product{
<<interface>>
+Create()
}
Factroy --> Product
class Factroy{
+CreateProduct() Product
}
3.3 代码示例
3.3.1 设计
- 定义一个抽象产品
Product
- 定义三个具体产品
ConcreteProductA
、ConcreteProductB
、ConcreteProductC
- 它们各自的
Get()
方法会访问它本身
- 它们各自的
- 定义一个简单工厂
- 简单工厂的CreateProduct()方法会返回一个产品
- 调用
- 实例化一个简单工厂
- 用简单工厂创建一个产品
- 用产品的Get()方法查询结果
3.3.2 代码
package main
import (
"fmt"
)
type Product interface {
Get()
}
type ConcreteProductA struct {
Name string
Kind string
}
func (c *ConcreteProductA) Get() {
fmt.Printf("%+v", c)
}
type ConcreteProductB struct {
Name string
Kind string
}
func (c *ConcreteProductB) Get() {
fmt.Printf("%+v", c)
}
type ConcreteProductC struct {
Name string
Kind string
}
func (c *ConcreteProductC) Get() {
fmt.Printf("%+v", c)
}
type Factroy struct {
}
func (f *Factroy) CreateProduct(myType int64, name string) Product {
switch myType {
case 1:
return &ConcreteProductA{
Name: name,
Kind: "A",
}
case 2:
return &ConcreteProductB{
Name: name,
Kind: "B",
}
case 3:
return &ConcreteProductC{
Name: name,
Kind: "C",
}
}
return nil
}
func main() {
factory := &Factroy{}
product := factory.CreateProduct(1, "nginx")
product.Get()
}
- 输出
&{Name:nginx Kind:A}
3.3.3 类图
classDiagram
Product <|.. ConcreteProductA
Product <|.. ConcreteProductB
Product <|.. ConcreteProductC
class ConcreteProductA{
+Name
+Kind
+Cet()
}
class ConcreteProductB{
+Name
+Kind
+Get()
}
class ConcreteProductC{
+Name
+Kind
+Get()
}
class Product{
<<interface>>
+Get()
}
Factroy --> Product
class Factroy{
+CreateProduct() Product
}