目录
- 实验目的
- 实验内容
- 一、能播放各种声音的软件产品
- 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);
}
}