Groovy 设计模式 -- Strategy 模式

时间:2023-03-09 00:13:59
Groovy 设计模式 -- Strategy 模式

策略模式

https://en.wikipedia.org/wiki/Strategy_pattern

In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. The strategy pattern

  • defines a family of algorithms,
  • encapsulates each algorithm, and
  • makes the algorithms interchangeable within that family.

Strategy lets the algorithm vary independently from clients that use it.[1] Strategy is one of the patterns included in the influential book Design Patterns by Gamma et al. that popularized the concept of using design patterns to describe how to design flexible and reusable object-oriented software.

The Strategy [2] design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.

What problems can the Strategy design pattern solve? [3]

  • A class should be configured with an algorithm instead of implementing an algorithm directly.
  • An algorithm should be selected and exchanged at run-time.

What is an algorithm? An algorithm is usually defined as a procedure that takes some value as input, performs a finite number of steps, and produces some value as output.
From a more general point of view, an algorithm is a piece of code that does something appropriate.

EXPLAIN

http://www.cnblogs.com/java-my-life/archive/2012/05/10/2491891.html

在阎宏博士的《JAVA与模式》一书中开头是这样描述策略(Strategy)模式的:

  策略模式属于对象的行为模式。其用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。


策略模式的结构

  策略模式是对算法的包装,是把使用算法的责任和算法本身分割开来,委派给不同的对象管理。策略模式通常把一个系列的算法包装到一系列的策略类里面,作为一个抽象策略类的子类。用一句话来说,就是:“准备一组算法,并将每一个算法封装起来,使得它们可以互换”。下面就以一个示意性的实现讲解策略模式实例的结构。

Groovy 设计模式 -- Strategy 模式

  这个模式涉及到三个角色:

  ●  环境(Context)角色:持有一个Strategy的引用。

  ●  抽象策略(Strategy)角色:这是一个抽象角色,通常由一个接口或抽象类实现。此角色给出所有的具体策略类所需的接口。

  ●  具体策略(ConcreteStrategy)角色:包装了相关的算法或行为。

DEMO

https://en.wikipedia.org/wiki/Strategy_pattern

import java.util.ArrayList;
import java.util.List; public class StrategyPatternWiki { public static void main(final String[] arguments) {
Customer firstCustomer = new Customer(new NormalStrategy()); // Normal billing
firstCustomer.add(1.0, 1); // Start Happy Hour
firstCustomer.setStrategy(new HappyHourStrategy());
firstCustomer.add(1.0, 2); // New Customer
Customer secondCustomer = new Customer(new HappyHourStrategy());
secondCustomer.add(0.8, 1);
// The Customer pays
firstCustomer.printBill(); // End Happy Hour
secondCustomer.setStrategy(new NormalStrategy());
secondCustomer.add(1.3, 2);
secondCustomer.add(2.5, 1);
secondCustomer.printBill();
}
} class Customer { private List<Double> drinks;
private BillingStrategy strategy; public Customer(final BillingStrategy strategy) {
this.drinks = new ArrayList<Double>();
this.strategy = strategy;
} public void add(final double price, final int quantity) {
drinks.add(strategy.getActPrice(price*quantity));
} // Payment of bill
public void printBill() {
double sum = 0;
for (Double i : drinks) {
sum += i;
}
System.out.println("Total due: " + sum);
drinks.clear();
} // Set Strategy
public void setStrategy(final BillingStrategy strategy) {
this.strategy = strategy;
} } interface BillingStrategy {
double getActPrice(final double rawPrice);
} // Normal billing strategy (unchanged price)
class NormalStrategy implements BillingStrategy { @Override
public double getActPrice(final double rawPrice) {
return rawPrice;
} } // Strategy for Happy hour (50% discount)
class HappyHourStrategy implements BillingStrategy { @Override
public double getActPrice(final double rawPrice) {
return rawPrice*0.5;
} }