第一部分·创建型模式-05-抽象工厂模式(Abstract Fa

2023-12-13  本文已影响0人  玄德公笔记

1. 概述

1.1 角色

1.2 类图

image.png
classDiagram
  class ProductA{
  <<interface>>
  }
  class ProductA1{
  }
  class ProductA2{
  }
  ProductA <|.. ProductA1
  ProductA  <|.. ProductA2
  class ProductB{
  <<interface>>
  }
  class ProductB1{
  }
  class ProductB2{
  }
  ProductB <|.. ProductB1
  ProductB  <|.. ProductB2
  class Client{
  }
  Client --> ProductA
  Client --> ProductB
  Client --> AbstractFactory
  class AbstractFactory{
  <<interface>>
  +CreateProductA() ProductA
  +CreateProductB() ProductB
  }
  class ConcreteFactory1{
  +CreateProductA() ProductA
  +CreateProductB() ProductB
  }
  class ConcreteFactory2{
  +CreateProductA() ProductA
  +CreateProductB() ProductB
  }
   AbstractFactory <|.. ConcreteFactory1
   AbstractFactory <|.. ConcreteFactory2   
  ProductA1 <.. ConcreteFactory1
  ProductB1 <.. ConcreteFactory1
  ProductA2 <.. ConcreteFactory2
  ProductB2 <.. ConcreteFactory2

2. 代码示例

2.1 设计

有冰箱和电视两种商品,但是我们创建的时候需要从另一个维度来分类——它的品牌。因此我们以西蜀牌和东吴牌分别创建两个具体工厂,每个具体工厂负责创建自己品牌的冰箱和电视。

2.2 代码

package main

import "fmt"

// 定义抽象产品——冰箱
type Fridge interface {
    SetBrand(brand string)
    SetType(productType string)
    SetTemperature(temperature int64)
    Get()
}

// 定义实际产品蜀国牌冰箱
type FridgeShu struct {
    Brand       string
    ProductType string
    Temperature int64
}

func (c *FridgeShu) SetBrand(brand string) {
    c.Brand = brand
}

func (c *FridgeShu) SetType(productType string) {
    c.ProductType = productType
}

func (c *FridgeShu) SetTemperature(temperature int64) {
    c.Temperature = temperature
}

func (c *FridgeShu) Get() {
    fmt.Printf("%+v\n", c)
}

// 定义实际产品东吴牌冰箱
type FridgeWu struct {
    Brand       string
    ProductType string
    Temperature int64
}

func (c *FridgeWu) SetBrand(brand string) {
    c.Brand = brand
}

func (c *FridgeWu) SetType(productType string) {
    c.ProductType = productType
}

func (c *FridgeWu) SetTemperature(temperature int64) {
    c.Temperature = temperature
}

func (c *FridgeWu) Get() {
    fmt.Printf("%+v\n", c)
}

// 定义第二个抽象产品电视机
type TV interface {
    SetBrand(brand string)
    SetType(productType string)
    SetPPI(ppi string)
    Get()
}

// 定义西蜀牌电视机
type TVShu struct {
    Brand       string
    ProductType string
    PPI         string
}

func (c *TVShu) SetBrand(brand string) {
    c.Brand = brand
}

func (c *TVShu) SetType(productType string) {
    c.ProductType = productType
}

func (c *TVShu) SetPPI(ppi string) {
    c.PPI = ppi
}

func (c *TVShu) Get() {
    fmt.Printf("%+v\n", c)
}

// 定义东吴牌电视机
type TVWu struct {
    Brand       string
    ProductType string
    PPI         string
}

func (c *TVWu) SetBrand(brand string) {
    c.Brand = brand
}

func (c *TVWu) SetType(productType string) {
    c.ProductType = productType
}

func (c *TVWu) SetPPI(ppi string) {
    c.PPI = ppi
}

func (c *TVWu) Get() {
    fmt.Printf("%+v\n", c)
}

// Factory部分

// 定义抽象工厂
type Factory interface {
    SetFridge(temperature int64) Fridge
    SetTV() TV
}

// 定义具体工厂——西蜀工厂
type FactoryShu struct {
}

func (f *FactoryShu) SetFridge(temperature int64) Fridge {
    a := &FridgeShu{}
    a.SetBrand("蜀")
    a.SetTemperature(temperature)
    a.SetType("冰箱")
    return a
}

func (f *FactoryShu) SetTV() TV {
    p := &TVShu{}
    p.SetBrand("蜀")
    p.SetType("电视")
    p.SetPPI("8k")
    return p
}

// 定义具体工厂——东吴工厂
type FactoryWu struct {
}

func (f *FactoryWu) SetFridge(temperature int64) Fridge {
    a := &FridgeWu{}
    a.SetBrand("蜀")
    a.SetTemperature(temperature)
    a.SetType("冰箱")
    return a
}

func (f *FactoryWu) SetTV() TV {
    p := &TVWu{}
    p.SetBrand("蜀")
    p.SetType("电视")
    p.SetPPI("4k")
    return p
}

// 写一个创建工厂的函数
func CreateFactory(pType int64) Factory {
    switch pType {
    case 1:
        return &FactoryShu{}
    case 2:
        return &FactoryWu{}
    }
    return nil
}

func main() {
    shuFactory := CreateFactory(1)
    shuFactory.SetFridge(4).Get()
    shuFactory.SetTV().Get()

    wuFactory := CreateFactory(2)
    wuFactory.SetFridge(5).Get()
    wuFactory.SetTV().Get()
}

&{Brand:蜀 ProductType:冰箱 Temperature:4}
&{Brand:蜀 ProductType:电视 PPI:8k}       
&{Brand:蜀 ProductType:冰箱 Temperature:5}
&{Brand:蜀 ProductType:电视 PPI:4k}   

2.3 类图

image.png
classDiagram
  class Fridge  {
   <<interface>>
    +SetBrand(brand string)
    +SetType(productType string)
    +SetTemperature(temperature int64)
    +Get()
  }
  
  class FridgeShu {
    +Brand:String
    +ProductType:String
    +Temperature:Int64
    +SetBrand(brand string)
    +SetType(productType string)
    +SetTemperature(temperature int64)
    +Get()
  }

  class FridgeWu {
    +Brand:String
    +ProductType:String
    +Temperature:Int64
    +SetBrand(brand string)
    +SetType(productType string)
    +SetTemperature(temperature int64)
    +Get()
  }
  
  Fridge <|.. FridgeShu
  Fridge <|.. FridgeWu
  class TV {
    << nterface>>
    +SetBrand(brand string)
    +SetType(productType string)
    +SetPPI(ppi string)
    +Get()
  }

  class TVShu{
    +Brand:String
    +ProductType:String
    +PPI:String
    +SetBrand(brand string)
    +SetType(productType string)
    +SetPPI(ppi string)
    +Get()
  }

  class TVWu{
    +Brand:String
    +ProductType:String
    +PPI:String
    +SetBrand(brand string)
    +SetType(productType string)
    +SetPPI(ppi string)
    +Get()
  }
  TV <|.. TVShu
  TV <|.. TVWu
  class Factory  {
  <<interface>>
    +SetFridge(temperature int64) Fridge
    +SetTV() TV
  }

class FactoryShu  {
    +SetFridge(temperature int64) Fridge
    +SetTV() TV
}
FridgeShu <.. FactoryShu
TVShu <.. FactoryShu

class FactoryWu {
    +SetFridge(temperature int64) Fridge
    +SetTV() TV
}
FridgeWu <.. FactoryWu
TVWu <.. FactoryWu
Factory <|-- FactoryShu
Factory <|-- FactoryWu

  class Client{
  }
  Client --> Fridge
  Client --> TV
  Client --> Factory

上一篇 下一篇

猜你喜欢

热点阅读