1
0
Fork 0

优化抽象工厂模式示例,添加品牌方法并重构工厂实现,更新构建脚本以支持更清晰的输出

This commit is contained in:
IvisTang 2025-12-09 10:56:44 +08:00
parent c5c0cf76b2
commit 59f61e54fd
5 changed files with 97 additions and 63 deletions

View File

@ -1,9 +1,16 @@
BUILDDIR := build BUILDDIR := build
DIRS := $(shell ls -d */) DIRS := $(shell ls -d */)
ifndef VERBOSE
.SILENT:
endif
all: build test clean
.PHONY: build .PHONY: build
build: build:
mkdir -p $(BUILDDIR); \ echo "Building Go examples..."
mkdir -p $(BUILDDIR)
for dir in $(DIRS); do \ for dir in $(DIRS); do \
if [ ! -f "$$dir/main.go" ]; then \ if [ ! -f "$$dir/main.go" ]; then \
continue; \ continue; \
@ -17,12 +24,11 @@ test:
for bin in $(BUILDDIR)/*; do \ for bin in $(BUILDDIR)/*; do \
echo "\n"; \ echo "\n"; \
$$bin; \ $$bin; \
done; \ echo "\n"; \
echo "\nAll Go examples executed.\n" done
echo "All Go examples executed."
.PHONY: clean .PHONY: clean
clean: clean:
echo "Cleaning build directory..."
rm -rf $(BUILDDIR)/* rm -rf $(BUILDDIR)/*
.PHONY: all
all: build test clean

View File

@ -7,5 +7,5 @@ make build ## 构建所有示例
make test ## 运行所有示例 make test ## 运行所有示例
make clean ## 清理构建产物 make clean ## 清理构建产物
# 或者运行所有命令 # 或者运行所有命令
make -s all make
``` ```

View File

@ -9,6 +9,7 @@ import "fmt"
// 抽象产品:手机 // 抽象产品:手机
type Phone interface { type Phone interface {
setOS() setOS()
brand() string
} }
// 具体产品:苹果手机 // 具体产品:苹果手机
@ -20,6 +21,10 @@ func (p *IPhone) setOS() {
fmt.Println(" 为该手机安装IOS系统") fmt.Println(" 为该手机安装IOS系统")
} }
func (p *IPhone) brand() string {
return p.Brand
}
// 具体产品:小米手机 // 具体产品:小米手机
type MiPhone struct { type MiPhone struct {
Brand string Brand string
@ -29,9 +34,14 @@ func (p *MiPhone) setOS() {
fmt.Println(" 为该手机安装安卓系统") fmt.Println(" 为该手机安装安卓系统")
} }
func (p *MiPhone) brand() string {
return p.Brand
}
// 抽象产品:笔记本电脑 // 抽象产品:笔记本电脑
type Laptop interface { type Laptop interface {
setOS() setOS()
brand() string
} }
// 具体产品:苹果笔记本电脑 // 具体产品:苹果笔记本电脑
@ -43,6 +53,10 @@ func (l *MacBook) setOS() {
fmt.Println(" 为该笔记本安装macOS系统") fmt.Println(" 为该笔记本安装macOS系统")
} }
func (l *MacBook) brand() string {
return l.Brand
}
// 具体产品:小米笔记本电脑 // 具体产品:小米笔记本电脑
type MiLaptop struct { type MiLaptop struct {
Brand string Brand string
@ -52,6 +66,10 @@ func (l *MiLaptop) setOS() {
fmt.Println(" 为该笔记本安装Windows系统") fmt.Println(" 为该笔记本安装Windows系统")
} }
func (l *MiLaptop) brand() string {
return l.Brand
}
// 抽象工厂 // 抽象工厂
type Factory interface { type Factory interface {
CreatePhone() Phone CreatePhone() Phone
@ -59,73 +77,71 @@ type Factory interface {
} }
// 具体工厂:苹果工厂 // 具体工厂:苹果工厂
type AppleFactory struct{} type AppleFactory struct {
name string
}
func (f *AppleFactory) CreatePhone() Phone { func (f *AppleFactory) CreatePhone() Phone {
phone := &IPhone{} phone := &IPhone{Brand: "Apple"}
phone.Brand = "Apple"
return phone return phone
} }
func (f *AppleFactory) CreateLaptop() Laptop { func (f *AppleFactory) CreateLaptop() Laptop {
laptop := &MacBook{} laptop := &MacBook{Brand: "Apple"}
laptop.Brand = "Apple"
return laptop return laptop
} }
func newAppleFactory() Factory {
return &AppleFactory{name: "苹果工厂"}
}
// 具体工厂:小米工厂 // 具体工厂:小米工厂
type MiFactory struct{} type MiFactory struct {
name string
}
func (f *MiFactory) CreatePhone() Phone { func (f *MiFactory) CreatePhone() Phone {
phone := &MiPhone{} phone := &MiPhone{Brand: "Xiaomi"}
phone.Brand = "Xiaomi"
return phone return phone
} }
func (f *MiFactory) CreateLaptop() Laptop { func (f *MiFactory) CreateLaptop() Laptop {
laptop := &MiLaptop{} laptop := &MiLaptop{Brand: "Xiaomi"}
laptop.Brand = "Xiaomi"
return laptop return laptop
} }
func newMiFactory() Factory {
return &MiFactory{name: "小米工厂"}
}
func client(factory Factory) {
phone := factory.CreatePhone()
laptop := factory.CreateLaptop()
fmt.Printf(" 生产了一台%s手机\n", phone.brand())
phone.setOS()
fmt.Printf(" 生产了一台%s笔记本电脑\n", laptop.brand())
laptop.setOS()
}
func main() { func main() {
var factory Factory var factory Factory
var phone Phone
var laptop Laptop
fmt.Println("======== 抽象工厂模式 ========") fmt.Println("======== 抽象工厂模式 ========")
// 选择苹果工厂 // 选择苹果工厂
fmt.Println("伟哥选择了苹果工厂生产产品:") factory = newAppleFactory()
factory = &AppleFactory{} fmt.Printf("伟哥选择了%s生产产品:\n", factory.(*AppleFactory).name)
phone = factory.CreatePhone() client(factory)
laptop = factory.CreateLaptop()
if iphone, ok := phone.(*IPhone); ok {
fmt.Printf(" 生产了一台%s手机\n", iphone.Brand)
iphone.setOS()
}
if macbook, ok := laptop.(*MacBook); ok {
fmt.Printf(" 生产了一台%s笔记本电脑\n", macbook.Brand)
macbook.setOS()
}
fmt.Println() fmt.Println()
fmt.Println("切换工厂生产其他品牌的产品...") fmt.Println("切换工厂生产其他品牌的产品...")
fmt.Println() fmt.Println()
// 选择小米工厂 // 选择小米工厂
fmt.Println("伟哥选择了小米工厂生产产品:") factory = newMiFactory()
factory = &MiFactory{} fmt.Printf("伟哥选择了%s生产产品:\n", factory.(*MiFactory).name)
phone = factory.CreatePhone() client(factory)
laptop = factory.CreateLaptop()
if miPhone, ok := phone.(*MiPhone); ok {
fmt.Printf(" 生产了一台%s手机\n", miPhone.Brand)
miPhone.setOS()
}
if miLaptop, ok := laptop.(*MiLaptop); ok {
fmt.Printf(" 生产了一台%s笔记本电脑\n", miLaptop.Brand)
miLaptop.setOS()
}
fmt.Println("====== 抽象工厂模式结束 ======") fmt.Println("====== 抽象工厂模式结束 ======")
} }

View File

@ -1,9 +1,13 @@
DIRS := $(shell ls -d */) DIRS := $(shell ls -d */)
.PHONY: test ifndef VERBOSE
test: .SILENT:
endif
all:
for dir in $(DIRS); do \ for dir in $(DIRS); do \
echo "\n"; \ echo "\n"; \
python3 $$dir/main.py; \ python3 $$dir/main.py; \
done; \ echo "\n"; \
echo "\nAll Python examples executed.\n" done
echo "All Python examples executed.\n"

