• Go实现常用软件设计模式二:工厂模式


    目录:

    1. 举个栗子
    2. 概念介绍
    3. 使用场景

    1.举个栗子

     类图 

    ```

    @startuml
    'https://plantuml.com/class-diagram

    class Elephant {
    String name
    String getName()
    }

    interface Container {
    List<Elephant> elephants
    void add(Elephant elephants)
    }

    class Refrigerator {
    List<Elephant> elephants;
    void add(Elephant elephants);
    }

    class Bowl {
    List<Elephant> elephants
    void add(Elephant elephants)
    }

    class Box {
    List<Elephant> elephants
    void add(Elephant elephants)
    }

    Container <|- Refrigerator
    Container <|- Bowl
    Container <|- Box

    class ContainerFactory {
    Container getInstance(String containerType)
    }

    class ContainerCounter {
    Container container
    void countTotalNum()
    }

    class Main {
    void main();
    }

    @enduml

    ```

    2.概念介绍

    创建一个中间件来分离创建对象和使用对象的逻辑

    优点:

    将创建对象的逻辑放在一起

    扩展新的相同行为的对象无需修改使用者的逻辑

    封装复杂的对象创建逻辑

    缺点:

    多了一个工厂类

    3.使用场景

    main.go

    ```

    package main

    import (
    "dbTest/Builder/factory/container/containerFactory"
    "dbTest/Builder/factory/containerCounter"
    "dbTest/Builder/factory/elephant"
    "fmt"
    "time"
    )


    func main() {
    fmt.Println("start add elephant")
    elephants := elephant.Elephant{
    "rampage",
    }
    var containerType string
    containerType = "refrigerator"
    Contains := containerFactory.GetInstance(containerType)
    Contains.AddElephant(elephants)
    containerCount := containerCounter.ContainerCounter{}
    for i := 0; i < 10; i++ {
    go containerCount.CounterNum(Contains)
    }
    time.Sleep(time.Second)
    fmt.Println("we are finished, nums:",Contains.GetTotalNum())
    }



    ```

     elephat.go

    ```

    package elephant

    type Elephant struct {
    Name string
    }

    func (e Elephant) getName() string {
    return e.Name
    }

    ```

    containerCounter.go

    ```

    package containerCounter

    import (
    "dbTest/Builder/factory/container/containerInterface"
    "fmt"
    )

    type ContainerCounter struct {

    }

    func (rc ContainerCounter) CounterNum(container containerInterface.Container) {
    fmt.Println("ContainerCounter:",container.GetTotalNum())
    return
    }

    ```

    containerFactory.go

    ```

    package containerFactory

    import (
    "dbTest/Builder/factory/container/bowl"
    "dbTest/Builder/factory/container/box"
    "dbTest/Builder/factory/container/containerInterface"
    "dbTest/Builder/factory/container/refrigerator"
    )

    func GetInstance(containerType string) containerInterface.Container{
    switch containerType {
    case "bowl":
    return &bowl.Bowl{}
    case "box":
    return &box.Box{}
    case "refrigerator":
    return &refrigerator.Refrigerator{}
    }
    return nil
    }

    ```

    container.go

    ```

    package containerInterface

    import "dbTest/Builder/factory/elephant"

    type Container interface {
    AddElephant(elephant elephant.Elephant)
    GetTotalNum() int
    }

    ```

    refrigerator.go

    ```

    package refrigerator

    import (
    "dbTest/Builder/factory/elephant"
    )

    // 1、设计为小写字母开头,表示只在network包内可见,限制客户端程序的实例化
    type Refrigerator struct {
    elephants []elephant.Elephant
    }

    func (r *Refrigerator) AddElephant(elephants elephant.Elephant) {
    r.elephants = append(r.elephants, elephants)
    }

    func (r *Refrigerator) GetTotalNum() int {
    return len(r.elephants)
    }

    ```

    box.go

    ```

    package box

    import "dbTest/Builder/factory/elephant"

    // 1、设计为小写字母开头,表示只在network包内可见,限制客户端程序的实例化
    type Box struct {
    elephants []elephant.Elephant
    }

    func (r *Box) AddElephant(elephants elephant.Elephant) {
    r.elephants = append(r.elephants, elephants)
    }

    func (r *Box) GetTotalNum() int {
    return len(r.elephants)
    }

    ```

    bowl.go

    ```

    package bowl

    import "dbTest/Builder/factory/elephant"

    // 1、设计为小写字母开头,表示只在network包内可见,限制客户端程序的实例化
    type Bowl struct {
    elephants []elephant.Elephant
    }

    func (r *Bowl) AddElephant(elephants elephant.Elephant) {
    r.elephants = append(r.elephants, elephants)
    }

    func (r *Bowl) GetTotalNum() int {
    return len(r.elephants)
    }

    ```

  • 相关阅读:
    【从0安装】安装pycharm
    【从0安装】安装appium
    【从0安装】安装android sdk
    【从0安装】安装java jdk
    【从0安装】安装nodejs
    【技术解析】如何用Docker实现SequoiaDB集群的快速部署
    巨杉数据库助力民生银行、广发银行前台智慧化业务
    巨杉数据库入选Gartner数据库报告,中国首家入选厂商
    【操作教程】利用YCSB测试巨杉数据库性能
    巨杉数据库首批入选广州数字金融协会,引领大湾区数字金融创新
  • 原文地址:https://www.cnblogs.com/gongxianjin/p/16443149.html
Copyright © 2020-2023  润新知