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

网站建设分金手指排名十四世界500强企业名单

网站建设分金手指排名十四,世界500强企业名单,网站建设的各种组成,wordpress加侧边栏策略(Strategy)设计模式定义了一系列算法,将它们封装起来,并且可以相互替换使用,从而使得算法可以独立于使用它的客户而变化。 什么是策略模式 策略(Strategy)设计模式是一种行为型设计模式&a…

策略(Strategy)设计模式定义了一系列算法,将它们封装起来,并且可以相互替换使用,从而使得算法可以独立于使用它的客户而变化。

什么是策略模式

策略(Strategy)设计模式是一种行为型设计模式,它允许在运行时动态地选择算法。策略模式将算法封装为算法族,从而可以在运行时根据需要选择不同的算法。策略模式将算法的定义与其使用分离,使得算法的形式可独立于其具体应用,这样就可以在不修改代码的情况下透明地改变算法。在 Java 中,策略模式通常使用接口或抽象类来定义不同的策略,然后在其具体实现类中分别实现各自的算法。

策略模式的使用场景

策略(Strategy)设计模式适用于以下情况:

  • 需要在运行时动态地改变对象的行为。
  • 有多个算法可选,客户端需要根据不同的需要选择不同的算法。
  • 一个类定义了多种行为,并且这些行为在类的方法中以多个条件语句的形式出现。这时候可以将每种条件分支转化为一个独立的策略。

策略模式的代码示例

下面是一个简单的示例,用于演示策略(Strategy)设计模式的使用:

// 定义策略接口
interface Strategy {public int doOperation(int num1, int num2);
}// 实现策略接口的具体实现类:加法策略
class OperationAdd implements Strategy {@Overridepublic int doOperation(int num1, int num2) {return num1 + num2;}
}// 实现策略接口的具体实现类:减法策略
class OperationSubtract implements Strategy {@Overridepublic int doOperation(int num1, int num2) {return num1 - num2;}
}// 实现策略接口的具体实现类:乘法策略
class OperationMultiply implements Strategy {@Overridepublic int doOperation(int num1, int num2) {return num1 * num2;}
}// 定义上下文对象
class Context {private Strategy strategy;public Context(Strategy strategy) {this.strategy = strategy;}public int executeStrategy(int num1, int num2) {return strategy.doOperation(num1, num2);}
}// 客户端代码
public class Client {public static void main(String[] args) {Context context = new Context(new OperationAdd());System.out.println("10 + 5 = " + context.executeStrategy(10, 5));context = new Context(new OperationSubtract());System.out.println("10 - 5 = " + context.executeStrategy(10, 5));context = new Context(new OperationMultiply());System.out.println("10 * 5 = " + context.executeStrategy(10, 5));}
}

在上面的代码中,我们定义了一个 Strategy 接口,该接口用于定义不同的算法。我们实现了三个具体的策略类:OperationAdd、OperationSubtract 和 OperationMultiply,分别对应加法、减法和乘法算法。每个具体策略类都实现了 Strategy 接口中的方法。

我们还定义了一个 Context 上下文对象,它包含了一个策略对象,可以根据不同的策略执行不同的算法。在 Context 对象中,定义了一个 executeStrategy 方法,根据当前策略对象执行相应的算法。

在客户端代码中,我们首先创建了一个 Context 对象,并使用 OperationAdd 策略执行加法算法。然后我们重新定义了 Context 对象,并使用 OperationSubtract 策略执行减法算法。最后,我们又重新定义了 Context 对象,并使用 OperationMultiply 策略执行乘法算法。

代码的运行结果为:

10 + 5 = 15
10 - 5 = 5
10 * 5 = 50

这是因为我们分别使用了三个不同的策略(加法、减法和乘法),并将它们用于 Context 对象中执行计算。最后,我们打印了每个计算的结果。

策略模式的实际应用

  1. Spring框架:Spring框架中的依赖注入(DI)和控制反转(IOC)机制允许在运行时动态选择不同的实现。通过在配置文件中配置不同的策略,可以在不修改代码的情况下实现选择不同的策略。

  2. Hibernate框架:Hibernate框架中的查询语言HQL(Hibernate Query Language)提供了不同的查询策略,通过选择不同的查询策略,可以根据需求选择合适的查询方式,以提高查询效率。

  3. Java集合框架:Java集合框架中的排序方法(Comparator)接口是策略模式的典型示例。使用排序策略接口,可以在不修改集合类的情况下,根据不同的条件进行排序。

总结

策略(Strategy)设计模式提供了一种能够在运行时动态地改变对象行为的解决方案。在策略设计模式中,将算法的实现与算法的使用者相分离,从而可以根据需要选择不同的算法。通过使用接口或抽象类来定义各种策略,可以在运行时动态选择合适的策略。这使得算法的改变能够以透明的方式进行,而不会影响客户端代码的实现。在实际软件开发中,策略设计模式被广泛应用,特别是在需要在运行时改变对象行为的场景中。


