基于MVC模式下的⼆⼿车拍卖平台设计
课程名称:《软件体系结构与设计模式》电脑通过手机上网
实验项⽬名称:基于MVC模式下的⼆⼿车拍卖平台设计
实验内容 按下列要求编写程序并上机调试运⾏:
1. 拍卖系统要⼜三个显⽰界⾯,分别是车体图⽚,⽂字描述和出价区。
2. 将系统分解成MVC三个类进⾏⽅法设计。
3. M和V必须观察者模式进⾏设计。
4. 使⽤SWING进⾏界⾯设计。
5. 按钮点击,必须更新车型和图⽚,并⽤时间驱动结构进⾏设计。
6. 额外要求:要求报价必须只能提⾼,不等减少,如减少弹出警告窗⼝。
以下写类图和代码
类图:
分析与代码展⽰:
其中,CarGUI提供⽤户输⼊界⾯,代表⽤户;CarModel为MVC体系结构中的模型部分(Model);BitView和SearchView代表视图部分(View);⽽Controller为控制器部分。
⽤户通过使⽤CarGUI图形界⾯在的列表选择待拍卖的车,单击“Search”按钮,获取车的图⽚和⽂字说明;然后输⼊竞拍价,单
击“Bit”按钮,竞拍价将显⽰在拍卖价格显⽰区域上;若下次的拍卖价格⽐这次的价格低,则会弹出警告窗⼝消息框并会在拍卖价格显⽰区域上显⽰⼀条信息:“Bit price for”+CarName+“:”+“illegal bit price”。
程序运⾏结果图如下图所⽰。
图1
图2
图3
图4
Observer 类
Observable 类
在该设计类图中设计了两个接⼝类:Observable与Observer,Model类实现Observable接⼝,⽽View类实现Observer接⼝。也就是说Model对象是被观察者⽽View对象是观察者。根据观察者机制,被观察者类应该包含registerObserver( )、notifyObservers( )⽅法,其功能是在被观察者对象中利⽤registerObserver( )加⼊被观察者对象,并且当被观察者的数据或者状态有了改变的时候,利⽤
notifyObservers( )⽅法通知观察者。此时,观察者类中的update( )⽅法将⾃动运⾏,达到及时更新观察者的⽬的。
CarModel 类public interface Observer {    public void update(Observable subject);}
1
2
3public interface Observable {    public void register(Observer obs);    public void notifyObservers();}
1
2
3
4
BitView 类import java.util.ArrayList;public class CarModel implements Observable{    private ArrayList<Observer> observers;    private String carName;    private String bitPrice;    private ImageIcon icon;    String [] carSelected = {"Honda Accord"," "," ","Made in 2005, used by the Department of defense.","Mileage: 60,000 miles.","5-speed automatic (25 city/3    String [] car2Selected = {"Honda Civic"," "," ","Made in 2006,used by the New York State government ","Mileage 40,4000 miles.","5-speed manual (25 city    String [] car3Selected = {"Toyota Camry"," "," ","Made in 2003, used by the Washington State government","Mileage: 100,000 miles.","5-speed automatic    String [] car4Selected = {"Toyota Corolla"," "," ","Made in 2002, used by the Washington State government","Mileage: 120,000 miles.","5-speed automatic    public CarModel() {        observers = new ArrayList<Observer>();    }    public String getCarName() {        return carName;    }    public String getBitPrice() {        return bitPrice;    }    public void setCarName(String carName) {        this.carName = carName;        notifyObservers();    }    public void setBitPrice(String bitPrice) {        th
is.bitPrice = bitPrice;        notifyObservers();    }    public ImageIcon getImage() {        return this.icon;    }    public void setImage(String file) {        this.icon = new ImageIcon(file);        notifyObservers();    }    @Override    public void register(Observer obs) {        observers.add(obs);    }    @Override    public void notifyObservers() {        for (int i = 0; i < observers.size(); i++) {            (i).update(this);        }    }}
2
3
4
5
6
7
8
9
10
11
12
13
14
冬天打雷民间有什么说法15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
1231539
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
青春有你2前9名邵小珊55
56
57
58
SearchView 类
其中,CarGUI提供了⽤户输⼊界⾯,代表⽤户。注意,在类CarGUI中,创建与初始化了CarModel、SearchView和BitView对象,并且调⽤CarModel中的register(Observer obs),分别将SearchView和BitView的对象注册到该CarModel对象。
①箱⼦布局管理,主要是Swing包中的Box来进⾏盒⼦布局,包括两种箱⼦:⼀种是⽔平箱⼦、⼀种是垂直箱⼦。构造⽅法如下:Box ateHorizontalBox()//创建⽔平箱⼦对象
Box ateVertiaclBox()//创建垂直箱⼦对象
愚人节整人大全
箱⼦创建好后,⽤add()把组件添加到箱⼦⾥⾯
②设置控件间隔的⽅法
CarGUI 类public class BitView implements Observer{    private final CarModel cm;    private final CarGUI cg;    public BitView(CarModel cm,CarGUI cg) {        = cm;        =cg;    }    @Override    public void update(Observable subject) {        if ( subject == cm){            String name= cm.getCarName();            String p = cm.getBitPrice();            String text=" Bit price for "+ name + " : "+ p;            ImageIcon imIcon = cm.getImage();            cg.setIcon(imIcon);//更新拍卖车图⽚区域            cg.showPrice(text);//更新拍卖价格显⽰区域        }    }}
2
3
4
5
6
7
8
9
10
11
12
13
14
1516
17
18
19
20
21
22
23
24
25
26
27import javax.swing.*;public class SearchView implements Observer {    private final CarModel cm; 
  private final CarGUI cg;    public SearchView(CarModel cm,CarGUI cg) {        = cm;        =cg;    }    @Override    public void update(Observable subject) {        if ( subject == cm){            ImageIcon imIcon = cm.getImage();            cg.setIcon(imIcon);        }    }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19