国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

抽象工廠設(shè)計模式go實現(xiàn)嘗試

這篇具有很好參考價值的文章主要介紹了抽象工廠設(shè)計模式go實現(xiàn)嘗試。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。


前言

本文章嘗試使用go實現(xiàn)“抽象工廠”。


代碼

package main

import (
	"fmt"
)

// 所有系列產(chǎn)品A接口
type IAbstractProductA interface {
	UsefulFunctionA() string
}

// 系列1具體產(chǎn)品A
type ConcreteProductA1 struct {
}

func (ConcreteProductA1) UsefulFunctionA() string {
	return "The result of the product A1."
}

// 系列2具體產(chǎn)品A
type ConcreteProductA2 struct {
}

func (ConcreteProductA2) UsefulFunctionA() string {
	return "The result of the product A2."
}

// 所有系列產(chǎn)品B接口
type IAbstractProductB interface {
	UsefulFunctionB() string
	AnotherUsefulFunctionB(collaborator IAbstractProductA) string
}

// 系列1具體產(chǎn)品B
type ConcreteProductB1 struct {
}

func (ConcreteProductB1) UsefulFunctionB() string {
	return "The result of the product B1."
}

func (ConcreteProductB1) AnotherUsefulFunctionB(collaborator IAbstractProductA) string {
	result := collaborator.UsefulFunctionA()
	return fmt.Sprintf("The result of the B1 collaborating with the (%s)", result)
}

// 系列2具體產(chǎn)品B
type ConcreteProductB2 struct {
}

func (ConcreteProductB2) UsefulFunctionB() string {
	return "The result of the product B2."
}

func (ConcreteProductB2) AnotherUsefulFunctionB(collaborator IAbstractProductA) string {
	result := collaborator.UsefulFunctionA()
	return fmt.Sprintf("The result of the B2 collaborating with the (%s)", result)
}

// 抽象工廠接口定義一系列方法返回同個系列的若干抽象產(chǎn)品
type IAbstractFactory interface {
	CreateProductA() IAbstractProductA
	CreateProductB() IAbstractProductB
}

// 具體工廠1
type ConcreteFactory1 struct {
}

func (ConcreteFactory1) CreateProductA() IAbstractProductA {
	return &ConcreteProductA1{}
}

func (ConcreteFactory1) CreateProductB() IAbstractProductB {
	return &ConcreteProductB1{}
}

// 具體工廠2
type ConcreteFactory2 struct {
}

func (ConcreteFactory2) CreateProductA() IAbstractProductA {
	return &ConcreteProductA2{}
}

func (ConcreteFactory2) CreateProductB() IAbstractProductB {
	return &ConcreteProductB2{}
}

// 客戶端代碼
func clientCode(factory IAbstractFactory) {
	product_a := factory.CreateProductA()
	product_b := factory.CreateProductB()
	fmt.Println(product_b.UsefulFunctionB())
	fmt.Println(product_b.AnotherUsefulFunctionB(product_a))
}

func main() {
	fmt.Println("Client: Testing client code with the first factory type:")
	clientCode(&ConcreteFactory1{})
	fmt.Println()
	fmt.Println("Client: Testing the same client code with the second factory type:")
	clientCode(&ConcreteFactory2{})
}

結(jié)果

Client: Testing client code with the first factory type:
The result of the product B1.
The result of the B1 collaborating with the (The result of the product A1.)

Client: Testing the same client code with the second factory type:
The result of the product B2.
The result of the B2 collaborating with the (The result of the product A2.)

總結(jié)

新人設(shè)計模式理解,望大家多多指點。文章來源地址http://www.zghlxwxcb.cn/news/detail-589059.html

