Python: Strategy Pattern

 GeovinDuStrategy.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 策略模式  Strategy Pattern Types of trading strategies:
class RiskyTradingStrategy(object):
    def MakeTrades(self):
        print("进行高风险交易!")
 
 
class ModerateTradingStrategy(object):
    def MakeTrades(self):
        print("进行适度的交易.")
 
 
class ConservativeTradingStrategy(object):
    def MakeTrades(self):
        print("进行安全交易.")
 
 
# Not Supported below Python 3.4!
from enum import Enum
 
 
class TradeConditions(Enum):
    BearMarket = 0,
    BullMarket = 1,
    RecoveringMarket = 2
 
 
# The trading firm which changes strategies based on market conditions:
class TradingFirm(object):
    def __init__(self):
        self.riskyStrategy = RiskyTradingStrategy()
        self.moderateStrategy = ModerateTradingStrategy()
        self.safeStrategy = ConservativeTradingStrategy()
 
        self.currentStrategy = self.moderateStrategy
 
    def MarketUpdte(self, tradeConditions):
        # Select the best strategy for the market conditions:
        if tradeConditions == TradeConditions.BearMarket:
            self.currentStrategy = self.safeStrategy
        elif tradeConditions == TradeConditions.BullMarket:
            self.currentStrategy = self.riskyStrategy
        elif tradeConditions == TradeConditions.RecoveringMarket:
            self.currentStrategy = self.moderateStrategy
 
        # Make trades with that strategy:
        self.currentStrategy.MakeTrades()
 
 
class Item:
    """Constructor function with price and discount"""
 
    def __init__(self, price, discount_strategy=None):
 
        """take price and discount strategy"""
 
        self.price = price
        self.discount_strategy = discount_strategy
 
    """A separate function for price after discount"""
 
    def price_after_discount(self):
 
        if self.discount_strategy:
            discount = self.discount_strategy(self)
        else:
            discount = 0
 
        return self.price - discount
 
    def __repr__(self):
 
        statement = "价格: {}, 折扣后的价格: {}"
        return statement.format(self.price, self.price_after_discount())
 
 
"""function dedicated to On Sale Discount"""
 
 
def on_sale_discount(order):
    return order.price * 0.25 + 20
 
 
"""function dedicated to 20 % discount"""
 
 
def twenty_percent_discount(order):
    return order.price * 0.20

  

main.py 调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 策略模式  Strategy Pattern
higeovindu=GeovinDuStrategy.TradingFirm()
higeovindu.MarketUpdte(higeovindu.moderateStrategy.MakeTrades())
higeovindu.safeStrategy.MakeTrades()
higeovindu.currentStrategy.MakeTrades()
higeovindu.riskyStrategy.MakeTrades()
 
print(GeovinDuStrategy.Item(20000))
 
"""with discount strategy as 20 % discount"""
print(GeovinDuStrategy.Item(20000, discount_strategy=GeovinDuStrategy.twenty_percent_discount))
 
"""with discount strategy as On Sale Discount"""
print(GeovinDuStrategy.Item(20000, discount_strategy=GeovinDuStrategy.on_sale_discount))

  

输出:

1
2
3
4
5
6
7
8
进行适度的交易.
进行适度的交易.
进行安全交易.
进行适度的交易.
进行高风险交易!
价格: 20000, 折扣后的价格: 20000
价格: 20000, 折扣后的价格: 16000.0
价格: 20000, 折扣后的价格: 14980.0

  

 

 

 

 

 

 

 

posted @   ®Geovin Du Dream Park™  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示