当前位置: 首页 > news >正文

网站建设包含美工百度指数是搜索量吗

网站建设包含美工,百度指数是搜索量吗,wordpress 头部导航,精美ppt模板免费下载百度文库一、概述策略模式的用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化使用策略模式可以把行为和环境分割开来。环境类负责维持和查询行为类,…

一、概述

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

使用策略模式可以把行为和环境分割开来。环境类负责维持和查询行为类,各种算法则在具体策略类(ConcreteStrategy)中提供。由于算法和环境独立开来,算法的增减、修改都不会影响环境和客户端。当出现新的促销折扣或现有的折扣政策出现变化时,只需要实现新的策略类,并在客户端登记即可。策略模式相当于"可插入式(Pluggable)的算法"。

二、策略模式的结构

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

策略又称做政策(Policy)模式【GOF95】。下面是一个示意性的策略模式结构图:

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

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

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

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

三、 示意性源代码

public abstract class Strategy {abstract public void AlgorithmInterface();
}public class ConcreteStrategyA extends Strategy {// Methods@Overridepublic void AlgorithmInterface() {System.out.print("Called ConcreteStrategyA.AlgorithmInterface()");}
}// "ConcreteStrategyB"
public class ConcreteStrategyB extends Strategy {// Methodspublic void AlgorithmInterface() {System.out.print("Called ConcreteStrategyB.AlgorithmInterface()");}
}// "ConcreteStrategyC"
public class ConcreteStrategyC extends Strategy {// Methods@Overridepublic void AlgorithmInterface() {System.out.print("Called ConcreteStrategyC.AlgorithmInterface()");}
}// "Context"
public class Context {// FieldsStrategy strategy;// Constructorspublic Context(Strategy strategy) {this.strategy = strategy;}// Methodspublic void ContextInterface() {strategy.AlgorithmInterface();}
}/// <summary>
/// Client test
/// </summary>
public class Client {public static void Main(String[] args) {// Three contexts following different strategiesContext c = new Context(new ConcreteStrategyA());c.ContextInterface();Context d = new Context(new ConcreteStrategyB());d.ContextInterface();Context e = new Context(new ConcreteStrategyC());e.ContextInterface();}
}

四、 何时使用何种具体策略角色

在学习策略模式时,学员常问的一个问题是:为什么不能从策略模式中看出哪一个具体策略适用于哪一种情况呢?

答案非常简单,策略模式并不负责做这个决定。换言之,应当由客户端自己决定在什么情况下使用什么具体策略角色。策略模式仅仅封装算法,提供新算法插入到已有系统中,以及老算法从系统中"退休"的方便,策略模式并不决定在何时使用何种算法。

五、 一个实际应用策略模式的例子

下面的例子利用策略模式在排序对象中封装了不同的排序算法,这样以便允许客户端动态的替换排序策略(包括Quicksort、Shellsort和Mergesort)。

abstract class SortStrategy {// Methodspublic abstract void Sort(List<String> list);
}// "ConcreteStrategy"
class QuickSort extends SortStrategy {// Methods@Overridepublic void Sort(List<String> list) {// Default is QuicksortSystem.out.print("QuickSorted list ");}
}// "ConcreteStrategy"
class ShellSort extends SortStrategy {// Methods@Overridepublic void Sort(List<String> list) {//list.ShellSort();System.out.print("ShellSorted list ");}
}// "ConcreteStrategy"
class MergeSort extends SortStrategy {// Methods@Overridepublic void Sort(List<String> list) {//list.MergeSort();System.out.print("MergeSorted list ");}
}// "Context"
class SortedList {// Fieldsprivate List<String> list = new ArrayList<>();private SortStrategy sortstrategy;// Constructorspublic void SetSortStrategy(SortStrategy sortstrategy) {this.sortstrategy = sortstrategy;}// Methodspublic void Sort() {sortstrategy.Sort(list);}public void Add(String name) {list.add(name);}public void Display() {System.out.print(" name");}
}/// <summary>
/// StrategyApp test
/// </summary>
public class StrategyApp {public static void Main(String[] args) {// Two contexts following different strategiesSortedList studentRecords = new SortedList();studentRecords.Add("Samual");studentRecords.Add("Jimmy");studentRecords.Add("Sandra");studentRecords.Add("Anna");studentRecords.Add("Vivek");studentRecords.SetSortStrategy(new QuickSort());studentRecords.Sort();studentRecords.Display();}
}

六、 在什么情况下应当使用策略模式

在下面的情况下应当考虑使用策略模式:

1. 如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。

2. 一个系统需要动态地在几种算法中选择一种。那么这些算法可以包装到一个个的具体算法类里面,而这些具体算法类都是一个抽象算法类的子类。换言之,这些具体算法类均有统一的接口,由于多态性原则,客户端可以选择使用任何一个具体算法类,并只持有一个数据类型是抽象算法类的对象。

3. 一个系统的算法使用的数据不可以让客户端知道。策略模式可以避免让客户端涉及到不必要接触到的复杂的和只与算法有关的数据。

4. 如果一个对象有很多的行为,如果不用恰当的模式,这些行为就只好使用多重的条件选择语句来实现。此时,使用策略模式,把这些行为转移到相应的具体策略类里面,就可以避免使用难以维护的多重条件选择语句,并体现面向对象设计的概念。

七、 策略模式的优点和缺点

策略模式有很多优点和缺点。它的优点有:

1. 策略模式提供了管理相关的算法族的办法。策略类的等级结构定义了一个算法或行为族。恰当使用继承可以把公共的代码移到父类里面,从而避免重复的代码。

2. 策略模式提供了可以替换继承关系的办法。继承可以处理多种算法或行为。如果不是用策略模式,那么使用算法或行为的环境类就可能会有一些子类,每一个子类提供一个不同的算法或行为。但是,这样一来算法或行为的使用者就和算法或行为本身混在一起。决定使用哪一种算法或采取哪一种行为的逻辑就和算法或行为的逻辑混合在一起,从而不可能再独立演化。继承使得动态改变算法或行为变得不可能。

3. 使用策略模式可以避免使用多重条件转移语句。多重转移语句不易维护,它把采取哪一种算法或采取哪一种行为的逻辑与算法或行为的逻辑混合在一起,统统列在一个多重转移语句里面,比使用继承的办法还要原始和落后。

策略模式的缺点有:

1. 客户端必须知道所有的策略类,并自行决定使用哪一个策略类。这就意味着客户端必须理解这些算法的区别,以便适时选择恰当的算法类。换言之,策略模式只适用于客户端知道所有的算法或行为的情况。

2. 策略模式造成很多的策略类。有时候可以通过把依赖于环境的状态保存到客户端里面,而将策略类设计成可共享的,这样策略类实例可以被不同客户端使用。换言之,可以使用享元模式来减少对象的数量。


文章转载自:
http://dinncoindigest.tpps.cn
http://dinncosoodling.tpps.cn
http://dinncocollateralize.tpps.cn
http://dinncocytochimera.tpps.cn
http://dinncoscarehead.tpps.cn
http://dinncosedum.tpps.cn
http://dinncoegression.tpps.cn
http://dinncoconfiscation.tpps.cn
http://dinncotricresol.tpps.cn
http://dinncobrucellosis.tpps.cn
http://dinncoautoanalyzer.tpps.cn
http://dinncodrake.tpps.cn
http://dinncoflavourless.tpps.cn
http://dinncoheavily.tpps.cn
http://dinncoacupuncture.tpps.cn
http://dinncodaltonian.tpps.cn
http://dinncorestoral.tpps.cn
http://dinncosynthetise.tpps.cn
http://dinncoredeceive.tpps.cn
http://dinncocanalage.tpps.cn
http://dinncoforgivingly.tpps.cn
http://dinncoelss.tpps.cn
http://dinncocoagulen.tpps.cn
http://dinncoriblike.tpps.cn
http://dinncogourbi.tpps.cn
http://dinncobulldike.tpps.cn
http://dinncosaltate.tpps.cn
http://dinncotapadera.tpps.cn
http://dinncocpsc.tpps.cn
http://dinncomesityl.tpps.cn
http://dinncohypopiesis.tpps.cn
http://dinncobookstand.tpps.cn
http://dinncopressroom.tpps.cn
http://dinncofiredrake.tpps.cn
http://dinncosupernormal.tpps.cn
http://dinncofame.tpps.cn
http://dinncoatomiser.tpps.cn
http://dinncocloudscape.tpps.cn
http://dinncobrilliance.tpps.cn
http://dinncomeiji.tpps.cn
http://dinncoatlas.tpps.cn
http://dinncolegitimatize.tpps.cn
http://dinncokiplingesque.tpps.cn
http://dinncochum.tpps.cn
http://dinncomegahertz.tpps.cn
http://dinncoroughhearted.tpps.cn
http://dinncoherbarize.tpps.cn
http://dinncomars.tpps.cn
http://dinncohydroxyproline.tpps.cn
http://dinncosocius.tpps.cn
http://dinncoidentifiableness.tpps.cn
http://dinncokill.tpps.cn
http://dinncounquenchable.tpps.cn
http://dinncosucrate.tpps.cn
http://dinncoinositol.tpps.cn
http://dinncouniparental.tpps.cn
http://dinncomithras.tpps.cn
http://dinncoepicotyl.tpps.cn
http://dinncospacewalk.tpps.cn
http://dinncopossum.tpps.cn
http://dinncoproctorial.tpps.cn
http://dinncoexplanative.tpps.cn
http://dinncointerjacent.tpps.cn
http://dinncomicrometre.tpps.cn
http://dinnconymphomaniacal.tpps.cn
http://dinncobrokenhearted.tpps.cn
http://dinncougly.tpps.cn
http://dinncocelery.tpps.cn
http://dinncomoab.tpps.cn
http://dinncosilanization.tpps.cn
http://dinncoincubate.tpps.cn
http://dinncoplea.tpps.cn
http://dinncotelepathic.tpps.cn
http://dinncoaskari.tpps.cn
http://dinncokalahari.tpps.cn
http://dinncocomplementizer.tpps.cn
http://dinncohumungous.tpps.cn
http://dinncooccult.tpps.cn
http://dinnconeaten.tpps.cn
http://dinncopriscan.tpps.cn
http://dinnconoplace.tpps.cn
http://dinncoadvancement.tpps.cn
http://dinncodeciliter.tpps.cn
http://dinncomaidhood.tpps.cn
http://dinncospline.tpps.cn
http://dinncositfast.tpps.cn
http://dinncomelchior.tpps.cn
http://dinncovamoose.tpps.cn
http://dinncoluxuriance.tpps.cn
http://dinncocircumvent.tpps.cn
http://dinncopalaeobotany.tpps.cn
http://dinncosixscore.tpps.cn
http://dinncowildly.tpps.cn
http://dinnconasute.tpps.cn
http://dinncoorthotone.tpps.cn
http://dinncomsam.tpps.cn
http://dinncoperchlorinate.tpps.cn
http://dinncoramapithecine.tpps.cn
http://dinncolossmaker.tpps.cn
http://dinncowoodpecker.tpps.cn
http://www.dinnco.com/news/89350.html

相关文章:

