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

目录

  • 实验目的
  • 实验内容
    • 一、能播放各种声音的软件产品
      • 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.mzph.cn/web/33632.shtml

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

相关文章

Python爬虫项目集:豆瓣电影排行榜top250

关于整理日常练习的一些爬虫小练习&#xff0c;可用作学习使用。 爬取项目以学习为主&#xff0c;尽可能使用更多的模块进行练习&#xff0c;而不是最优解。 爬虫概要 示例python 库爬取模块request解析模块BeautifulSoup存储类型list&#xff08;方便存入数据库&#xff09…

2024年最新 Windows 操作系统安装部署 MongoDB 数据库详细教程(更新中)

MongoDB 概述 MongoDB 是一个基于分布式文件存储的开源数据库系统&#xff0c;由C语言编写&#xff0c;旨在为WEB应用提供可扩展的高性能数据存储解决方案。MongoDB是一个介于关系数据库和非关系数据库之间的产品&#xff0c;是非关系数据库当中功能最丰富&#xff0c;最像关系…

数据库SqlServer笔试题

相关面试题 redis安装说明书 http://t.csdnimg.cn/iM260 单体并发瓶颈 redis sqlsever mysql多少 http://t.csdnimg.cn/DTXIh Redis高频面试题http://t.csdnimg.cn/FDOnv 数据库SqlServer笔试题 数据库SqlServer笔试题-CSDN博客 SQL 大数据查询如何进行优化&#xff1f;sqlser…

深入探索:Spring JdbcTemplate的数据库访问之歌

介绍 在当今的企业应用程序开发中&#xff0c;与数据库进行交互是至关重要的一环。Spring框架为我们提供了多种方式来简化数据库访问&#xff0c;其中之一就是Spring JdbcTemplate。 Spring JdbcTemplate是Spring框架中的一个核心模块&#xff0c;它提供了一种优雅的方式来使…

使用mysql的binlog进行数据恢复

1.mysql安装环境 在你本地电脑windows上建一个和生产环境一样的mysql版本 我的是 mysql5.7.43 安装教程可以自行上网搜&#xff08;这里不做介绍&#xff09; 可参考&#xff1a; 1.1安装路径 我的mysql安装路径&#xff1a; D:\mysql\mysql-5.7.43-winx64\bin * 1.2my.in…

Docker部署私有仓库(registryHarbor)

简介Docker Hub 官方仓库 在 Docker 中&#xff0c;当我们执行 docker pull xxx 的时候 &#xff0c;它实际上是从 registry.hub.docker.com 这个地址去查找&#xff0c;这就是Docker公司为我们提供的公共仓库。在工作中&#xff0c;我们不可能把企业项目push到公有仓库进行管理…

并查集 Rank 的优化

并查集 Rank 的优化 并查集是一种数据结构,用于处理一些不交集的合并及查询问题。它支持两种操作:查找(Find)和合并(Union)。查找操作用于确定某个元素属于哪个子集,而合并操作用于将两个子集合并成一个集合。在并查集中,每个子集用一棵树来表示,树根的元素作为该子集…

讲座学习截图——《CAD/CAE/CAM几何引擎-软件概述》(一)

目录 引出CAD/CAE/CAM几何引擎-软件概述 郝建兵CADCAECAM 几何模型内核ACIS 两个老大之一Open CascadeParasolid 两个老大之一Autodesk的内核 总结其他自定义信号和槽1.自定义信号2.自定义槽3.建立连接4.进行触发 自定义信号重载带参数的按钮触发信号触发信号拓展 lambda表达式…

每天一个数据分析题(三百八十四)- 回归与分类

关于回归与分类问题的讨论不正确的是&#xff1a; A. 回归问题的目标变量通常是连续的数值变量&#xff0c;而分类问题的目标变量通常是离散的分类变量 B. 回归问题和分类问题同属于有监督学习范畴 C. 回归问题最常用的评价指标体系有混淆矩阵以及ROC曲线 D. 回归问题的常见…

02_RISC-V RTOS系统移植及启动