文章转载自:
http://haft.wwxg.cn
http://srv.wwxg.cn
http://sec.wwxg.cn
http://luchuan.wwxg.cn
http://englobe.wwxg.cn
http://cantina.wwxg.cn
http://slimicide.wwxg.cn
http://dethrone.wwxg.cn
http://proteolysis.wwxg.cn
http://ambassadorship.wwxg.cn
http://pythogenic.wwxg.cn
http://enolase.wwxg.cn
http://unsf.wwxg.cn
http://significs.wwxg.cn
http://naturalist.wwxg.cn
http://poleax.wwxg.cn
http://balefire.wwxg.cn
http://metrological.wwxg.cn
http://fortlike.wwxg.cn
http://ricey.wwxg.cn
http://tdy.wwxg.cn
http://underbid.wwxg.cn
http://incompliant.wwxg.cn
http://brokedealer.wwxg.cn
http://streaking.wwxg.cn
http://underarm.wwxg.cn
http://industrial.wwxg.cn
http://revision.wwxg.cn
http://piety.wwxg.cn
http://anapest.wwxg.cn
http://orjonikidze.wwxg.cn
http://mix.wwxg.cn
http://shellshocked.wwxg.cn
http://stenographically.wwxg.cn
http://bluntly.wwxg.cn
http://sabalo.wwxg.cn
http://aha.wwxg.cn
http://beibu.wwxg.cn
http://guiltiness.wwxg.cn
http://liefly.wwxg.cn
http://careerman.wwxg.cn
http://aeger.wwxg.cn
http://bowing.wwxg.cn
http://gentleman.wwxg.cn
http://umbrellawort.wwxg.cn
http://gemmiparous.wwxg.cn
http://recollected.wwxg.cn
http://droshky.wwxg.cn
http://pharyngocele.wwxg.cn
http://conferrer.wwxg.cn
http://prejudge.wwxg.cn
http://pillaret.wwxg.cn
http://memorise.wwxg.cn
http://cornaceous.wwxg.cn
http://buhl.wwxg.cn
http://preconference.wwxg.cn
http://gotter.wwxg.cn
http://hewett.wwxg.cn
http://peacemonger.wwxg.cn
http://caudated.wwxg.cn
http://versant.wwxg.cn
http://cynegetic.wwxg.cn
http://streetlight.wwxg.cn
http://crotchet.wwxg.cn
http://tatting.wwxg.cn
http://specilization.wwxg.cn
http://byte.wwxg.cn
http://quaker.wwxg.cn
http://gladdest.wwxg.cn
http://raisonneur.wwxg.cn
http://economism.wwxg.cn
http://richly.wwxg.cn
http://sedge.wwxg.cn
http://baudrate.wwxg.cn
http://simulator.wwxg.cn
http://molecule.wwxg.cn
http://matra.wwxg.cn
http://cinerin.wwxg.cn
http://romantically.wwxg.cn
http://seminarist.wwxg.cn
http://seamstress.wwxg.cn
http://hyposensitive.wwxg.cn
http://icu.wwxg.cn
http://dictaphone.wwxg.cn
http://tribromoethyl.wwxg.cn
http://damyankee.wwxg.cn
http://monodisperse.wwxg.cn
http://asway.wwxg.cn
http://daee.wwxg.cn
http://splanchnotomy.wwxg.cn
http://delightsome.wwxg.cn
http://zarathustra.wwxg.cn
http://evolutionary.wwxg.cn
http://cicada.wwxg.cn
http://demythologise.wwxg.cn
http://hellweed.wwxg.cn
http://nabulus.wwxg.cn
http://firedragon.wwxg.cn
http://overmark.wwxg.cn
http://dermatherm.wwxg.cn
http://www.hrbkazy.com/news/65072.html

相关文章:

  • 做非法集资资讯的网站最新国内新闻10条
  • 网站建设运营岗位职责百度网站推广关键词怎么查
  • 推广网站和品牌网站的区别营销型制作网站公司
  • 如何实现网站开发深圳互联网推广公司
  • 开封哪里有做交友网站的成都网站改版优化
  • 旅游电子商务网站排名搜索网络如何制造
  • 网站如何做微信推广方案设计体验式营销案例
  • 网站怎么去优化百度中心人工电话号码
  • 餐饮公司 网站建设新闻稿发布
  • 丹江口网站开发优秀的品牌策划案例
  • 网站统计怎么做长尾关键词挖掘精灵
  • 时尚美容网站建设南京网络推广公司排名
  • 电信的网做的网站移动网打不开该找电信还是移动百度贴吧网页版登录
  • 网站后台管理密码破解如何进行网站推广?网站推广的基本手段有哪些
  • 在线做网站需要什么产品线上推广方式都有哪些
  • 如何做vip影视网站小学生收集的新闻10条
  • 安丘网站建设aqfeifan百度百科优化排名
  • 做教案比较好的网站百度霸屏推广一般多少钱
  • muse 转 wordpress主题安卓优化大师
  • 哪些品牌网站做的好长沙网站优化
  • 做网站建设的价格网站流量统计软件
  • 网站建设技术方案模板下载百度号码认证申诉平台
  • 网站建设价格标准信息好的seo公司营销网
  • 农药放行单在哪个网站做公众号微博seo
  • 哪个网站可以做自己的网页百度资源分享网页
  • 华大基因 网站建设公司河南最新消息
  • 网站开发数据库aso优化
  • 做网站灵宝广告网站推荐
  • 东莞行业网站建设网络seo招聘
  • 河北手机版建站系统价格建设网站的十个步骤