View File

@ -64,6 +64,8 @@ class MiLaptop(Laptop):
# 抽象工厂 # 抽象工厂
class AbstractFactory(ABC): class AbstractFactory(ABC):
name: str
@abstractmethod @abstractmethod
def createPhone(self) -> Phone: def createPhone(self) -> Phone:
pass pass
@ -75,6 +77,9 @@ class AbstractFactory(ABC):
# 具体工厂 - 苹果工厂 # 具体工厂 - 苹果工厂
class AppleFactory(AbstractFactory): class AppleFactory(AbstractFactory):
def __init__(self):
self.name = "苹果工厂"
def createPhone(self): def createPhone(self):
return Iphone() return Iphone()
@ -84,6 +89,9 @@ class AppleFactory(AbstractFactory):
# 具体工厂 - 小米工厂 # 具体工厂 - 小米工厂
class XiaomiFactory(AbstractFactory): class XiaomiFactory(AbstractFactory):
def __init__(self):
self.name = "小米工厂"
def createPhone(self): def createPhone(self):
return MiPhone() return MiPhone()
@ -91,30 +99,30 @@ class XiaomiFactory(AbstractFactory):
return MiLaptop() return MiLaptop()
def client(factory: AbstractFactory):
phone = factory.createPhone()
laptop = factory.createLaptop()
print(f" 生产了一台{phone.brand}手机")
phone.setOS()
print(f" 生产了一台{laptop.brand}笔记本电脑")
laptop.setOS()
def main(): def main():
print("======== 抽象工厂模式 ========") print("======== 抽象工厂模式 ========")
# 伟哥先去苹果工厂生产产品 # 伟哥先去苹果工厂生产产品
print("伟哥选择了苹果工厂生产产品:") print("伟哥选择了苹果工厂生产产品:")
appleFactory = AppleFactory() factory = AppleFactory()
applePhone = appleFactory.createPhone() print(f"伟哥选择了{factory.name}生产产品:")
appleLaptop = appleFactory.createLaptop() client(factory)
print(f" 生产了一台{applePhone.brand}手机")
applePhone.setOS()
print(f" 生产了一台{appleLaptop.brand}笔记本电脑")
appleLaptop.setOS()
print("\n切换工厂生产其他品牌的产品...\n") print("\n切换工厂生产其他品牌的产品...\n")
# 然后伟哥去小米工厂生产产品 # 然后伟哥去小米工厂生产产品
print("伟哥选择了小米工厂生产产品:") factory = XiaomiFactory()
xiaomiFactory = XiaomiFactory() print(f"伟哥选择了{factory.name}生产产品:")
xiaomiPhone = xiaomiFactory.createPhone() client(factory)
xiaomiLaptop = xiaomiFactory.createLaptop()
print(f" 生产了一台{xiaomiPhone.brand}手机")
xiaomiPhone.setOS()
print(f" 生产了一台{xiaomiLaptop.brand}笔记本电脑")
xiaomiLaptop.setOS()
print("====== 抽象工厂模式结束 ======") print("====== 抽象工厂模式结束 ======")