Strategy pattern - http://en.wikipedia.org/wiki/Strategy_pattern
2014-01-17 11:35
661 查看
From Wikipedia, the free encyclopedia
Jump to:
navigation,
search
In
computer programming, the strategy pattern (also known as thepolicy pattern) is a
software design pattern, whereby an
algorithm's behaviour can be selected at runtime. Formally speaking, the strategy pattern defines a family ofalgorithms, encapsulates each one, and makes
them interchangeable. 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 patterns
in software design.
For instance, a class that performs validation on incoming data may use a strategy pattern to select a validation algorithm based on the type of data, the source of the data, user choice, or other discriminating factors. These factors are not known for each
case until run-time, and may require radically different validation to be performed. The validation strategies, encapsulated separately from the validating object, may be used by other validating objects in different areas of the system (or even different
systems) without code duplication.
The essential requirement in the
programming language is the ability to store a reference to some code in a data structure and retrieve it. This can be achieved by mechanisms such as the nativefunction
pointer, the
first-class function, classes or class instances in
object-oriented programming languages, or accessing the language implementation's internal storage of code viareflection.
1Structure
2Example
3Strategy and open/closed principle
4See also
5References
6External links
Strategy Pattern in
UML
Strategy pattern in
LePUS3 (legend)
Java.
Accelerate and
brake behaviors must be declared in each new
car model.
According to the strategy pattern, the behaviors of a class should not be inherited. Instead they should be encapsulated using interfaces. As an example, consider a car class. Two possible functionalities for car arebrake and
accelerate.
Since accelerate and brake behaviors change frequently between models, a common approach is to implement these behaviors in subclasses. This approach has significant drawbacks: accelerate and brake behaviors must be declared in each new Car model. The work
of managing these behaviors increases greatly as the number of models increases, and requires code to be duplicated across models. Additionally, it is not easy to determine the exact nature of the behavior for each model without investigating the code in each.
The strategy pattern uses aggregation instead of inheritance. In the strategy pattern, behaviors are defined as separate interfaces and specific classes that implement these interfaces. Specific classes encapsulate these interfaces. This allows better decoupling
between the behavior and the class that uses the behavior. The behavior can be changed without breaking the classes that use it, and the classes can switch between behaviors by changing the specific implementation used without requiring any significant code
changes. Behaviors can also be changed at run-time as well as at design-time. For instance, a car object’s brake behavior can be changed from
This gives greater flexibility in design and is in harmony with the
Open/closed principle (OCP) that states that classes should be open for extension but closed for modification.
List of object-oriented programming terms
Mixin
Policy-based design
Jump up^
Eric Freeman, Elisabeth Freeman, Kathy Sierra and Bert Bates,Head First Design Patterns, First Edition, Chapter 1, Page 24, O'Reilly Media, Inc, 2004.ISBN
978-0-596-00712-6
The Strategy Pattern from the Net Objectives Repository
Strategy Pattern for Java article
Strategy Pattern for CSharp article
Strategy pattern in UML and in LePUS3 (a formal modelling notation)
Refactoring: Replace Type Code with State/Strategy
From Wikipedia, the free encyclopedia
Jump to:
navigation,
search
In
computer programming, the strategy pattern (also known as thepolicy pattern) is a
software design pattern, whereby an
algorithm's behaviour can be selected at runtime. Formally speaking, the strategy pattern defines a family ofalgorithms, encapsulates each one, and makes
them interchangeable. 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 patterns
in software design.
For instance, a class that performs validation on incoming data may use a strategy pattern to select a validation algorithm based on the type of data, the source of the data, user choice, or other discriminating factors. These factors are not known for each
case until run-time, and may require radically different validation to be performed. The validation strategies, encapsulated separately from the validating object, may be used by other validating objects in different areas of the system (or even different
systems) without code duplication.
The essential requirement in the
programming language is the ability to store a reference to some code in a data structure and retrieve it. This can be achieved by mechanisms such as the nativefunction
pointer, the
first-class function, classes or class instances in
object-oriented programming languages, or accessing the language implementation's internal storage of code viareflection.
Contents
[hide]1Structure
2Example
3Strategy and open/closed principle
4See also
5References
6External links
Structure[edit]
Strategy Pattern in
UML
Strategy pattern in
LePUS3 (legend)
Example[edit]
The following example is inJava.
/** The classes that implement a concrete strategy should implement this. * The Context class uses this to call the concrete strategy. */ interface Strategy { 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; // Do an addition with a and b } }; class Subtract implements Strategy { public int execute(int a, int b) { System.out.println("Called Subtract's execute()"); return a - b; // Do a subtraction with a and b } }; class Multiply implements Strategy { public int execute(int a, int b) { System.out.println("Called Multiply's execute()"); return a * b; // Do a multiplication with a and 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); } }; /** Tests the pattern */ class StrategyExample { public static void main(String[] args) { Context context; // Three contexts following different strategies context = new Context(new Add()); int resultA = context.executeStrategy(3,4); context = new Context(new Subtract()); int resultB = context.executeStrategy(3,4); context = new Context(new Multiply()); int resultC = context.executeStrategy(3,4); System.out.println("Result A : " + resultA ); System.out.println("Result B : " + resultB ); System.out.println("Result C : " + resultC ); } };
Strategy and open/closed principle[edit]
Accelerate and
brake behaviors must be declared in each new
car model.
According to the strategy pattern, the behaviors of a class should not be inherited. Instead they should be encapsulated using interfaces. As an example, consider a car class. Two possible functionalities for car arebrake and
accelerate.
Since accelerate and brake behaviors change frequently between models, a common approach is to implement these behaviors in subclasses. This approach has significant drawbacks: accelerate and brake behaviors must be declared in each new Car model. The work
of managing these behaviors increases greatly as the number of models increases, and requires code to be duplicated across models. Additionally, it is not easy to determine the exact nature of the behavior for each model without investigating the code in each.
The strategy pattern uses aggregation instead of inheritance. In the strategy pattern, behaviors are defined as separate interfaces and specific classes that implement these interfaces. Specific classes encapsulate these interfaces. This allows better decoupling
between the behavior and the class that uses the behavior. The behavior can be changed without breaking the classes that use it, and the classes can switch between behaviors by changing the specific implementation used without requiring any significant code
changes. Behaviors can also be changed at run-time as well as at design-time. For instance, a car object’s brake behavior can be changed from
BrakeWithABS()to
Brake()by changing the
brakeBehaviormember to:
brakeBehavior = new Brake();
This gives greater flexibility in design and is in harmony with the
Open/closed principle (OCP) that states that classes should be open for extension but closed for modification.
See also[edit]
Higher-order functionList of object-oriented programming terms
Mixin
Policy-based design
References[edit]
Wikimedia Commons has media related to Strategy (design pattern). |
Eric Freeman, Elisabeth Freeman, Kathy Sierra and Bert Bates,Head First Design Patterns, First Edition, Chapter 1, Page 24, O'Reilly Media, Inc, 2004.ISBN
978-0-596-00712-6
External links[edit]
Strategy Pattern in UML (Spanish, but english model)The Wikibook Computer Science Design Patterns has a page on the topic of: Strategy implementations in various languages |
Strategy Pattern for Java article
Strategy Pattern for CSharp article
Strategy pattern in UML and in LePUS3 (a formal modelling notation)
Refactoring: Replace Type Code with State/Strategy
相关文章推荐
- Vmware学习(二),Vmware三种网络
- Linux 网络管理员福利:Namp命令的29个实战例子
- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />详解
- 2013年第4季度橱柜品牌网络知名度排名
- Android Q4. [机卡兼容]终端显示可打紧急呼叫状态下,有时候打紧急号码打不通,最后显示“网络连接失败”
- asp xmlhttp 读取文件
- 腾讯或将成首家网络银行 Q币十年发行超千亿
- android监控网络状态变化防止多次广播的一种解决办法
- tcp OSI结构
- HTTP/1.1与HTTP/1.0的区别
- PHP CURL访问HTTPS使用详解
- https://nodejstools.codeplex.com
- WEBHttpRequest
- EXTJS网络系统收集
- lighttpd 使用反向代理实现简单负载均衡(多域名多内网主机实现)
- 网络编程
- apache(httpd)网站VirtualHost配置
- HTTP协议详解
- unix/linux常用网络命令系列:lsof
- unix/linux常用网络命令系列:前言