系统移植&#xff1a; https://so.csdn.net/so/search?spm1001.2100.3001.4498&qRISCV%E7%A7%BB%E6%A4%8DRT-Thread&t&uRT-thread移植指南-RISC-V&#xff1a;https://blog.csdn.net/ty1121466568/article/details/120455709riscv cpu 移植 rt-thread 需要考虑的…

C/C++ 类型转换

char* 转 string const char *name "hello"; String Str name;Serial.printf("%s\n", Str); string 转 char* String str "hello"; char *p (char *)str.c_str();Serial.printf("%s\n", p); char *转 char[] const char *str …

设置Nginx缓存策略

详细信息 Nginx服务器的缓存策略设置方法有两种&#xff1a;add_header或者expires。 1. add_header 1&#xff09;语法&#xff1a;add_header name value。 2&#xff09;默认值&#xff1a;none。 3&#xff09;使用范围&#xff1a;http、server、location。 配置示例…

双目相机测距原理

一、普通双目相机测距原理 普通双目相机具有如下特点&#xff1a;左右两个相机位于同一平面&#xff08;光轴平行&#xff09;&#xff0c;且相机参数&#xff08;焦距f&#xff09;一致。其原理图如下&#xff1a; 如图所示&#xff0c;P点为相应的物体位置&#xff0c;CL和C…

【等保】网络安全等级保护(等保2.0PPT)

等保2.0&#xff08;网络安全等级保护基本要求的第二代标准&#xff09;的推出和实施&#xff0c;是基于多方面的考虑和需求。以下是实施等保2.0的主要原因&#xff1a; 加强网络安全保护&#xff1a; 随着网络技术的不断发展和网络威胁的不断增加&#xff0c;传统的网络安全保…

2024年广西三支一扶报名详细流程(附报名照处理流程)​

2024年广西将招募1650名高校毕业生到基层从事支农、支医、支教和帮扶乡村振兴工作&#xff08;简称“三支一扶”&#xff09;。 招募对象为全日制普通高校应届及择业期内2022年至2024年毕业的全日制普通高校毕业生。 ➡️招募条件。 1.具有全日制大专&#xff08;含高职高专&am…

B端系统:配置页面如何设计,这可是用户体验的关键的关键。

提升配置页面体验的十大原则 设计B端系统的配置页面时&#xff0c;用户体验确实是非常关键的。以下是一些设计原则和建议&#xff0c;可以帮助提高配置页面的用户体验&#xff1a; 简洁明了&#xff1a;配置页面应该尽量简洁明了&#xff0c;避免过多的复杂选项和信息。使用清…

【代码阅读】SSC:Semantic Scan Context for Large-Scale Place Recognition

一、主函数 官方开源的代码提供了四个主函数&#xff0c;其中eval_pair.cpp和eval_top1.cpp是一组&#xff0c;分别用于计算两帧的相似度分数以及一帧点云在所有的51帧点云中相似度最高的25帧的相似度分数。eval_seq.cpp是在eval_top1.cpp的基础上&#xff0c;给了一堆序列&am…

PointPillars安装

PointPillars使⽤Pillar Feature Net (PFN)将原始点云数据转换为伪图像&#xff08;pseudo-image&#xff09;。 以KITTI的激光 雷达坐标系为例&#xff0c;若输入点云的截取范围[x_min, y_min, z_min, x_max, y_max, z_max]为[0, -39.68, -3, 69.12, 39.68, 1], 且每个pillar…

暑期工作闭坑指南

**暑期工作闭坑指南** 随着暑期的到来&#xff0c;许多学生都会选择利用这段时间进行实习或兼职工作&#xff0c;以增加实践经验、提升个人能力&#xff0c;并赚取一定的收入。然而&#xff0c;在寻找暑期工作时&#xff0c;往往会遇到一些陷阱和风险。为了帮助大家避开这些坑…

must be built with the ios 17 sdk or later,included in Xcode 15 or later.

2024.4.29 号开始&#xff0c;苹果又开始搞开发者了。 Xcode - 支持 - Apple Developer xcode可以从这里下载&#xff0c; Sign In - Apple 电脑不支持&#xff0c;头疼&#xff0c;必须 macOS Ventura 13.5 或以上才能支持。 电脑哪里搞&#xff0c;再买一台吗&#xff1f; 用…