实验08 软件设计模式及应用

news2024/10/6 14:29:18

目录

  • 实验目的
  • 实验内容
    • 一、能播放各种声音的软件产品
      • Sound.java
      • Dog.java
      • Violin.java
      • Simulator.java
      • Application.java
      • 运行结果
    • 二、简单工厂模式--女娲造人。
      • Human.java
      • WhiteHuman.java
      • YellowHuman.java
      • BlackHuman.java
      • HumanFactory.java
      • NvWa.java
      • 运行结果
    • 三、工厂方法模式--女娲造人。
      • Human.java
      • WhiteHuman.java
      • YellowHuman.java
      • BlackHuman.java
      • HumanFactory.java
      • WhiteHumanF.java
      • YellowHumanF.java
      • BlackHumanF.java
      • NvWa.java
      • 运行结果
    • 四、适配器模式--交流电转直流电。
      • AlternateCurrent.java
      • PowerCompany.java
      • DirectCurrent.java
      • ElectricAdapter.java
      • Wash.java
      • Recorder.java
      • Application.java
      • 运行结果
    • 五、策略模式--评分方案
      • Strategy.java
      • StrategyA.java
      • StrategyB.java
      • AverageScore.java
      • Application.java
      • 运行结果

实验目的

(1) 理解设计模式的基本概念
(2) 理解设计模式遵循的原则
(3) 掌握经典设计模式及应用

实验内容

一、能播放各种声音的软件产品

