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

View File

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

View File

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

View File

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

View File

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