【Fluent Python】第六章 使用一等函数实现设计模式 Design Patterns with First-Class Functions

rethink the Strategy, Command, Template Method, and Visitor patterns.
General idea: you can replace instances of some participant class in these patterns with simple functions, reducing a lot of boilerplate code.

Case Study: Refactoring Strategy

Strategy is a good example of a design pattern that can be simpler in Python if you leverage functions as firs-class objects.

we will describe and implement Strategy using the classic structure described in Design Patterns.

Classic Strategy

acc to Design Patterns, Strategy pattern is –

  • define a family of algorithms, encapsulate each one, and make them interchangeable.
  • Strategy lets the algorithm vary independently from clients that use it.

e.g. compute discounts to orders according to the attributes of the customer or inspection of the ordered items.
—.
Consider an online store with these discount rules:

  • customers with 1,000 or more fidelity points get a global 5% discount per order
  • a 10% discount is applied to each line item with 20 or more units in the same order
  • orders with at least 10 distinct items get a 7% global discount

在这里插入图片描述
Context:

  • provides a service by delegating some computation to interchangeable components that implement alternative algorithms
  • (in this example, the context is an Order, which is configured to apply a promotional discount according to one of several algorithms)

Strategy:

  • the interface common to the components that implement the different algoriths.
  • (in this example, this role is played by an abstract class called Promotion)

Concrete Strategy

  • one of the concrete subclasses of Strategy
    (in this example, FidelityPromo, BulkitemPromo, LargeOrderPromo)

implementation

the concrete strategy is chosen by the client of the context class.
(before instantiating an order, the system would somehow select a promotional discount strategy and pass it to the Order constructor. selection of strategy is out of scope)
在这里插入图片描述
在这里插入图片描述

Demonstration

在这里插入图片描述

Function-Oriented Strategy
Choosing the Best Strategy: Simply Approach
Finding Strategies in a Module

Modules in Python are also first-class objects, and the standard library provides several functions to handle them.

The built-in globals is described as follows in the doc:

return a dictionary representing the current global symbol table.
This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called)

Command

Command is another design pattern that can be simplified by the use of functions passed as arguments.

The goal of Command is to decouple an object that invokes an operation from the provider object that implements.

The idea is to put a Command object between the two, implementing an interface with a single method, execute, which calls some method in the Receiver to perform the desired operation.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值