到了這里,關(guān)于抽象工廠設(shè)計模式go實現(xiàn)嘗試的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 原型設(shè)計模式go實現(xiàn)嘗試

    本文章嘗試使用go實現(xiàn)“原型”。 新人設(shè)計模式理解,望大家多多指點。

    2024年02月15日
    瀏覽(22)
  • 自學(xué)設(shè)計模式(簡單工廠模式、工廠模式、抽象工廠模式)

    使用工廠模式來生產(chǎn)某類對象(代碼簡化且容易維護,類之間有血緣關(guān)系,可以通過工廠類進行生產(chǎn)); 簡單工廠模式(用于創(chuàng)建簡單對象) 對于簡單工廠模式,需要的工廠類只有一個; 在工廠類中的公共成員函數(shù)來創(chuàng)建所需對象; 工廠模式 簡單工廠模式會違反開放封閉

    2024年02月11日
    瀏覽(27)
  • 【設(shè)計模式】單例模式、工廠方法模式、抽象工廠模式

    1. 單例模式 (Singleton Pattern): 場景: 在一個應(yīng)用程序中,需要一個全局唯一的配置管理器,確保配置信息只有一個實例。 2. 工廠方法模式 (Factory Method Pattern): 場景: 創(chuàng)建一組具有相似功能但具體實現(xiàn)不同的日志記錄器。 3. 抽象工廠模式 (Abstract Factory Pattern): 場景: 創(chuàng)建不同

    2024年01月15日
    瀏覽(29)
  • 設(shè)計模式-抽象工廠模式

    抽象工廠模式:該模式是對工廠模式的拓展,因為工廠模式中創(chuàng)建的產(chǎn)品都需要繼承自同一個父類或接口,創(chuàng)建的產(chǎn)品類型相同,無法創(chuàng)建其他類型產(chǎn)品,所以抽象工廠模式對其進行拓展,使其可以創(chuàng)建其他類型的產(chǎn)品。 手機產(chǎn)品 Pad產(chǎn)品 工廠 優(yōu)點:創(chuàng)建的產(chǎn)品種類不單一

    2024年02月13日
    瀏覽(22)
  • 【設(shè)計模式】抽象工廠模式

    【設(shè)計模式】抽象工廠模式

    抽象工廠模式(Abstract Factory Pattern)是圍繞一個超級工廠創(chuàng)建其他工廠。該超級工廠又稱為其他工廠的工廠。這種類型的設(shè)計模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對象的最佳方式。 在抽象工廠模式中,接口是負責(zé)創(chuàng)建一個相關(guān)對象的工廠,不需要顯式指定它們的類。每個

    2024年02月13日
    瀏覽(28)
  • 設(shè)計模式 - 抽象工廠模式

    設(shè)計模式 - 抽象工廠模式

    學(xué)完工廠模式,才發(fā)現(xiàn)還有一個抽象工廠模式;學(xué)習(xí)后發(fā)現(xiàn)不論是通過接口方式、還是繼承方式,都可以使用抽象工廠模式;但是個人建議更多的時候,我們可以優(yōu)先考慮接口方式,畢竟 單繼承,多實現(xiàn) 設(shè)計模式分為三種類型,共23種 創(chuàng)建型模式:單例模式、工廠模式、抽

    2024年02月13日
    瀏覽(28)
  • 設(shè)計模式(三):抽象工廠模式

    設(shè)計模式(三):抽象工廠模式

    抽象工廠模式 (Abstract Factory Pattern)屬于創(chuàng)建型模式,是圍繞一個超級工廠創(chuàng)建其他工廠。該超級工廠又稱為其他工廠的工廠。 在 抽象工廠模式 中,接口是負責(zé)創(chuàng)建一個相關(guān)對象的工廠,不需要顯式指定它們的類。每個生成的工廠都能按照工廠模式提供對象。 通過使用 抽

    2024年04月25日
    瀏覽(23)
  • 設(shè)計模式(3)抽象工廠模式

    設(shè)計模式(3)抽象工廠模式

    一、介紹: 1、定義:提供一個創(chuàng)建一系列相關(guān)或相互依賴對象的接口,而無須指定它們具體的類。 2、組成結(jié)構(gòu): (1)抽象工廠角色:擔(dān)任這個角色的是工廠方法模式的核心,它是與應(yīng)用程序無關(guān)的。任何在模式中創(chuàng)建對象的工廠類必須實現(xiàn)這個接口。 (2)具體工廠角色

    2024年02月11日
    瀏覽(23)
  • 重溫設(shè)計模式 --- 抽象工廠模式

    抽象工廠模式 一種創(chuàng)建型設(shè)計模式,它提供了一種方式來封裝一組具有相同主題的工廠,而不必指定它們具體的類。這樣,客戶端代碼就可以使用抽象工廠來創(chuàng)建一組相關(guān)的對象,而不必關(guān)心實際創(chuàng)建的具體類。 抽象工廠模式有以下幾個主要角色: 抽象工廠(AbstractFactory)

    2024年02月13日
    瀏覽(37)
  • 【設(shè)計模式專題之抽象工廠模式】3. 家具工廠

    題目描述 小明家新開了兩個工廠用來生產(chǎn)家具,一個生產(chǎn)現(xiàn)代風(fēng)格的沙發(fā)和椅子,一個生產(chǎn)古典風(fēng)格的沙發(fā)和椅子,現(xiàn)在工廠收到了一筆訂單,請你幫他設(shè)計一個系統(tǒng),描述訂單需要生產(chǎn)家具的信息。 輸入描述 輸入的第一行是一個整數(shù) N(1 ≤ N ≤ 100),表示訂單的數(shù)量。

    2024年03月12日
    瀏覽(31)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包