java实验策略模式观察者模式和组合模式Word下载.docx
《java实验策略模式观察者模式和组合模式Word下载.docx》由会员分享,可在线阅读,更多相关《java实验策略模式观察者模式和组合模式Word下载.docx(50页珍藏版)》请在冰点文库上搜索。
![java实验策略模式观察者模式和组合模式Word下载.docx](https://file1.bingdoc.com/fileroot1/2023-4/30/f2f79fe7-c4a9-4698-85c3-2cb8024535f2/f2f79fe7-c4a9-4698-85c3-2cb8024535f21.gif)
提示:
定义主题接口,观察者接口,定义Notice、学生、老师和行政人员类,定义测试类。
其中Notice类实现主题接口,老师、学生和行政人员实现观察者接口。
思考,如果要求实现学生和老师均可以订阅多个信息,即除了订阅学校发布信息,也可以订阅所属系发送的信息,请编程实现。
3.定义一个游戏地图。
地图是由每个方块拼合起来。
地图上有墙等障碍物,也有可以通行的基本图元构成。
请使用组合模式,绘制一个游戏地图,地图的内容自行定义,也可以类似图3形式:
图3游戏地图
4.已知有一个二维数组数据如图1所示,请结合策略模式、观察者模式和组合模式实现一个MVC结构的应用程序。
要求:
如果用户移动滑块,可以修改二维数组的值,并在运行的用户界面中显示出来。
其中,饼状图和柱状图中的区域分布为二位数组每一维的总和。
运行结果如图4和图5所示。
60
50
90
40
30
10
20
70
图4数据模型
图5运行结果
三、实验环境
1、PC微机;
2、DOS操作系统或Windows操作系统;
3、jdk程序集成环境Eclipse
四、源代码、测试结果及UML图
一:
1.UML图
2.源程序代码:
packagecourse.strategy.shape;
importjava.awt.Graphics;
importjava.util.ArrayList;
importjava.util.List;
importjavax.swing.JPanel;
//绘制所有图形
@SuppressWarnings("
serial"
)
publicclassDrawShapeextendsJPanel{
privateList<
MyShape>
shapes;
publicDrawShape(){
shapes=newArrayList<
();
}
publicvoidaddShape(MyShapeshape){//添加图形
if(shape!
=null)shapes.add(shape);
}
publicvoidpaintComponent(Graphicsg){//依次绘制图形
for(MyShapems:
shapes){
ms.draw(g);
System.out.println(ms);
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
//形状家族
publicabstractclassMyShape{
publicabstractfloatarea();
publicabstractfloatperimeter();
publicabstractvoiddraw(Graphicsg);
importjava.awt.Color;
//圆形
publicclassMyCircleextendsMyShape{
privatestaticfinalfloatPI=3.14f;
privateintradius;
//定义圆形半径
privateintx,y;
//定义起点(x,y)坐标
publicMyCircle(intx,inty,intradius){//构造函数
super();
this.x=x;
this.y=y;
this.radius=radius;
@Override
publicfloatarea(){//求面积
//TODO自动生成的方法存根
returnPI*radius*radius;
publicfloatperimeter(){//求周长
return2*PI*radius;
publicStringtoString(){
return"
MyCircle[radius="
+radius+"
x坐标="
+x+"
y坐标="
+y+"
]"
+
"
面积="
+area()+"
周长="
+perimeter();
publicvoiddraw(Graphicsg){//绘制图形
g.setColor(Color.blue);
g.fillOval(x,y,2*radius,2*radius);
//矩形
publicclassMyRectangleextendsMyShape{
privateintwidth,height;
//定义矩形的宽和高
publicMyRectangle(intx,inty,intwidth,intheight){
this.width=width;
this.height=height;
publicfloatarea(){
returnwidth*height;
publicfloatperimeter(){
return2*(width+height);
MyRectangle[x坐标="
矩形宽="
+width
+"
矩形高="
+height+"
+"
publicvoiddraw(Graphicsg){
g.setColor(Color.green);
g.fillRect(x,y,width,height);
//椭圆形
publicclassMyEllipseextendsMyShape{
privateintaLong,bShort;
//定义椭圆的长轴和短轴
publicMyEllipse(intx,inty,intaLong,intbShort){
this.x=x;
this.y=y;
this.aLong=aLong;
this.bShort=bShort;
return1.0f/4*PI*aLong*bShort;
returnPI*bShort+2*(aLong-bShort);
MyEllipse[x坐标="
长轴="
+aLong
短轴="
+bShort+"
g.setColor(Color.red);
g.fillOval(x,y,aLong,bShort);
//三角形
publicclassMyTriangleextendsMyShape{
privateinta,b,c,n;
privateint[]xPoints;
privateint[]yPoints;
publicMyTriangle(int[]xPoints,int[]yPoints,intn,inta,intb,intc){
this.xPoints=xPoints;
this.yPoints=yPoints;
this.n=n;
this.a=a;
this.b=b;
this.c=c;
floatp=(a+b+c)/2;
return(float)Math.sqrt(p*(p-a)*(p-b)*(p-c));
returna+b+c;
MyTriangle[a="
+a+"
b="
+b+"
c="
+c+"
g.setColor(Color.yellow);
g.fillPolygon(xPoints,yPoints,n);
importjavax.swing.JFrame;
//测试程序
publicclassTest{//测试绘制所有形状
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODO自动生成的方法存根
JFrameframe=newJFrame();
DrawShapeds=newDrawShape();
ds.addShape(newMyCircle(20,20,60));
ds.addShape(newMyRectangle(200,20,120,100));
ds.addShape(newMyEllipse(40,200,150,100));
intxPoints[]={300,240,370};
intyPoints[]={200,280,300};
ds.addShape(newMyTriangle(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.源代码:
packagecourse.observer.notice;
//主题
publicinterfaceSubject{
publicvoidregisterObserver(Observerobserver);
publicvoidremoveObserver(Observerobserver);
publicvoidnotifyAllObservers();
importjava.util.Iterator;
//学校通知
publicclassNoticeimplementsSubject{
Observer>
observers;
privateStringmessage;
publicNotice(){
observers=newArrayList<
publicvoidregisterObserver(Observerobserver){
if(!
observers.contains(observer))
observers.add(observer);
publicvoidremoveObserver(Observerobserver){
if(observers.contains(observer))
observers.remove(observer);
publicvoidnotifyAllObservers(){
for(Iterator<
it=observers.iterator();
it.hasNext();
){
Observero=it.next();
o.getNotice(getMessage());
publicvoidsetMessage(Stringstr){
this.message=str;
publicStringgetMessage(){
returnmessage;
//学院通知
publicclassDepartmentNoticeimplementsSubject{
publicDepartmentNotice(){
observers.add(observer);
observers.remove(observer);
for(Iterator<
//观察者
publicinterfaceObserver{
publicvoidgetNotice(Stringmessage);
//具体观察者,教师
publicclassTeacherimplementsObserver{
publicvoidgetNotice(Stringmessage){
System.out.println("
教师收到通知:
"
+message);
//具体观察者,学生
publicclassStudentimplementsObserver{
学生收到通知:
//具体观察者,行政人员
publicclassAdministratorimplementsObserver{
行政人员收到通知:
publicclassTest{