java实验策略模式观察者模式和组合模式.docx
实验二 策略模式、观察者模式和组合模式一、实验目的:(1)学习和掌握策略模式;(2)学习和掌握观察者模式;(3)学习和掌握组合模式;(4)学习和掌握使用这些模式解决实际问题;二、实验内容1. 请使用策略模式实现下列编程要求:已知几何形状家族有圆形、矩形、椭圆形、三角形等形状。请用Java语言定义一个抽象类MyShape表示形状这样概念,MyShape抽象类中提供了计算面积、周长、显示形状信息的抽象方法,然后分别定义它的子类MyCircle(圆形)、MyRectangle(矩形)、MyEllipse(椭圆形)、MyTriangle(三角形)等特定几何形状。并绘制这些定义的几何家族的所有形状。2 请用观察者模式实现功能:学校发布信息,学生可以订阅,老师可以订阅,行政人员也可以订阅。提示:定义主题接口,观察者接口,定义Notice、学生、老师和行政人员类,定义测试类。其中Notice类实现主题接口,老师、学生和行政人员实现观察者接口。思考,如果要求实现学生和老师均可以订阅多个信息,即除了订阅学校发布信息,也可以订阅所属系发送的信息,请编程实现。3定义一个游戏地图。地图是由每个方块拼合起来。地图上有墙等障碍物,也有可以通行的基本图元构成。请使用组合模式,绘制一个游戏地图,地图的内容自行定义,也可以类似图3形式: 图3游戏地图4已知有一个二维数组数据如图1所示,请结合策略模式、观察者模式和组合模式实现一个MVC结构的应用程序。要求:如果用户移动滑块,可以修改二维数组的值,并在运行的用户界面中显示出来。其中,饼状图和柱状图中的区域分布为二位数组每一维的总和。运行结果如图4和图5所示。605090904030102070 图4 数据模型图5运行结果三、实验环境1、PC微机;2、DOS操作系统或 Windows 操作系统;3、jdk程序集成环境Eclipse四、源代码、测试结果及UML图一:1.UML图2. 源程序代码:package course.strategy.shape;import java.awt.Graphics;import java.util.ArrayList;import java.util.List;import javax.swing.JPanel;/绘制所有图形SuppressWarnings("serial")public class DrawShape extends JPanel private List<MyShape> shapes; public DrawShape() shapes=new ArrayList<MyShape>(); public void addShape(MyShape shape) /添加图形 if(shape!=null) shapes.add(shape); public void paintComponent(Graphics g) /依次绘制图形 for(MyShape ms:shapes) ms.draw(g); System.out.println(ms); 、package course.strategy.shape;import java.awt.Graphics;/形状家族public abstract class MyShape public abstract float area(); public abstract float perimeter(); public abstract void draw(Graphics g);、package course.strategy.shape;import java.awt.Color;import java.awt.Graphics;/圆形public class MyCircle extends MyShape private static final float PI=3.14f; private int radius; /定义圆形半径 private int x,y; /定义起点(x,y)坐标 public MyCircle(int x,int y,int radius) /构造函数super();this.x=x;this.y=y;this.radius = radius;Overridepublic float area() /求面积/ TODO 自动生成的方法存根return PI*radius*radius;Overridepublic float perimeter() /求周长/ TODO 自动生成的方法存根return 2*PI*radius;Overridepublic String toString() return "MyCircle radius=" + radius + ", x坐标=" + x + ", y坐标=" + y + ""+ " 面积="+area()+",周长="+perimeter();Overridepublic void draw(Graphics g) /绘制图形/ TODO 自动生成的方法存根g.setColor(Color.blue);g.fillOval(x, y, 2*radius, 2*radius);、package course.strategy.shape;import java.awt.Color;import java.awt.Graphics;/矩形public class MyRectangle extends MyShape private int x,y; /定义起点(x,y)坐标private int width,height; /定义矩形的宽和高 public MyRectangle(int x,int y,int width, int height) super();this.x=x;this.y=y;this.width = width;this.height = height;Overridepublic float area() / TODO 自动生成的方法存根return width*height;Overridepublic float perimeter() / TODO 自动生成的方法存根return 2*(width+height);Overridepublic String toString() return "MyRectangle x坐标=" + x + ", y坐标=" + y + ", 矩形宽=" + width+ ", 矩形高=" + height + ""+" 面积="+area()+",周长="+perimeter();Overridepublic void draw(Graphics g) / TODO 自动生成的方法存根g.setColor(Color.green);g.fillRect(x, y, width, height);、package course.strategy.shape;import java.awt.Color;import java.awt.Graphics;/椭圆形public class MyEllipse extends MyShape private static final float PI=3.14f;private int x,y; /定义起点(x,y)坐标private int aLong,bShort; /定义椭圆的长轴和短轴 public MyEllipse(int x, int y,int aLong, int bShort) super();this.x = x;this.y = y;this.aLong = aLong;this.bShort = bShort; Overridepublic float area() / TODO 自动生成的方法存根return 1.0f/4*PI*aLong*bShort;Overridepublic float perimeter() / TODO 自动生成的方法存根return PI*bShort+2*(aLong-bShort);Overridepublic String toString() return "MyEllipse x坐标=" + x + ", y坐标=" + y + ", 长轴=" + aLong+ ", 短轴=" + bShort + ""+" 面积="+area()+",周长="+perimeter();Overridepublic void draw(Graphics g) / TODO 自动生成的方法存根g.setColor(Color.red);g.fillOval(x, y, aLong, bShort);、package course.strategy.shape;import java.awt.Color;import java.awt.Graphics;/三角形public class MyTriangle extends MyShape private int a,b,c,n; private int xPoints; private int yPoints;public MyTriangle( int xPoints, int yPoints,int n,int a,int b,int c) super();this.xPoints = xPoints;this.yPoints = yPoints;this.n = n;this.a=a; this.b=b; this.c=c;Overridepublic float area() / TODO 自动生成的方法存根float p=(a+b+c)/2; return (float)Math.sqrt(p*(p-a)*(p-b)*(p-c);Overridepublic float perimeter() / TODO 自动生成的方法存根return a+b+c;Overridepublic String toString() return "MyTriangle a=" + a + ", b=" + b + ", c=" + c + ""+ " 面积="+area()+",周长="+perimeter();Overridepublic void draw(Graphics g) / TODO 自动生成的方法存根g.setColor(Color.yellow);g.fillPolygon(xPoints, yPoints, n);、package course.strategy.shape;import javax.swing.JFrame;/测试程序public class Test /测试绘制所有形状/* * param args */public static void main(String args) / TODO 自动生成的方法存根 JFrame frame=new JFrame(); DrawShape ds=new DrawShape(); ds.addShape(new MyCircle(20,20,60); ds.addShape(new MyRectangle(200,20,120,100); ds.addShape(new MyEllipse(40,200,150,100); int xPoints=300,240,370; int yPoints=200,280,300; ds.addShape(new MyTriangle(xPoints,yPoints,3,9,12,15); frame.getContentPane().add(ds); frame.setTitle("绘制图形演示"); frame.setSize(500,400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 3. 测试结果图:二:1.UML图:2. 源代码:package course.observer.notice;/主题public interface Subject public void registerObserver(Observer observer); public void removeObserver(Observer observer); public void notifyAllObservers();、package course.observer.notice;import java.util.ArrayList;import java.util.Iterator;import java.util.List;/学校通知public class Notice implements Subject private List<Observer> observers; private String message; public Notice() observers=new ArrayList<Observer>(); Overridepublic void registerObserver(Observer observer) / TODO 自动生成的方法存根if(!observers.contains(observer) observers.add(observer); Overridepublic void removeObserver(Observer observer) / TODO 自动生成的方法存根if(observers.contains(observer) observers.remove(observer);Overridepublic void notifyAllObservers() / TODO 自动生成的方法存根 for(Iterator<Observer> it=observers.iterator();it.hasNext();) Observer o=it.next(); o.getNotice(getMessage(); public void setMessage(String str) this.message=str; public String getMessage() return message; 、package course.observer.notice;import java.util.ArrayList;import java.util.Iterator;import java.util.List;/学院通知public class DepartmentNotice implements Subject private List<Observer> observers; private String message; public DepartmentNotice() observers=new ArrayList<Observer>(); Overridepublic void registerObserver(Observer observer) / TODO 自动生成的方法存根observers.add(observer);Overridepublic void removeObserver(Observer observer) / TODO 自动生成的方法存根observers.remove(observer);Overridepublic void notifyAllObservers() / TODO 自动生成的方法存根for(Iterator<Observer> it=observers.iterator();it.hasNext();) Observer o=it.next(); o.getNotice(getMessage(); public void setMessage(String str) this.message=str; public String getMessage() return message; 、package course.observer.notice;/观察者public interface Observer public void getNotice(String message);、package course.observer.notice;/具体观察者,教师public class Teacher implements Observer Overridepublic void getNotice(String message) / TODO 自动生成的方法存根System.out.println("教师收到通知:"+message);、package course.observer.notice;/具体观察者,学生public class Student implements Observer Overridepublic void getNotice(String message) / TODO 自动生成的方法存根System.out.println("学生收到通知:"+message);、package course.observer.notice;/具体观察者,行政人员public class Administrator implements Observer Overridepublic void getNotice(String message) / TODO 自动生成的方法存根System.out.println("行政人员收到通知:"+message);、package course.observer.notice;/测试程序public class Test /* * param args */public static void main(String args) / TODO 自动生成的方法存根 Notice notice=new Notice(); DepartmentNotice depart=new DepartmentNotice(); Observer tea,stu,admin; tea=new Teacher(); stu=new Student(); admin=new Administrator(); notice.registerObserver(tea); notice.registerObserver(stu); notice.registerObserver(admin); depart.registerObserver(tea); depart.registerObserver(stu); notice.setMessage("学校通知:五一全体放假七天。"); depart.setMessage("学院通知:这个礼拜周末补课,请各位做好心里准备。"); notice.notifyAllObservers(); System.out.println(); depart.notifyAllObservers(); System.out.println(); notice.removeObserver(tea); notice.notifyAllObservers();3. 测试结果图:三: 1.UML图2. 源代码package posite.gameMap;import java.awt.Dimension;import java.awt.Graphics;import javax.swing.JPanel;SuppressWarnings("serial")public abstract class GameComponent extends JPanel public abstract void paintComponent(Graphics g); public Dimension getPreferredSize() return new Dimension(50,50); 、package posite.gameMap;import java.awt.Graphics;import java.awt.Image;import javax.swing.BorderFactory;import javax.swing.ImageIcon;SuppressWarnings("serial")public class Grass extends GameComponent private ImageIcon icon;private Image image;public Grass()setBorder(BorderFactory.createEmptyBorder();icon=new ImageIcon("src/images/grass.jpg"); image=icon.getImage();Overridepublic void paintComponent(Graphics g) / TODO 自动生成的方法存根g.drawImage(image,0,0,getWidth(),getHeight(),this);、package posite.gameMap;import java.awt.Graphics;import java.awt.Image;import javax.swing.ImageIcon;SuppressWarnings("serial")public class House extends GameComponent private ImageIcon icon;private Image image; public House() icon=new ImageIcon("src/images/house.jpg"); image=icon.getImage(); Overridepublic void paintComponent(Graphics g) / TODO 自动生成的方法存根 g.drawImage(image,0,0,getWidth(),getHeight(),this);、package posite.gameMap;import java.awt.Graphics;import java.awt.Image;import javax.swing.ImageIcon;SuppressWarnings("serial")public class Tree extends GameComponent private ImageIcon icon;private Image image;public Tree()icon=new ImageIcon("src/images/tree.jpg"); image=icon.getImage();Overridepublic void paintComponent(Graphics g) / TODO 自动生成的方法存根g.drawImage(image,0,0,getWidth(),getHeight(),this);、package posite.gameMap;import java.awt.Graphics;import java.awt.Image;import java.io.File;import javax.imageio.ImageIO;import javax.swing.ImageIcon;SuppressWarnings("serial")public class Wall extends GameComponentprivate ImageIcon icon;private Image image; public Wall() icon=new ImageIcon("src/images/wall.jpg"); image=icon.getImage(); Overridepublic void paintComponent(Graphics g) / TODO 自动生成的方法存根tryimage=ImageIO.read(new File("src/images/wall.jpg");g.drawImage(image,0,0,getWidth(),getHeight(),this);catch(Exception e)e.printStackTrace(); 、package posite.gameMap;import java.awt.Graphics;import java.awt.Image;import javax.swing.ImageIcon;SuppressWarnings("serial")public class Road extends GameComponent private ImageIcon icon;private Image image; public Road() icon=new ImageIcon("src/images/road.jpg"); image=icon.getImage(); Overridepublic void paintComponent(Graphics g) / TODO 自动生成的方法存根 g.drawImage(image,0,0,getWidth(),getHeight(),this);、package posite.gameMap;import java.awt.Color;import java.awt.Container;import java.awt.GridLayout;import javax.swing.JFrame;/测试程序SuppressWarnings("serial")public class Test extends JFrame private GridLayout layou