首页 > 其他分享 >策略模式演示

策略模式演示

时间:2022-09-22 23:36:03浏览次数:42  
标签:演示 策略 double courseMark moralMark System 模式 using string

参考:

AwardSuper.cs

using System;
using System.Collections.Generic;
using System.Text;
namespace StrategyMode
{
    abstract class AwardSuper
{ public abstract string Award(double courseMark,double moralMark,double gymMark); } }

ThreeGoodAward.cs

using System;
using System.Collections.Generic;
using System.Text;
namespace StrategyMode
{
    class ThreeGoodAward : AwardSuper
    {
        public override string Award(double courseMark,double moralMark,double gymMark)
        {
            if (courseMark >= 80 && moralMark >= 80 && gymMark >= 80)
                return "三好学生";
            else
                return "";
        } 
    }
}

CourseAward.cs

using System;
using System.Collections.Generic;
using System.Text;
namespace StrategyMode
{
    class CourseAward :AwardSuper
    {
        public override string Award(double courseMark, double moralMark, double gymMark)
        {
            if (courseMark >= 80)
                return "课程奖";
            else
                return "";
        }
    }
}

MoralAward.cs:

using System;
using System.Collections.Generic;
using System.Text;
namespace StrategyMode
{
    class MoralAward :AwardSuper
    {
        public override string Award(double courseMark, double moralMark, double gymMark)
        {
            if (moralMark >= 90 && moralMark >= 60&&gymMark >= 60)
                return "道德奖";
            else
                return "";
        }
    }
}

AwardContext.cs:

using System;
using System.Collections.Generic;
using System.Text;
namespace StrategyMode
{
 class AwardContext
 {
    private AwardSuper aws;
    public AwardContext(AwardSuper asuper)
    {
        this.aws=asuper;
    }
    public string GetResult(double courseMark, double moralMark, double gymMark)
    {
        return aws.Award(courseMark,moralMark,gymMark);
    }
 }
}

Program.cs:

using System;
namespace StrategyMode
{
    class Program
    {
        static void Main(string[] args)
        {
            AwardContext cc = null;
            string strSel = string.Empty;
            Console.WriteLine("请选择奖励种类:");
            strSel=Console.ReadLine();
            switch(strSel)
            {
                case "three":
                    cc = new AwardContext(new ThreeGoodAward());
                    break;
                case "course":
                    cc = new AwardContext(new CourseAward());
                    break;
                case "moral":
                    cc = new AwardContext(new MoralAward());
                    break;
            }
            Console.WriteLine("请输入Course Mark:");
            string courseMark = Console.ReadLine();
            Console.WriteLine("请输入Moral Mark:");
            string moralMark = Console.ReadLine();
            Console.WriteLine("请输入Gym Mark:");
            string gymMark = Console.ReadLine();
            string strResult = cc.GetResult(Convert.ToDouble(courseMark),Convert.ToDouble(moralMark), Convert.ToDouble(gymMark));
            Console.WriteLine(strResult);
        }
    }
}

 

标签:演示,策略,double,courseMark,moralMark,System,模式,using,string
From: https://www.cnblogs.com/exesoft/p/16721192.html

相关文章

  • 设计模式-结构型模式之外观
    简化应用程序内部接口,提供一个单一接口以供外部(前端or客户侧)调用1classValley:23defrun(self):4self.pre_declare()5self.declar......
  • 设计模式之(12)——外观模式
    外观模式(facadePattern)又叫门面模式,隐藏了子系统的复杂实现,为子系统中的一组接口提供了一个统一的访问入口,使得子系统容易被访问或使用,说白了就是把复杂的子系统封装成......
  • 1.设计模式-单例
    单例模式单例模式的核心是保证一个类只有一个实例,并且提供一个访问实例的全局访问点。单例的使用场景Spring中bean对象的模式实现方式servlet中每个servlet的实例s......
  • 双向关联演示
    代码参考:usingSystem;namespaceInterdependDemo{classSchoolInfo{privateStudentInfostudentInfo;privateTeacherInfoteacherIn......
  • Python实现单例模式
    单例模式介绍:单例模式是一种常用的软件设计模型,该模式的主要目的是确保某一个类只有一个实例存在。当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场。......
  • 设计模式-创建型模式之工厂
    一、简单工厂创建对象的工作交由统一的函数接口来管理,传入不同的参数,返回不同的类对象1classAqSb:23def__repr__(self):4return"按期申......
  • JAVA中容器设计的进化史:从白盒到黑盒,再到跻身为设计模式之一的迭代器
    大家好,又见面了。在我们的项目编码中,不可避免的会用到一些容器类,我们可以直接使用List、Map、Set、Array等类型。当然,为了体现业务层面的含义,我们也会根据实际需要自行封......
  • VMware虚拟机三种网络模式
    桥接模式NAT模式主机模式......
  • 桥接模式
    桥接模式音频接口packagebridgetypeVideointerface{decode(string)}AVI格式packagebridgetypeAVIstruct{}funcNewAVI()*AVI{return&AVI......
  • linux系统管理类-备份策略
    1.如果一个系统没有任何的备份策略,请写出一个较为全面合理的备份方案!通用的系统数据目录-必须要备份的1.如果是数据库服务器:需要备份这些差异备份好处是,原始文件大......