diff --git a/go/flyweight/main.go b/go/flyweight/main.go index c7985f9..b9b747e 100644 --- a/go/flyweight/main.go +++ b/go/flyweight/main.go @@ -7,6 +7,11 @@ import "fmt" 汤宝宝开了一家服装租赁店,好多客人来店里租衣服~ */ +// 享元抽象类 +type ClothingInterface interface { + Show() +} + // 享元对象 - 服装 type Clothing struct { style string @@ -20,16 +25,16 @@ func (c *Clothing) Show() { // 享元工厂 - 服装店 type ClothingFactory struct { - clothingPool map[string]*Clothing + clothingPool map[string]ClothingInterface } func NewClothingFactory() *ClothingFactory { return &ClothingFactory{ - clothingPool: make(map[string]*Clothing), + clothingPool: make(map[string]ClothingInterface), } } -func (f *ClothingFactory) GetClothing(style, size, color string) *Clothing { +func (f *ClothingFactory) GetClothing(style, size, color string) ClothingInterface { key := style + "-" + size + "-" + color if clothing, exists := f.clothingPool[key]; exists { return clothing @@ -50,17 +55,17 @@ func (f *ClothingFactory) ShowStock() { // 客人 type Customer struct { name string - clothing []*Clothing + clothing []ClothingInterface } func NewCustomer(name string) *Customer { return &Customer{ name: name, - clothing: []*Clothing{}, + clothing: []ClothingInterface{}, } } -func (c *Customer) RentClothing(clothing *Clothing) { +func (c *Customer) RentClothing(clothing ClothingInterface) { fmt.Printf("%s 租了一件服装: ", c.name) clothing.Show() c.clothing = append(c.clothing, clothing) diff --git a/python/flyweight/main.py b/python/flyweight/main.py index e266d91..21657eb 100644 --- a/python/flyweight/main.py +++ b/python/flyweight/main.py @@ -3,8 +3,15 @@ 汤宝宝开了一家服装租赁店,好多客人来店里租衣服~ """ +from abc import ABC, abstractmethod -class Clothing: +# 享元接口 +class ClothingInterface(ABC): + @abstractmethod + def show(self): + pass + +class Clothing(ClothingInterface): """享元对象 - 服装""" style: str @@ -23,12 +30,12 @@ class Clothing: class ClothingFactory: """享元工厂 - 服装店""" - _clothing_pool: dict[str, Clothing] + _clothing_pool: dict[str, ClothingInterface] def __init__(self): self._clothing_pool = {} - def get_clothing(self, style: str, size: str, color: str) -> Clothing: + def get_clothing(self, style: str, size: str, color: str) -> ClothingInterface: key = f"{style}-{size}-{color}" if key not in self._clothing_pool: self._clothing_pool[key] = Clothing(style, size, color) @@ -43,13 +50,13 @@ class ClothingFactory: class Customer: name: str - clothing: list[Clothing] + clothing: list[ClothingInterface] def __init__(self, name: str): self.name = name self.clothing = [] - def rent_clothing(self, clothing: Clothing): + def rent_clothing(self, clothing: ClothingInterface): self.clothing.append(clothing) print(f"{self.name} 租了一件服装: ", end="") clothing.show() diff --git a/ts/src/flyweight/index.ts b/ts/src/flyweight/index.ts index 53175b5..bdff66a 100644 --- a/ts/src/flyweight/index.ts +++ b/ts/src/flyweight/index.ts @@ -3,8 +3,13 @@ 汤宝宝开了一家服装租赁店,好多客人来店里租衣服~ */ +// 享元抽象类 - 服装接口 +interface ClothingInterface { + show(): void; +} + // 享元对象 - 服装 -class Clothing { +class Clothing implements ClothingInterface { private style: string; private size: string; private color: string; @@ -24,13 +29,13 @@ class Clothing { // 享元工厂 - 服装店 class ClothingFactory { - private clothingPool: { [key: string]: Clothing }; + private clothingPool: { [key: string]: ClothingInterface }; constructor() { this.clothingPool = {}; } - getClothing(style: string, size: string, color: string): Clothing { + getClothing(style: string, size: string, color: string): ClothingInterface { const key = `${style}-${size}-${color}`; if (!this.clothingPool[key]) { this.clothingPool[key] = new Clothing(style, size, color); @@ -50,14 +55,14 @@ class ClothingFactory { // 客人 class Customer { private name: string; - clothing: Clothing[]; + clothing: ClothingInterface[]; constructor(name: string) { this.name = name; this.clothing = []; } - rentClothing(clothing: Clothing) { + rentClothing(clothing: ClothingInterface) { console.log(`${this.name} 租了一件服装.`); this.clothing.push(clothing); clothing.show();