package com.pattern.strategy;/** 策略模式(别名:政策)* 定义一系列算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。* 优点:* 1.上下文(Context)和具体策略(ConcreteStrategy)是松耦合关系。因此上下文只知道它要使用某一个实现Strategy接口类的实例,但不需要知道具体是哪一个类。* 2.策略模式满足"开-闭原则"。当增加新的具体策略时,不需要修改上下文类的代码,上下文就可以引用新的具体策略的实例。*/public class Test {public static void main(String [] args){StudentSorce dtuScore = new StudentSorce();double[] zhanshan = {87,90,88,87,96};double[] lishi = {89,84,92,91,81};StrategyOne strAverage = new StrategyOne();dtuScore.setStrategy(strAverage);System.out.println("张三各科平均分为:"+dtuScore.GetStudentSorce(zhanshan));System.out.println("李四各科平均分为:"+dtuScore.GetStudentSorce(lishi));StrategyTwo strTotalMark = new StrategyTwo();dtuScore.setStrategy(strTotalMark);System.out.println("张三各科总分为:"+dtuScore.GetStudentSorce(zhanshan));System.out.println("李四各科总分为:"+dtuScore.GetStudentSorce(lishi));}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。