策略模式

[align=center]Strategy pattern[/align]
[b]Definition:[/b]
The strategy pattern defines a family of algorithms,encapsulate each one,and makes them interchangeable.Strategy lets the algorithm vary independently from clients that use it.Whereby an algorithm's behaviour can be selected at runtime.

[b]Intent[/b]
Encapsulate what varys,and conform to the OCP principle.



/**
* The classes that implements a concrete strategy should implement this.
* The context class uses this to call the concrete strategy.*/
public interface Stragegy{
int execute(int a,int b);
}

/** Implements the algorithm using the strategy interface*/
class Add implements Strategy {
public int execute(int a,int b){
System.out.println("Called Add's execute()");
return a+b;
}
}

class Subtract implements Strategy{

public int execute(int a,int b){
system.out.println("Called from Subtract's execute()");
return a-b;
}
}
/**Configured with a ConcreteStrategy object and maintains a reference to a
strategy object
*/
class Context{
private Strategy strategy;

public Context(Strategy strategy){
this.strategy = strategy;
}

public int executeStrategy(int a,int b){
return this.strategy.execute(a,b);
}
}

/** Test the pattern*/
class StrategyExample{
public static void main(String[] args){
Context context = new Context(new Add());
int resultA = context.executeStrategy(3,4);
context = new Context(new Subtract());
int resultS = context.executeStrategy(3,4);

}
}


[b]Usage[/b]
validation strategy,sort strategy,and so on.

[b]Applicability[/b]
Use Strategy pattern when
1 Many related classes differ only in their behavior.Strategies provide a way to configure a class with one of many behaviors.

2 You need different variants of an algorithm.For example.,you might define algorithms reflecting different space/time trade-offs.Strategies can be used when these variants are implements as a class hierachy of algorithms.

3 an algorithm uses data that clients shouldn't know about.Use the Strategy pattern to avoid exposing complex,algorithm-specific data structures.

4 a class defines many behaviors,and these appears as multiple conditional statements in its operations.Instead of many conditionals,move related conditional branches into their own Strategy class.

[b]Consequence[/b]
1 It provides an alternative for subclassing.
2 It eliminates conditional statement.

[b]Drawbacks[/b]
1 Clients must be aware of different Strategies.The pattern has a potential drawback in that the client must understand how Strategies differ before it can select the appropriate one.Clients must be exposed to implementation issues.Therefore you should use the Strategy pattern only when the variation in behavior is relevant to clients.

2 Increased number of objects.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值