重构享元模式示例,统一服装接口类型,增强代码一致性和可读性
This commit is contained in:
parent
4589debb92
commit
232b926a97
|
|
@ -7,6 +7,11 @@ import "fmt"
|
||||||
汤宝宝开了一家服装租赁店,好多客人来店里租衣服~
|
汤宝宝开了一家服装租赁店,好多客人来店里租衣服~
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// 享元抽象类
|
||||||
|
type ClothingInterface interface {
|
||||||
|
Show()
|
||||||
|
}
|
||||||
|
|
||||||
// 享元对象 - 服装
|
// 享元对象 - 服装
|
||||||
type Clothing struct {
|
type Clothing struct {
|
||||||
style string
|
style string
|
||||||
|
|
@ -20,16 +25,16 @@ func (c *Clothing) Show() {
|
||||||
|
|
||||||
// 享元工厂 - 服装店
|
// 享元工厂 - 服装店
|
||||||
type ClothingFactory struct {
|
type ClothingFactory struct {
|
||||||
clothingPool map[string]*Clothing
|
clothingPool map[string]ClothingInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClothingFactory() *ClothingFactory {
|
func NewClothingFactory() *ClothingFactory {
|
||||||
return &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
|
key := style + "-" + size + "-" + color
|
||||||
if clothing, exists := f.clothingPool[key]; exists {
|
if clothing, exists := f.clothingPool[key]; exists {
|
||||||
return clothing
|
return clothing
|
||||||
|
|
@ -50,17 +55,17 @@ func (f *ClothingFactory) ShowStock() {
|
||||||
// 客人
|
// 客人
|
||||||
type Customer struct {
|
type Customer struct {
|
||||||
name string
|
name string
|
||||||
clothing []*Clothing
|
clothing []ClothingInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCustomer(name string) *Customer {
|
func NewCustomer(name string) *Customer {
|
||||||
return &Customer{
|
return &Customer{
|
||||||
name: name,
|
name: name,
|
||||||
clothing: []*Clothing{},
|
clothing: []ClothingInterface{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Customer) RentClothing(clothing *Clothing) {
|
func (c *Customer) RentClothing(clothing ClothingInterface) {
|
||||||
fmt.Printf("%s 租了一件服装: ", c.name)
|
fmt.Printf("%s 租了一件服装: ", c.name)
|
||||||
clothing.Show()
|
clothing.Show()
|
||||||
c.clothing = append(c.clothing, clothing)
|
c.clothing = append(c.clothing, clothing)
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,15 @@
|
||||||
汤宝宝开了一家服装租赁店,好多客人来店里租衣服~
|
汤宝宝开了一家服装租赁店,好多客人来店里租衣服~
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
class Clothing:
|
# 享元接口
|
||||||
|
class ClothingInterface(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def show(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Clothing(ClothingInterface):
|
||||||
"""享元对象 - 服装"""
|
"""享元对象 - 服装"""
|
||||||
|
|
||||||
style: str
|
style: str
|
||||||
|
|
@ -23,12 +30,12 @@ class Clothing:
|
||||||
class ClothingFactory:
|
class ClothingFactory:
|
||||||
"""享元工厂 - 服装店"""
|
"""享元工厂 - 服装店"""
|
||||||
|
|
||||||
_clothing_pool: dict[str, Clothing]
|
_clothing_pool: dict[str, ClothingInterface]
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._clothing_pool = {}
|
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}"
|
key = f"{style}-{size}-{color}"
|
||||||
if key not in self._clothing_pool:
|
if key not in self._clothing_pool:
|
||||||
self._clothing_pool[key] = Clothing(style, size, color)
|
self._clothing_pool[key] = Clothing(style, size, color)
|
||||||
|
|
@ -43,13 +50,13 @@ class ClothingFactory:
|
||||||
|
|
||||||
class Customer:
|
class Customer:
|
||||||
name: str
|
name: str
|
||||||
clothing: list[Clothing]
|
clothing: list[ClothingInterface]
|
||||||
|
|
||||||
def __init__(self, name: str):
|
def __init__(self, name: str):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.clothing = []
|
self.clothing = []
|
||||||
|
|
||||||
def rent_clothing(self, clothing: Clothing):
|
def rent_clothing(self, clothing: ClothingInterface):
|
||||||
self.clothing.append(clothing)
|
self.clothing.append(clothing)
|
||||||
print(f"{self.name} 租了一件服装: ", end="")
|
print(f"{self.name} 租了一件服装: ", end="")
|
||||||
clothing.show()
|
clothing.show()
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,13 @@
|
||||||
汤宝宝开了一家服装租赁店,好多客人来店里租衣服~
|
汤宝宝开了一家服装租赁店,好多客人来店里租衣服~
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// 享元抽象类 - 服装接口
|
||||||
|
interface ClothingInterface {
|
||||||
|
show(): void;
|
||||||
|
}
|
||||||
|
|
||||||
// 享元对象 - 服装
|
// 享元对象 - 服装
|
||||||
class Clothing {
|
class Clothing implements ClothingInterface {
|
||||||
private style: string;
|
private style: string;
|
||||||
private size: string;
|
private size: string;
|
||||||
private color: string;
|
private color: string;
|
||||||
|
|
@ -24,13 +29,13 @@ class Clothing {
|
||||||
|
|
||||||
// 享元工厂 - 服装店
|
// 享元工厂 - 服装店
|
||||||
class ClothingFactory {
|
class ClothingFactory {
|
||||||
private clothingPool: { [key: string]: Clothing };
|
private clothingPool: { [key: string]: ClothingInterface };
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.clothingPool = {};
|
this.clothingPool = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
getClothing(style: string, size: string, color: string): Clothing {
|
getClothing(style: string, size: string, color: string): ClothingInterface {
|
||||||
const key = `${style}-${size}-${color}`;
|
const key = `${style}-${size}-${color}`;
|
||||||
if (!this.clothingPool[key]) {
|
if (!this.clothingPool[key]) {
|
||||||
this.clothingPool[key] = new Clothing(style, size, color);
|
this.clothingPool[key] = new Clothing(style, size, color);
|
||||||
|
|
@ -50,14 +55,14 @@ class ClothingFactory {
|
||||||
// 客人
|
// 客人
|
||||||
class Customer {
|
class Customer {
|
||||||
private name: string;
|
private name: string;
|
||||||
clothing: Clothing[];
|
clothing: ClothingInterface[];
|
||||||
|
|
||||||
constructor(name: string) {
|
constructor(name: string) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.clothing = [];
|
this.clothing = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
rentClothing(clothing: Clothing) {
|
rentClothing(clothing: ClothingInterface) {
|
||||||
console.log(`${this.name} 租了一件服装.`);
|
console.log(`${this.name} 租了一件服装.`);
|
||||||
this.clothing.push(clothing);
|
this.clothing.push(clothing);
|
||||||
clothing.show();
|
clothing.show();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue