问题描述
它是如何工作的,它是用什么,什么时候应该使用它?
推荐答案
让我们以简单的方式解释策略模式:
您有一个类Car()的方法run(),因此您以伪语言使用这种方式:
mycar = new Car() mycar.run()
现在,您可能需要在程序执行时即时更改run()行为.例如,您可能需要模拟电动机故障或在视频游戏中使用"升压"按钮.
有几种方法可以进行此模拟:使用条件语句,而标志变量是一种方法.策略模式是另一种:它将run()方法的行为委派给另一类:
Class Car() { this.motor = new Motor(this) // passing "this" is important for the motor so it knows what it is running method run() { this.motor.run() } method changeMotor(motor) { this.motor = motor } }
如果您想改变汽车的行为,则可以更改电动机. (在程序中比现实生活更容易,对吗?;-))
如果您有很多复杂的状态,这将非常有用:您可以更轻松地更改和维护它们.
其他推荐答案
问题
该策略模式用于解决可能(或预见它们可能)通过不同的策略来实施或解决的问题,并且在此类情况下具有明确定义的界面.每种策略本身都是完全有效的,在某些情况下可以在运行时切换应用程序的某些策略.
.代码示例
namespace StrategyPatterns { // Interface definition for a Sort algorithm public interface ISort { void Sort(List<string> list) } // QuickSort implementation public class CQuickSorter : ISort { void Sort(List<string> list) { // Here will be the actual implementation } } // BubbleSort implementation public class CBubbleSort : ISort { void Sort(List<string> list) { // The actual implementation of the sort } } // MergeSort implementation public class CMergeSort : ISort { void Sort(List<string> list) { // Again the real implementation comes here } } public class Context { private ISort sorter; public Context(ISort sorter) { // We pass to the context the strategy to use this.sorter = sorter; } public ISort Sorter { get{return sorter;) } } public class MainClass { static void Main() { List<string> myList = new List<string>(); myList.Add("Hello world"); myList.Add("Another item"); myList.Add("Item item"); Context cn = new Context(new CQuickSorter()); // Sort using the QuickSort strategy cn.Sorter.Sort(myList); myList.Add("This one goes for the mergesort"); cn = new Context(new CMergeSort()); // Sort using the merge sort strategy cn.Sorter.Sort(myList); } } }
其他推荐答案
- 什么是策略?策略是旨在实现特定目标的行动计划;
- "定义一个算法家族,封装每种算法,并使它们可以互换.策略使该算法与使用它的客户的独立性不同." (四个帮派);
- 指定一组类,每个类都代表潜在行为.这些类之间的切换会改变应用程序行为. (策略);
- 可以在运行时(使用多态性)或设计时间选择此行为;
- 在界面中捕获抽象,埋在派生类中的实现细节;
- 该策略的一种替代方法是使用条件逻辑来改变应用程序行为. (坏);
-
使用此模式使添加或删除特定的行为变得更容易,而无需重新编码和重新测试,应用程序的所有或部分;
-
良好用途:
- 当我们有一组类似的算法及其在应用程序的不同部分之间切换它们时.使用策略模式可以避免IF和减轻维护;
- 当我们想为不一定对每个子类有意义的超级类添加新方法时.我们使用新方法的实例变量,而不是以传统方式使用传统方式,而是新功能接口的子类.这被称为组成:与其通过继承遗传能力,而是由具有正确能力的对象组成;
问题描述
How does it work, what is it used for and when should one use it?
推荐答案
Let's explain the strategy pattern the easy way:
You have a class Car() with a method run(), so you use it this way in a pseudo language:
mycar = new Car() mycar.run()
Now, you may want to change the run() behavior on the fly, while the program is executing. For example, you might want to simulate a motor failure or the use of a "boost" button in a video game.
There are several ways to do this simulation: using conditional statements and a flag variable is one way. The strategy pattern is another: it delegates the behavior of the run() method to another class:
Class Car() { this.motor = new Motor(this) // passing "this" is important for the motor so it knows what it is running method run() { this.motor.run() } method changeMotor(motor) { this.motor = motor } }
If you want to change the car's behavior, you can just change the motor. (Easier in a program than in real life, right? ;-) )
It's very useful if you have a lot of complex states: you can change and maintain them much more easily.
其他推荐答案
Problem
The strategy pattern is used to solve problems that might (or is foreseen they might) be implemented or solved by different strategies and that possess a clearly defined interface for such cases. Each strategy is perfectly valid on its own with some of the strategies being preferable in certain situations that allow the application to switch between them during runtime.
Code Example
namespace StrategyPatterns { // Interface definition for a Sort algorithm public interface ISort { void Sort(List<string> list) } // QuickSort implementation public class CQuickSorter : ISort { void Sort(List<string> list) { // Here will be the actual implementation } } // BubbleSort implementation public class CBubbleSort : ISort { void Sort(List<string> list) { // The actual implementation of the sort } } // MergeSort implementation public class CMergeSort : ISort { void Sort(List<string> list) { // Again the real implementation comes here } } public class Context { private ISort sorter; public Context(ISort sorter) { // We pass to the context the strategy to use this.sorter = sorter; } public ISort Sorter { get{return sorter;) } } public class MainClass { static void Main() { List<string> myList = new List<string>(); myList.Add("Hello world"); myList.Add("Another item"); myList.Add("Item item"); Context cn = new Context(new CQuickSorter()); // Sort using the QuickSort strategy cn.Sorter.Sort(myList); myList.Add("This one goes for the mergesort"); cn = new Context(new CMergeSort()); // Sort using the merge sort strategy cn.Sorter.Sort(myList); } } }
其他推荐答案
- What is a Strategy? A strategy is a plan of action designed to achieve a specific goal;
- “Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.” (Gang of Four);
- Specifies a set of classes, each representing a potential behaviour. Switching between those classes changes the application behaviour. (the Strategy);
- This behaviour can be selected at runtime (using polymorphism) or design time;
- Capture the abstraction in an interface, bury implementation details in derived classes;
- An alternative to the Strategy is to change the application behaviour by using conditional logic. (BAD);
Using this pattern makes it easier to add or remove specific behaviour, without having to recode and retest, all or parts of the application;
Good uses:
- When we have a set of similar algorithms and its need to switch between them in different parts of the application. With Strategy Pattern is possible to avoid ifs and ease maintenance;
- When we want to add new methods to superclass that don’t necessarily make sense to every subclass. Instead of using an interface in a traditional way, adding the new method, we use an instance variable that is a subclass of the new Functionality interface. This is known as Composition : Instead of inheriting an ability through inheritance the class is composed with Objects with the right ability;