Opengist

Explore

  • All gists
  • Topics
  • Users
Give feedback on the new UI Powered by Opengist ⋅ 1156ms

user:thomas gists created by user

title:mygist gists with given title

description:sync gists with given description

filename:myfile.txt gists having files with given name

extension:yml gists having files with given extension

language:go gists having files with given language

topic:homelab gists with given topic

all:systemctl search all fields

Login
s

shopping-cart

Recently created Least recently created Recently updated Least recently updated

timmy / 策略模式在購物車計價中的應用

Last active 6 hours ago design-patterns discount-calculation object-oriented-programming pricing-strategy python shopping-cart strategy-pattern

這段程式碼實作「策略模式(Strategy Pattern)」,用於計算購物車的總金額,並允許 根據不同的定價策略(如正常價格或折扣價格) 來計算最終價格。

1 0 0 1
1 from abc import ABC, abstractmethod
2
3 # 策略介面
4 class PricingStrategy(ABC):
5 @abstractmethod
6 def calculate_price(self, price):
7 pass
8
9 # 具體策略類
10 class NormalStrategy(PricingStrategy):