Knowing the OO basics does not make you a good OO designer.
- Good OO designs are reusable,extensible and maintainable.
- Patterns show you how to build systems with good OO design qualities.
- Patterns are proven object oriented experience.
- Patterns don’t give you code,they give you general solutions to design problems.You apply them to your specific application.
- Patterns aren’t invented,they are discovered.
- Most patterns and principles address issues of change in software.
- Most patterns allow some part of a system to vary independently of all other parts.
- We often try to take what varies in a system and encapsulate it.
- Patterns provide a shared language that can maximize the value of your communication with other developers.
策略模式:
定义
The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。

Context(应用场景):
- 需要使用ConcreteStrategy提供的算法。
- 内部维护一个Strategy的实例。
- 负责动态设置运行时Strategy具体的实现算法。
- 负责跟Strategy之间的交互和数据传递。
Strategy(抽象策略类):
- 定义了一个公共接口,各种不同的算法以不同的方式实现这个接口,Context使用这个接口调用不同的算法,一般使用接口或抽象类实现。
ConcreteStrategy(具体策略类):
- 实现了Strategy定义的接口,提供具体的算法实现。
时序图如下

举一个.NET框架里使用Strategy模式的例子,ArrayList类肯定都会用过吧,那么它的Sort方法想必大家也一定不陌生了。Sort方法的定义如下:
public virtual void Sort (IComparer comparer)
可以看到Sort方法接收一个IComparer类型的参数,那么这个IComparer接口是做什么用的呢?下面我们看一段程序,下面的代码示例演示如何使用默认比较器和一个反转排序顺序的自定义比较器,对 ArrayList 中的值进行排序。
using System;
using System.Collections;
public class SamplesArrayList {
public class myReverserClass : IComparer {
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare( Object x, Object y ) {
return( (new CaseInsensitiveComparer()).Compare( y, x ) );
}
}
public static void Main() {
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumps" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );
// Displays the values of the ArrayList.
Console.WriteLine( "The ArrayList initially contains the following values:" );
PrintIndexAndValues( myAL );
// Sorts the values of the ArrayList using the default comparer.
myAL.Sort();
Console.WriteLine( "After sorting with the default comparer:" );
PrintIndexAndValues( myAL );
// Sorts the values of the ArrayList using the reverse case-insensitive comparer.
IComparer myComparer = new myReverserClass();
myAL.Sort( myComparer );
Console.WriteLine( "After sorting with the reverse case-insensitive comparer:" );
PrintIndexAndValues( myAL );
}
public static void PrintIndexAndValues( IEnumerable myList ) {
int i = 0;
foreach ( Object obj in myList )
Console.WriteLine( "\t[{0}]:\t{1}", i++, obj );
Console.WriteLine();
}
}
/*
This code produces the following output.
The ArrayList initially contains the following values:
[0]: The
[1]: quick
[2]: brown
[3]: fox
[4]: jumps
[5]: over
[6]: the
[7]: lazy
[8]: dog
After sorting with the default comparer:
[0]: brown
[1]: dog
[2]: fox
[3]: jumps
[4]: lazy
[5]: over
[6]: quick
[7]: the
[8]: The
After sorting with the reverse case-insensitive comparer:
[0]: the
[1]: The
[2]: quick
[3]: over
[4]: lazy
[5]: jumps
[6]: fox
[7]: dog
[8]: brown
*/
练习:
刘备要到江东娶老婆了,走之前诸葛亮给赵云(伴郎)三个锦囊妙计,说是按天机拆开能解决棘手问题,嘿,还别说,真解决了大问题,搞到最后是周瑜陪了夫人又折兵,那咱们先看看这个场景是什么样子的。
先说说这个场景中的要素:三个妙计,一个锦囊,一个赵云,妙计是亮哥给的,妙计放在锦囊里,俗称就是锦囊妙计嘛,那赵云就是一个干活的人,从锦囊取出妙计,执行,然后获胜。用java程序怎么表现这些呢?
那我们先来看看图?