理解开-闭原则(Open-Closed Principle,OCP–对扩展开放-而对修改关闭
在这里插入图片描述

Sound.java

public interface Sound {
    public abstract void playSound();
}

Dog.java

public class Dog implements Sound{
    public void playSound(){
        System.out.println("汪汪…汪汪");
    }
}

Violin.java

public class Violin implements Sound{
    public void playSound(){
        System.out.println("小提琴.梁祝");
    }
}

Simulator.java

public class Simulator {
    private Sound sound;
    public void setSound(Sound sound){
        this.sound=sound;
    }
    public void play(){
        if (sound!=null){
            sound.playSound();
        }else {
            System.out.println("没有可播放的声音");
        }
    }
}

Application.java

public class Application {
    public static void main(String[] args) {
        Simulator simulator=new Simulator();
        simulator.setSound(new Dog());
        simulator.play();
        simulator.setSound(new Violin());
        simulator.play();
    }
}

运行结果

在这里插入图片描述

二、简单工厂模式–女娲造人。

女娲架起了八卦炉(技术术语:建立工厂),开始造人。
过程如下:先捏泥巴成人形,再放入八卦炉里烤,最后扔到地上成长。时间烤短了,造出了“白种人”;时间烤长了,造出了“黑种人”;时间烤得不长不短,造出了“黄种人”。
在这里插入图片描述

Human.java

public interface Human {
    public void talk();
}

WhiteHuman.java

public class WhiteHuman implements Human{
    public void talk(){
        System.out.println("Hello");
    }
}

YellowHuman.java

public class YellowHuman implements Human{
    public void talk(){
        System.out.println("您好");
    }
}

BlackHuman.java

public class BlackHuman implements Human{
    public void talk(){
        System.out.println("i am a BlackHuman");
    }
}

HumanFactory.java

public class HumanFactory {
    public static Human createHuman(String s){
        Human human=null;
        if (s.equals(new String("WhiteHuman"))){
            human=new WhiteHuman();
        }
        else if (s.equals(new String("YellowHuman"))){
            human=new YellowHuman();
        }
        else if (s.equals(new String("BlackHuman"))){
            human=new BlackHuman();
        }
        return human;
    }
}

NvWa.java

public class NvWa {
    public static void main(String[] args) {
        Human human=null;
        human=HumanFactory.createHuman("WhiteHuman");
        human.talk();
        human=HumanFactory.createHuman("YellowHuman");
        human.talk();
        human=HumanFactory.createHuman("BlackHuman");
        human.talk();
    }
}

运行结果

在这里插入图片描述

三、工厂方法模式–女娲造人。

在这里插入图片描述

Human.java

public interface Human {
    public void talk();
}

WhiteHuman.java

public class WhiteHuman implements Human{
    public void talk(){
        System.out.println("Hello");
    }
}

YellowHuman.java

public class YellowHuman implements Human{
    public void talk(){
        System.out.println("您好");
    }
}

BlackHuman.java

public class BlackHuman implements Human{
    public void talk(){
        System.out.println("i am a BlackHuman");
    }
}

HumanFactory.java

public interface HumanFactory {
    public Human createHuman();
}

WhiteHumanF.java

public class WhiteHumanF implements HumanFactory{
    public Human createHuman(){
        return new WhiteHuman();
    }
}

YellowHumanF.java

public class YellowHumanF implements HumanFactory{
    public Human createHuman(){
        return new YellowHuman();
    }
}

BlackHumanF.java

public class BlackHumanF implements HumanFactory{
    public Human createHuman(){
        return new BlackHuman();
    }
}

NvWa.java

public class NvWa {
    public static void main(String[] args) {
        HumanFactory humanFactory=null;
        Human human=null;
        humanFactory=new WhiteHumanF();
        human=humanFactory.createHuman();
        human.talk();
        humanFactory=new YellowHumanF();
        human=humanFactory.createHuman();
        human.talk();
        humanFactory=new BlackHumanF();
        human=humanFactory.createHuman();
        human.talk();
    }
}

运行结果

在这里插入图片描述

四、适配器模式–交流电转直流电。

用户家里现有一台洗衣机,洗衣机使用交流电,现在用户新买了一台录音机,录音机只能使用直流电。由于供电系统供给用户家里的是交流电,因此用户需要用适配器将交流电转化直流电供录音机使用。
在这里插入图片描述
在这里插入图片描述

AlternateCurrent.java

public interface AlternateCurrent {
    public String giveAlternateCurrent();
}

PowerCompany.java

public class PowerCompany implements AlternateCurrent{
    public String giveAlternateCurrent(){
        return "10101010101010101010";
    }
}

DirectCurrent.java

public interface DirectCurrent {
    public String giveDirectCurrent();
}

ElectricAdapter.java

public class ElectricAdapter implements DirectCurrent {
    AlternateCurrent out;
    ElectricAdapter (AlternateCurrent out){
        this.out=out;
    }
     public String giveDirectCurrent(){
         String m=out.giveAlternateCurrent();
         StringBuffer str=new StringBuffer(m);
         for (int i = 0; i < str.length(); i++)
             if (str.charAt(i)=='0') str.setCharAt(i,'1');
         m=new String(str);
         return m;
     }
}

Wash.java

public class Wash {
    String name;
    Wash(){
        name="洗衣机";
    }
    public void turnOn(AlternateCurrent a){
        String s =a.giveAlternateCurrent();
        System.out.println(name+"使用交流电:\n"+s);
        System.out.println("开始洗衣物");
    }
}

Recorder.java

public class Recorder {
    String name;
    Recorder (){
        name="录音机";
    }
    public void turnOn(DirectCurrent a){
        String s=a.giveDirectCurrent();
        System.out.println(name+"使用直流电:\n"+s);
        System.out.println("开始录音");
    }
}

Application.java

public class Application {
    public static void main(String[] args) {
        AlternateCurrent aElectric =new PowerCompany();
        Wash wash=new Wash();
        wash.turnOn(aElectric);
        DirectCurrent dElectric=new ElectricAdapter(aElectric);
        Recorder recorder=new Recorder();
        recorder.turnOn(dElectric);
    }
}

运行结果

在这里插入图片描述

五、策略模式–评分方案

在多个裁判负责打分的比赛中,每位裁判给选手一个得分,选手的最后得分是根据全体裁判的得分计算出来的。请给出几种计算选手得分的评分方案(策略),对于某次比赛,可以从你的方案中选择一种方案作为本次比赛的评分方案。
在这里插入图片描述
在这里插入图片描述

Strategy.java

public interface Strategy {
    public double computerAverage(double []a);
}

StrategyA.java

public class StrategyA implements Strategy{
    public double computerAverage(double a[]){
        double score=0,sum=0;
        for(int i=0;i<a.length;i++) {
            sum=sum+a[i];
        }
        score=sum/a.length;
        return score;
    }
}

StrategyB.java

import java.util.Arrays;
public class StrategyB implements Strategy{
    public double computerAverage(double a[]){
        if(a.length<=2)
            return 0;
        double score=0,sum=0;
        Arrays.sort(a);  //排序数组
        for (int i=1; i<a.length-1; i++) {
            sum=sum+a[i];
        }
        score=sum/(a.length-2);
        return score;
    }
}

AverageScore.java

public class AverageScore {
    Strategy strategy;
    public void setStrategy(Strategy strategy){
        this.strategy=strategy;
    }
    public double getAverage(double a[]){
        return strategy.computerAverage(a);
    }
}

Application.java

public class Application {
    public static void main(String[] args) {
        AverageScore game=new AverageScore();
        game.setStrategy(new StrategyA());
        double []a={9.12,9.25,8.87,9.99,6.99,7.88};
        double aver=game.getAverage(a);
        System.out.println(aver);
    }
}

运行结果

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1859486.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

React 扩展

文章目录 PureComponent1. 使用 React.Component&#xff0c;不会进行浅比较2. 使用 shouldComponentUpdate 生命周期钩子&#xff0c;手动比较3. 使用 React.PureComponent&#xff0c;自动进行浅比较 Render Props1. 使用 Children props&#xff08;通过组件标签体传入结构&…

nginx负载均衡案例,缓存知识----补充

负载均衡案例 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near great all on wordpress.* to wp172.16.1.% indentified by 1 at line 1 MariaDB [(none)]>…

iptables(5)常用扩展模块iprange、string、time、connlimit、limit

简介 之前我们已经介绍过扩展模块的简单使用,比如使用-m tcp/udp ,-m multiport参数通过--dports,--sports可以设置连续和非连续的端口范围。那么我们如何匹配其他的一些参数呢,比如源地址范围,目的地址范围,时间范围等,这就是我们这篇文章介绍的内容。 iprange扩展模块…

Jmeter插件管理器,websocket协议,Jmeter连接数据库,测试报告的查看

目录 1、Jmeter插件管理器 1、Jmeter插件管理器用处&#xff1a;Jmeter发展并产生大量优秀的插件&#xff0c;比如取样器、性能监控的插件工具等。但要安装这些优秀的插件&#xff0c;需要先安装插件管理器。 2、插件的下载&#xff0c;从Availabale Plugins中选择&#xff…

服务器(Linux系统的使用)——自学习梳理

root表示用户名 后是机器的名字 ~表示文件夹&#xff0c;刚上来是默认的用户目录 ls -a 可以显示出隐藏的文件 蓝色的表示文件夹 白色的是文件 ll -a 查看详细信息 total表示所占磁盘总大小 一般以KB为单位 d开头表示文件夹 -代表文件 后面得三组rwx分别对应管理员用户-组…

VSCode创建并运行html页面(使用Live Server插件)

目录 一、参考博客二、安装Live Server插件三、新建html页面3.1 选择文件夹3.2 新建html文件3.3 快速生成html骨架 四、运行html页面 一、参考博客 https://blog.csdn.net/zhuiqiuzhuoyue583/article/details/126610162 https://blog.csdn.net/m0_74014525/article/details/13…

设计模式5-策略模式(Strategy)

设计模式5-策略模式 简介目的定义结构策略模式的结构要点 举例说明1. 策略接口2. 具体策略类3. 上下文类4. 客户端代码 策略模式的反例没有使用策略模式的代码 对比分析 简介 策略模式也是属于组件协作模式一种。现代软件专业分工之后的第一个结果是框架语音应用程序的划分。组…

MATLAB-振动问题:单自由度无阻尼振动系统受迫振动

一、基本理论 二、MATLAB实现 令式&#xff08;1.3&#xff09;中A0 2&#xff0c;omega0 30&#xff0c;omega 40&#xff0c;matlab程序如下&#xff1a; clear; clc; close all;A0 2; omega0 30; omega 40; t 0:0.02:5; y A0 * sin( (omega0 - omega) * t /2) .* s…

【源码+文档+调试讲解】牙科就诊管理系统

摘 要 现代经济快节奏发展以及不断完善升级的信息化技术&#xff0c;让传统数据信息的管理升级为软件存储&#xff0c;归纳&#xff0c;集中处理数据信息的管理方式。本牙科就诊管理系统就是在这样的大环境下诞生&#xff0c;其可以帮助管理者在短时间内处理完毕庞大的数据信息…

计算机网络 访问控制列表以及NAT

一、理论知识 1. 单臂路由 单臂路由是一种在路由器上配置多个子接口的方法&#xff0c;每个子接口代表不同的 VLAN&#xff0c;用于在一个物理接口上支持多 VLAN 通信。此方法使得不同 VLAN 之间可以通过路由器进行通信。 2. NAT (网络地址转换) NAT 是一种在私有网络和公共…

vue+three.js渲染3D模型

安装three.js: npm install three 页面部分代码&#xff1a; <div style"width: 100%; height: 300px; position: relative;"><div style"height: 200px; background-color: white; width: 100%; position: absolute; top: 0;"><div id&…

小阿轩yx-MySQL数据库管理

小阿轩yx-MySQL数据库管理 使用 MySQL 数据库 在服务器运维工作中不可或缺的 SQL &#xff08;结构化查询语句&#xff09;的四种类型 数据定义语言&#xff08;DDL&#xff09;&#xff1a;DROP&#xff08;删除&#xff09;、CREATE&#xff08;创建&#xff09;、ALTER&…

帝国cms定时审核并更新的方法

比如你网站采集了成千上万篇文章&#xff0c;不可能一下子全部放出来的&#xff0c;所以为了模拟人工发布&#xff0c;那么就需要定时审核发布文章内容&#xff0c;本文内容核心解决了更加个性化的逼真模拟人工更新网站内容。 第一&#xff1a;首先要满足你的表中有未审核的数据…

JavaWeb——MySQL:DML

目录 DML&#xff1a;添加&#xff0c;修改&#xff0c;删除表的数据&#xff1b; 1. 添加表的数据 1.1 给所有列添加数据 1.2 给指定列添加数据 1.3 批量添加信息 总结&#xff1a; DML&#xff1a;添加&#xff0c;修改&#xff0c;删除表的数据&#xff1b; 1. 添加表的…

cube-studio开源一站式机器学习平台,在线ide,jupyter,vscode,matlab,rstudio,ssh远程连接,tensorboard

全栈工程师开发手册 &#xff08;作者&#xff1a;栾鹏&#xff09; 一站式云原生机器学习平台 前言 开源地址&#xff1a;https://github.com/tencentmusic/cube-studio cube studio 腾讯开源的国内最热门的一站式机器学习mlops/大模型训练平台&#xff0c;支持多租户&…

微博默认按照最新时间排序

微博默认按照最新时间排序 目前微博会默认按照推荐顺序排序&#xff0c;如果你想要默认按照时间顺序排序的话&#xff0c;可以使用这个油猴脚本。 演示&#xff1a; 脚本安装地址 源代码地址 参考 本项目基于 vite-plugin-monkey 开发 菜单切换&#xff08;useOption&…

设计模式原则——单一职责原则(SPS)

设计模式原则 设计模式示例代码库地址&#xff1a; https://gitee.com/Jasonpupil/designPatterns 单一职责原则&#xff08;SPS&#xff09;&#xff1a; 又称单一功能原则&#xff0c;面向对象五个基本原则&#xff08;SOLID&#xff09;之一原则定义&#xff1a;一个类应…

深度学习工具jupyter创建并检测pytorch环境以及安装工具包

1. 前言 确保已经安装Python和anaconda&#xff08;anaconda一般自带jupyter&#xff09;。然后创建一个jupyter环境&#xff0c;查看启动后的new有没有环境选项。 如果遇到了EnvironmentLocationNotFound:Not such a environment。说明conda环境安装位置有问题&#xff0c;往…

Qt6 windows10下悬浮按钮栏实现

打王者荣耀的时候,你是否有看到中间的按钮是悬浮在屏幕的左右侧,以及中下位置,背景则是一个播放着视频的画面。 从程序的角度来看,它就是实现了一个悬浮按钮栏的功能。当然了,你也可以做成悬浮列表,悬浮下拉框,悬浮xxx等控件。 这里,我们实现在Windows的版本。手机版…

Vue45 scope

scope 限制css的作用范围 用法 <template><div class"demo"><h2 >学生姓名&#xff1a;{{name}}</h2><h2>学生性别&#xff1a;{{sex}}</h2></div> </template><script>export default {name:Student,data…