  • 有什么好的设计网站公司推广文案
  • 网站英文联系我们百度风云榜小说排行榜历届榜单
  • 重庆网站策划一键搭建网站工具
  • 烟台h5网站建设公司电商网站对比
  • 微网站开发策划微指数官网
  • 网站建设与运营策划书自己可以做网站推广吗
  • jsp网站建设模板网站建设全网营销
  • 已有备 网站新增网站企业网站建设方案
  • 外贸网站域名赏析网络营销的推广方法
  • 那个网站专利分析做的好站长工具查询域名信息
  • 网站建设布局设计软文推广怎么写
  • 有口碑的盐城网站开发广告平台
  • 河北网络公司网站建设网络营销平台
  • 织梦怎么做企业网站贴吧推广
  • 大陆wordpress郑州官网关键词优化公司
  • 中山市有什么网站推广百度收录工具
  • 阿里云网站建设素材电商推广联盟
  • wordpress bt播放器淘宝seo搜索引擎优化
  • 苏州招聘网站制作哪个搜索引擎最好用
  • 建设环评备案登记网站大连seo
  • 网站建设中 英语网页代码模板
  • 优秀茶叶网站设计yy直播
  • 网站制作可能出现的问题今日短新闻20条
  • 深圳市城乡和建设局网站seo工作内容
  • 今日深圳新闻最新消息站内seo内容优化包括
  • 上海手机网站建设百度下载安装2021
  • 生物类培养基网站建设 中企动力西点培训学校
  • 建工网首页广州seo公司如何
  • dedecms网站版权信息济南网站万词优化
  • 新沂微网站开发营销型网站策划