添加抽象工厂模式示例,包含手机和笔记本电脑的具体产品及工厂实现,并更新相关文档和构建脚本
This commit is contained in:
parent
7498a885fc
commit
c5c0cf76b2
|
|
@ -0,0 +1,2 @@
|
|||
go/build/*
|
||||
python/.venv
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
BUILDDIR := build
|
||||
DIRS := $(shell ls -d */)
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
mkdir -p $(BUILDDIR); \
|
||||
for dir in $(DIRS); do \
|
||||
if [ ! -f "$$dir/main.go" ]; then \
|
||||
continue; \
|
||||
fi; \
|
||||
pkg=$${dir%/}; \
|
||||
go build -o $(BUILDDIR)/$$pkg ./$$dir/main.go; \
|
||||
done
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
for bin in $(BUILDDIR)/*; do \
|
||||
echo "\n"; \
|
||||
$$bin; \
|
||||
done; \
|
||||
echo "\nAll Go examples executed.\n"
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -rf $(BUILDDIR)/*
|
||||
|
||||
.PHONY: all
|
||||
all: build test clean
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
# how to
|
||||
|
||||
To build the Go examples in this directory, navigate to the specific example folder and run the following command:
|
||||
|
||||
```bash
|
||||
make build ## 构建所有示例
|
||||
make test ## 运行所有示例
|
||||
make clean ## 清理构建产物
|
||||
# 或者运行所有命令
|
||||
make -s all
|
||||
```
|
||||
|
|
@ -17,7 +17,7 @@ type IPhone struct {
|
|||
}
|
||||
|
||||
func (p *IPhone) setOS() {
|
||||
fmt.Println("为该手机安装IOS系统")
|
||||
fmt.Println(" 为该手机安装IOS系统")
|
||||
}
|
||||
|
||||
// 具体产品:小米手机
|
||||
|
|
@ -26,7 +26,7 @@ type MiPhone struct {
|
|||
}
|
||||
|
||||
func (p *MiPhone) setOS() {
|
||||
fmt.Println("为该手机安装安卓系统")
|
||||
fmt.Println(" 为该手机安装安卓系统")
|
||||
}
|
||||
|
||||
// 抽象产品:笔记本电脑
|
||||
|
|
@ -40,7 +40,7 @@ type MacBook struct {
|
|||
}
|
||||
|
||||
func (l *MacBook) setOS() {
|
||||
fmt.Println("为该笔记本安装macOS系统")
|
||||
fmt.Println(" 为该笔记本安装macOS系统")
|
||||
}
|
||||
|
||||
// 具体产品:小米笔记本电脑
|
||||
|
|
@ -49,7 +49,7 @@ type MiLaptop struct {
|
|||
}
|
||||
|
||||
func (l *MiLaptop) setOS() {
|
||||
fmt.Println("为该笔记本安装Windows系统")
|
||||
fmt.Println(" 为该笔记本安装Windows系统")
|
||||
}
|
||||
|
||||
// 抽象工厂
|
||||
|
|
@ -93,20 +93,24 @@ func main() {
|
|||
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)
|
||||
fmt.Printf(" 生产了一台%s手机\n", iphone.Brand)
|
||||
iphone.setOS()
|
||||
}
|
||||
if macbook, ok := laptop.(*MacBook); ok {
|
||||
fmt.Printf("生产了一台%s笔记本电脑\n", macbook.Brand)
|
||||
fmt.Printf(" 生产了一台%s笔记本电脑\n", macbook.Brand)
|
||||
macbook.setOS()
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println("切换工厂生产其他品牌的产品...")
|
||||
fmt.Println()
|
||||
|
||||
// 选择小米工厂
|
||||
|
|
@ -115,11 +119,13 @@ func main() {
|
|||
phone = factory.CreatePhone()
|
||||
laptop = factory.CreateLaptop()
|
||||
if miPhone, ok := phone.(*MiPhone); ok {
|
||||
fmt.Printf("生产了一台%s手机\n", miPhone.Brand)
|
||||
fmt.Printf(" 生产了一台%s手机\n", miPhone.Brand)
|
||||
miPhone.setOS()
|
||||
}
|
||||
if miLaptop, ok := laptop.(*MiLaptop); ok {
|
||||
fmt.Printf("生产了一台%s笔记本电脑\n", miLaptop.Brand)
|
||||
fmt.Printf(" 生产了一台%s笔记本电脑\n", miLaptop.Brand)
|
||||
miLaptop.setOS()
|
||||
}
|
||||
|
||||
fmt.Println("====== 抽象工厂模式结束 ======")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
DIRS := $(shell ls -d */)
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
for dir in $(DIRS); do \
|
||||
echo "\n"; \
|
||||
python3 $$dir/main.py; \
|
||||
done; \
|
||||
echo "\nAll Python examples executed.\n"
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
# how to
|
||||
|
||||
To run the Python examples in this directory, navigate to the specific example folder and execute the following command:
|
||||
|
||||
```bash
|
||||
make -s test ## 运行所有示例
|
||||
```
|
||||
|
|
@ -0,0 +1,123 @@
|
|||
"""
|
||||
实例
|
||||
今天伟哥约自己在富士康工作的老乡出来喝酒,没想到老乡最近一直在加班。听说接了个大活,要同时为小米公司和苹果公司代工他们的笔记本电脑和手机生产业务。
|
||||
"""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
# 抽象产品 - 手机
|
||||
class Phone(ABC):
|
||||
brand: str
|
||||
|
||||
@abstractmethod
|
||||
def setOS(self) -> None:
|
||||
pass
|
||||
|
||||
|
||||
# 具体产品 - 苹果手机
|
||||
class Iphone(Phone):
|
||||
def __init__(self):
|
||||
self.brand = "Apple"
|
||||
|
||||
def setOS(self):
|
||||
print(" 为该手机安装IOS系统")
|
||||
|
||||
|
||||
# 具体产品 - 小米手机
|
||||
class MiPhone(Phone):
|
||||
def __init__(self):
|
||||
self.brand = "Xiaomi"
|
||||
|
||||
def setOS(self):
|
||||
print(" 为该手机安装安卓系统")
|
||||
|
||||
|
||||
# 抽象产品 - 笔记本电脑
|
||||
class Laptop(ABC):
|
||||
brand: str
|
||||
|
||||
@abstractmethod
|
||||
def setOS(self) -> None:
|
||||
pass
|
||||
|
||||
|
||||
# 具体产品 - 苹果笔记本电脑
|
||||
class Macbook(Laptop):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.brand = "Apple"
|
||||
|
||||
def setOS(self):
|
||||
print(" 为该笔记本电脑安装macOS系统")
|
||||
|
||||
|
||||
# 具体产品 - 小米笔记本电脑
|
||||
class MiLaptop(Laptop):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.brand = "Xiaomi"
|
||||
|
||||
def setOS(self):
|
||||
print(" 为该笔记本电脑安装Windows系统")
|
||||
|
||||
|
||||
# 抽象工厂
|
||||
class AbstractFactory(ABC):
|
||||
@abstractmethod
|
||||
def createPhone(self) -> Phone:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def createLaptop(self) -> Laptop:
|
||||
pass
|
||||
|
||||
|
||||
# 具体工厂 - 苹果工厂
|
||||
class AppleFactory(AbstractFactory):
|
||||
def createPhone(self):
|
||||
return Iphone()
|
||||
|
||||
def createLaptop(self):
|
||||
return Macbook()
|
||||
|
||||
|
||||
# 具体工厂 - 小米工厂
|
||||
class XiaomiFactory(AbstractFactory):
|
||||
def createPhone(self):
|
||||
return MiPhone()
|
||||
|
||||
def createLaptop(self):
|
||||
return MiLaptop()
|
||||
|
||||
|
||||
def main():
|
||||
print("======== 抽象工厂模式 ========")
|
||||
|
||||
# 伟哥先去苹果工厂生产产品
|
||||
print("伟哥选择了苹果工厂生产产品:")
|
||||
appleFactory = AppleFactory()
|
||||
applePhone = appleFactory.createPhone()
|
||||
appleLaptop = appleFactory.createLaptop()
|
||||
print(f" 生产了一台{applePhone.brand}手机")
|
||||
applePhone.setOS()
|
||||
print(f" 生产了一台{appleLaptop.brand}笔记本电脑")
|
||||
appleLaptop.setOS()
|
||||
|
||||
print("\n切换工厂生产其他品牌的产品...\n")
|
||||
|
||||
# 然后伟哥去小米工厂生产产品
|
||||
print("伟哥选择了小米工厂生产产品:")
|
||||
xiaomiFactory = XiaomiFactory()
|
||||
xiaomiPhone = xiaomiFactory.createPhone()
|
||||
xiaomiLaptop = xiaomiFactory.createLaptop()
|
||||
print(f" 生产了一台{xiaomiPhone.brand}手机")
|
||||
xiaomiPhone.setOS()
|
||||
print(f" 生产了一台{xiaomiLaptop.brand}笔记本电脑")
|
||||
xiaomiLaptop.setOS()
|
||||
|
||||
print("====== 抽象工厂模式结束 ======")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Reference in New Issue