实现代理模式示例,添加火车站和代售点类,增强代码可读性
This commit is contained in:
parent
232b926a97
commit
9bb30b8bfb
|
|
@ -0,0 +1,68 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
/*
|
||||||
|
实例:
|
||||||
|
汤宝宝买火车票,可以去火车站买,也可以去代售点买,但是汤宝宝选择全都要。然而,火车站总共只剩两张票,这可苦了小明了。
|
||||||
|
*/
|
||||||
|
|
||||||
|
type Seller interface {
|
||||||
|
Sell(name string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type TrainStation struct {
|
||||||
|
stock int
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTrainStation(stock int) *TrainStation {
|
||||||
|
fmt.Printf("火车站库存%d张票\n", stock)
|
||||||
|
return &TrainStation{stock: stock}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts *TrainStation) Sell(name string) error {
|
||||||
|
fmt.Printf("%s去火车站买票。\n", name)
|
||||||
|
if ts.stock > 0 {
|
||||||
|
ts.stock--
|
||||||
|
fmt.Printf("%s在火车站买到一张火车票。\n", name)
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
fmt.Printf("火车站没有票了, %s没买到票。\n", name)
|
||||||
|
return fmt.Errorf("no ticket")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type TicketAgency struct {
|
||||||
|
name string
|
||||||
|
station *TrainStation
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTicketAgency(name string, station *TrainStation) *TicketAgency {
|
||||||
|
return &TicketAgency{name: name, station: station}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tp *TicketAgency) Sell(name string) error {
|
||||||
|
fmt.Printf("%s去%s买票。\n", name, tp.name)
|
||||||
|
err := tp.station.Sell(tp.name)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%s在%s没买到票。\n", name, tp.name)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Printf("%s在%s买到一张火车票。\n", name, tp.name)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("============= 代理模式 =============")
|
||||||
|
station := newTrainStation(2)
|
||||||
|
agency := newTicketAgency("代售点A", station)
|
||||||
|
station.Sell("汤宝宝")
|
||||||
|
agency.Sell("汤宝宝")
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
agency.Sell("小明")
|
||||||
|
station.Sell("小明")
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println("=========== 代理模式结束 ===========")
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,59 @@
|
||||||
|
"""
|
||||||
|
实例:
|
||||||
|
汤宝宝买火车票,可以去火车站买,也可以去代售点买,但是汤宝宝选择全都要。然而,火车站总共只剩两张票,这可苦了小明了。
|
||||||
|
"""
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
|
||||||
|
class Seller(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def sell(self, name: str) -> bool:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class TrainStation(Seller):
|
||||||
|
stock: int
|
||||||
|
|
||||||
|
def __init__(self, stock: int) -> None:
|
||||||
|
print(f"火车站库存{stock}张票")
|
||||||
|
self.stock = stock
|
||||||
|
|
||||||
|
def sell(self, name: str) -> bool:
|
||||||
|
print(f"{name}去火车站买票。")
|
||||||
|
if self.stock > 0:
|
||||||
|
self.stock -= 1
|
||||||
|
print(f"{name}在火车站买到一张火车票。")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
print(f"火车站没票了,{name}没买到票。")
|
||||||
|
return False
|
||||||
|
|
||||||
|
class TicketAgency(Seller):
|
||||||
|
name: str
|
||||||
|
station: TrainStation
|
||||||
|
|
||||||
|
def __init__(self, name: str, station: TrainStation) -> None:
|
||||||
|
self.name = name
|
||||||
|
self.station = station
|
||||||
|
|
||||||
|
def sell(self, name: str) -> bool:
|
||||||
|
print(f"{name}去{self.name}买票。")
|
||||||
|
result = self.station.sell(self.name)
|
||||||
|
if result:
|
||||||
|
print(f"{name}在{self.name}买到一张火车票。")
|
||||||
|
else:
|
||||||
|
print(f"{name}在{self.name}没买到票。")
|
||||||
|
return result
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("============= 代理模式 =============")
|
||||||
|
station = TrainStation(stock=2)
|
||||||
|
agency = TicketAgency(name="代售点A", station=station)
|
||||||
|
station.sell("汤宝宝")
|
||||||
|
agency.sell("汤宝宝")
|
||||||
|
print("")
|
||||||
|
agency.sell("小明")
|
||||||
|
station.sell("小明")
|
||||||
|
|
||||||
|
print("\n=========== 代理模式结束 ===========")
|
||||||
|
|
@ -0,0 +1,64 @@
|
||||||
|
/*
|
||||||
|
实例:
|
||||||
|
汤宝宝买火车票,可以去火车站买,也可以去代售点买,但是汤宝宝选择全都要。然而,火车站总共只剩两张票,这可苦了小明了。
|
||||||
|
*/
|
||||||
|
|
||||||
|
interface Seller {
|
||||||
|
sell(name: string): boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
class TrainStation implements Seller {
|
||||||
|
private stock: number;
|
||||||
|
|
||||||
|
constructor(stock: number) {
|
||||||
|
console.log(`火车站库存${stock}张票`);
|
||||||
|
this.stock = stock;
|
||||||
|
}
|
||||||
|
|
||||||
|
sell(name: string): boolean {
|
||||||
|
console.log(`${name}去火车站买票`);
|
||||||
|
if (this.stock > 0) {
|
||||||
|
this.stock--;
|
||||||
|
console.log(`${name}在火车站买到一张火车票。`);
|
||||||
|
return true;
|
||||||
|
}else {
|
||||||
|
console.log(`火车站没有票了, ${name}没买到票。`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TicketAgency implements Seller {
|
||||||
|
name: string;
|
||||||
|
private station: TrainStation;
|
||||||
|
|
||||||
|
constructor(name: string, station: TrainStation) {
|
||||||
|
this.name = name;
|
||||||
|
this.station = station;
|
||||||
|
}
|
||||||
|
|
||||||
|
sell(name: string): boolean {
|
||||||
|
console.log(`${name}去${this.name}买票`);
|
||||||
|
const result = this.station.sell(this.name);
|
||||||
|
if (result) {
|
||||||
|
console.log(`${name}在${this.name}买到一张火车票。`);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
console.log(`${name}在${this.name}没买到票。`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(function(){
|
||||||
|
console.log("============= 代理模式 =============");
|
||||||
|
const station = new TrainStation(2);
|
||||||
|
const agency = new TicketAgency("代售点A", station);
|
||||||
|
station.sell("汤宝宝");
|
||||||
|
agency.sell("汤宝宝");
|
||||||
|
console.log()
|
||||||
|
agency.sell("小明");
|
||||||
|
station.sell("小明");
|
||||||
|
console.log("\n=========== 代理模式结束 ===========");
|
||||||
|
|
||||||
|
})()
|
||||||
Loading…
Reference in New Issue