代码见Patterns.rar
分享到:
相关推荐
策略模式是一种设计模式,它允许在运行时动态地选择算法或策略,从而使程序能够根据不同的条件执行不同的行为。这种模式的核心思想是将算法封装在独立的类中,这些类具有相同的接口,使得它们可以互相替换而不会影响...
什么是策略模式? 引用一段百度百科的解释: 策略模式是指有一定行动内容的相对稳定的策略名称。策略模式在古代中又称“计策”,简称“计”,如《汉书·高帝纪上》:“汉王从其计”。这里的“计”指的就是计谋、...
策略模式是一种行为设计模式,它使你能在运行时改变对象的行为。在Java中,策略模式通常涉及接口或抽象类的实现,允许程序在运行时选择并应用不同的算法或策略。这种模式的核心在于将算法封装到独立的可互换的策略中...
策略模式是一种行为设计模式,它使你能在运行时改变对象的行为。在策略模式中,一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为模式。 策略模式定义了一系列的算法,并将每一个算法封装起来,使...
策略模式是一种行为设计模式,它使你能在运行时改变对象的行为。在C#中,策略模式的应用可以帮助我们处理复杂的问题,通过将算法封装在独立的类中,使得我们可以根据需要灵活地选择不同的策略来实现特定的功能。下面...
Java设计模式中的策略模式是一种行为设计模式,它允许在运行时选择算法或策略,从而提供了更大的灵活性。策略模式的核心在于将算法封装到独立的可互换的策略类中,使得算法的变化不会影响到使用算法的客户端代码。 ...
策略模式是一种设计模式,主要目的是将算法的实现与使用算法的环境解耦,使得算法可以在不修改客户端代码的情况下进行变化。这种模式的核心在于定义一系列的算法,并将每一个算法封装起来,使它们可以相互替换,同时...
策略模式是一种行为设计模式,它使你能在运行时改变对象的行为。在C++中,策略模式通常通过定义一系列算法类并让它们之间可以互换来实现。这些算法类都实现了一个公共接口,使得它们可以被相同的客户端代码调用,而...
技术: 1. spring事件驱动(ApplicationEventPublisher) 2. 策略模式处理事件 目的: 1. 通过event,代码逻辑异步处理 2. 通过策略模式,构建具体监听实现 3. 解耦 4. 容错(降低代码块错误风险)
接下来我们再说下什么叫策略模式:将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。 可能说了这么多你还没明白什么事策略...
策略模式是一种行为设计模式,它使你能在运行时改变对象的行为。在这个例子中,我们通过创建不同的图片保存策略(GIFSaver、JPEGSaver、PNGSaver)来实现这一模式,每个策略都实现了同一个接口`ImageSaver`,其中...
策略模式是一种行为型设计模式,它定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。在PHP中实现策略模式涉及到几个核心概念:抽象策略角色、具体策略角色和环境角色...
### 浅析.NET策略模式 #### 一、策略模式简介 策略模式是一种行为设计模式,它使你能在运行时改变对象的行为。此模式允许在运行时动态地选择算法或策略,从而实现对不同策略的选择与封装。在.NET环境中,策略模式...
在Java中,策略模式主要用来处理同一操作的不同实现,使得系统可以根据不同的条件选择不同的算法或行为。 策略模式的核心结构包括策略接口(Strategy Interface)、具体策略实现(Concrete Strategy ...
SpringBoot结合策略模式实战套路 策略模式是一种常用的设计模式,它可以使我们的代码更加灵活、可维护和可扩展。在SpringBoot项目中,策略模式可以与依赖注入机制相结合,实现更加灵活的业务逻辑处理。在本文中,...
策略模式是一种设计模式,它使你能在运行时改变对象的行为,通过将算法封装在独立的策略类中,使得相同的算法可以有不同的实现。这种模式的主要目的是将决策逻辑与核心业务逻辑解耦,使得算法的选择和使用更加灵活。...