资源描述
计算机科学和技术学院
《软件设计模式和体系结构》
课程作业(一)
(/ 第二学期)
学生姓名: 雷 君
学生专业: 软件工程
学生班级: 142601
学生学号: 26170108
指导老师: 王 飞
目 录
试验一 1
工厂方法模式——汽车保险 1
抽象工厂方法模式——房屋信息 3
试验二 6
组合模式——空军指挥系统 6
适配器模式——用户信息验证 8
试验三 11
桥接模式——几何立体体积 11
访问者模式——计算机部件销售软件 14
试验四 17
策略模式——整数排序 17
状态模式——交通信号灯 19
试验五 21
MVC软件体系结构 21
试验一
工厂方法模式——汽车保险
【试验内容】
在例2.3汽车保险管理应用程序实例上添加一个名为LuxuryCarInsurance类,而且,该类要和其它类一样能实施对应功效。
【添加代码】
1、 添加LuxuryCarInsurance类:
public class LuxuryCarInsurance implements AutoInsurance
{
private String description;
public String getInsuranceDescription()
{
description = " LuxuryCarInsurance: \n\nLuxuryCarInsurance coverage pays
for medical bills" +
" lost wages, rehabilitation, treatment and/or" +
" funeral costs for anyone injured or killed " +
" by your car. Such coverage will also pay for" +
" pain and suffering damages when a third " +
" party successfully sues. ";
return description;
}
}
2、 添加LuxuryCarPolicyProducer类:
public class LuxuryCarPolicyProducer implements PolicyProducer
{
public AutoInsurance getPolicyObj() //Fruit factory()
{
return new LuxuryCarInsurance();
}
}
3、 添加GUI:
(1)public static final String LUXURYCAR = "LuxuryCar Insurance";
(2)cmbInsuranceType.addItem(LUXURYCAR);
(3)if (type.equals(LUXURYCAR)) {
pp=new LuxuryCarPolicyProducer();
}
【试验结果】
【试验小结】
使用工厂方法访问而且初始化适宜类对象,简化了应用程序,应用程序本身不再含有大量条件语句判定何时选择哪个类。其次,工厂方法实现了部分特殊某个类机制,尤其是层次结构不一样类需要不一样初始化方法时候。
抽象工厂方法模式——房屋信息
【试验内容】
在例2.4中设计而且实现了豪华(Super)和中等(Medum)别墅(House)和公寓(Condo)查询。要求在该设计基础上,增加一个新类SemiDetacher(半独立式楼宇),而且编写代码实现对应查询功效。
【添加代码】
1、添加SemiDetacher类:
public interface SemiDetacher
{
public String getSemiDetacherInfo();
public String getSemiDetacherFeatures();
}
2、 添加SuperSemiDetacher类:
public class SuperSemiDetacher implements SemiDetacher
{
private String name;
public SuperSemiDetacher(String cName)
{
name = cName;
}
public String getSemiDetacherInfo()
{
return "superSemiDetacher.html";
}
public String getSemiDetacherFeatures()
{
return "Super SemiDetacher ";
}
}
3、添加MediumSemiDetacher类:
public class MediumSemiDetacher implements SemiDetacher
{
private String name;
public MediumSemiDetacher(String cName)
{
name = cName;
}
public String getSemiDetacherInfo()
{
return "MediumSemiDetacher.html";
}
public String getSemiDetacherFeatures()
{
return "Medium SemiDetacher ";
}
}
4、 添加BuildingFactory:
public abstract SemiDetacher getSemiDetacher();
5、添加MediumBuildingFactory:
public SemiDetacher getSemiDetacher()
{
return new MediumSemiDetacher("Medium SemiDetacher");
}
6、 添加SuperBuildingFactory:
public SemiDetacher getSemiDetacher()
{
return new SuperSemiDetacher("Super SemiDetacher");
}
7、添加GUI:
(1)public static final String SEMIDETACHER = "SemiDetacher";
(2)cmbHouseType.addItem(SEMIDETACHER);
(3)if (type.equals(AbstractFactoryGUI.SEMIDETACHER)) {
SemiDetacher cd = bf.getSemiDetacher();
String fileNm = cd.getSemiDetacherInfo();
putHouseInfoToScreen(fileNm);
}
【试验结果】
【试验小结】
当用户对象要从一个相关产品组中创建一个对象,而没有必需知道到底要创建哪个对象时,能够使用抽象工厂模式。假如不使用抽象工厂模式,创建对象条件语句将会出现在用户程序很多地方,程序克维护性差。抽象工厂模式帮助程序员避免了以上所述反复、复杂条件语句,提供必需创建对象接口。
试验二
组合模式——空军指挥系统
【试验内容】
在例3.3设计中,添加一个空军大队(Wing)类,该类和Squadron、Group类是平行,所以应该继承了AirUnit类。该类写法和Squadron或Group类是类似,所不一样是一个Wing有216中类型飞机。
【添加代码】
1、 添加Wing类:
public class Wing extends AirUnit{
public static final String FEATURES = "A Wing with 216 aircrafts";
Airforce[] fighters = new Airforce[162];
Airforce[] bombers = new Airforce[18];
Airforce[] transporters= new Airforce[18];
Airforce[] eAircrafts = new Airforce[18];
public Wing(){
for(int k=0;k<162;k++){
// need 162 fighters
}
for(int k=0;k<18;k++){
// need 18 bombers
}
for(int k=0;k<18;k++){
// need 18 transporters
}
for(int k=0;k<18;k++){
// need 18 eAirplanes
}
}
public String getDescription(){
return FEATURES;
}
public String fight(){
return super.fight();
}
}
2、 添加GUI:
(1)private String[] AirForceUnit = {"SQUADRON", "GROUP", "WING"};
(2)add(1, 6, airCheckBox[13]);
(3)else if ((m==13) && (ckBoxStates[13] == SELECTED)){
unit = new Wing();
airUnits.attach(unit);
unitInfo = unit.getDescription();
}
【试验结果】
【试验小结】
我们这么来简单了解组合模式,组合模式就是把部分现有对象或元素,经过组合后组成新对象,新对象提供内部方法,能够让我们很方便完成这些元素或内部对象访问和操作。我们也能够把组合对象了解成一个容器,容器提供多种访问其内部对象或元素API,我们只需要使用这些方法就能够操作它了。
适配器模式——用户信息验证
【试验内容】
相关例3.7用于验证用户信息离架产品类CusInfo Validation功效扩展问题。要求使用适配器模式。
【添加代码】
1、 添加InformationAdapter:
public boolean isValidEmailAddr(String EmailAddr){
boolean isValid=true;
int a=0;
int b=0;
String ns = EmailAddr.trim();
String nStr = ns.replaceAll("\\s{1,}", "");
int len = nStr.length();
if ( (((nStr.charAt(0) >='A')&&(nStr.charAt(0) >='Z'))||
((nStr.charAt(0) >='a')&&(nStr.charAt(0) >='z'))) && (len>=5) ) {
for(int m=0; m<len; m++){
if( (Character.isLetter(nStr.charAt(m))==true)&&
( Character.isDigit(nStr.charAt(m))==true) ){
isValid=false;
}
if(nStr.charAt(m)=='@'){
a++;
}
if(nStr.charAt(m)>='0'&&nStr.charAt(m)<='9'){
b++;
}
if((m==0)&&(Character.isLetter(nStr.charAt(m))==false)){
isValid=false;
}
}
if(a!=1){
isValid=false;
}
if(b==0){
isValid=false;
}
return isValid;
}
else{
return false;
}
}
2、 添加CusInfoValidator:
public abstract boolean isValidEmailAddr(String EmailAddr);
3、添加GUI:
(1)private JTextField txtCustomerName, txtAddress,txtZip,txtCellPhone,txtSSN,
txtEmailAddr;
(2)private JLabel lblCustomerName, lblAddress,lblZip, lblCellphone, lblSSN,
lblEmailAddr;
(3)txtEmailAddr=new JTextField(20);
(4)lblEmailAddr= new JLabel("EmailAddr :");
(5)UIPanel.add(lblEmailAddr);
UIPanel.add(txtEmailAddr);
(6)gridbag.setConstraints(lblEmailAddr, gbc);
gbc.gridx = 1;
gbc.gridy = 5;
gridbag.setConstraints(txtEmailAddr, gbc);
gbc.gridx = 0;
gbc.gridy = 6;
(7)public String getEmailAddr(){
return txtEmailAddr.getText();
}
(8)String emailaddr = getEmailAddr();
(9)if(cusInfo.isValidEmailAddr(emailaddr)==false){
dataTextArea.append("\nWrong format of EmailAddr.");
}
else{
dataTextArea.append("\nCorrect format of EmailAddr.");
}
【试验结果】
【试验小结】
经过适配器,用户端能够调用同一接口,所以对用户端来说是透明。这么做更简单、更直接、更紧凑;复用了现存类,处理了现存类和复用环境要求不一致问题;将目标类和适配者类解耦,经过引入一个适配器类重用现有适配者类,而无需修改原有代码; 一个对象适配器能够把多个不一样适配者类适配到同一个目标,也就是说,同一个适配器能够把适配者类和它子类全部适配到目标接口。对于对象适配器来说,更换适配器实现过程比较复杂。
试验三
桥接模式——几何立体体积
【试验内容】
在例3.14中设计试验层次类部分中,添加Ellipsoid(椭球)类,而且实现针对椭球体积计算。
【添加代码】
1、添加椭球类:
public class Ellipsoid implements GeoForm{
private double aRadius;
private double bRadius;
private double cRadius;
public Ellipsoid (double aRadius, double bRadius, double cRadius){
this.aRadius = aRadius;
this.bRadius = bRadius;
this.cRadius = cRadius;
}
public double computeVolume(){
double volume = 1.3333333*3.1415926* aRadius* bRadius* cRadius;
return volume;
}
}
2、添加GUI:
(1)private JTextField txtEllipsoidRadius_a;
private JTextField txtEllipsoidRadius_b;
private JTextField txtEllipsoidRadius_c;
(2)public static final String ELLIPSOID = "Ellipsoid";
(3)cmbGeoForm.addItem(ELLIPSOID);
(4)else if(selection.equals(ELLIPSOID) ){
String a_radius = txtEllipsoidRadius_a.getText();
String b_radius = txtEllipsoidRadius_b.getText();
String c_radius = txtEllipsoidRadius_c.getText();
double a = Double.valueOf(a_radius);
double b =Double.valueOf(b_radius);
double c = Double.valueOf(c_radius);
form = new Ellipsoid(a, b, c);
}
(5)else if(selection.equals(ELLIPSOID) )
displayNewGUI( getTypePanel(ELLIPSOID));
(6)else if(type.equals(ELLIPSOID)){
JLabel lblRadius_a = new JLabel("Input Radius a");
JLabel lblRadius_b = new JLabel("Input Radius b");
JLabel lblRadius_c = new JLabel("Input Radius c");
txtEllipsoidRadius_a = new JTextField(8);
txtEllipsoidRadius_b = new JTextField(8);
txtEllipsoidRadius_c = new JTextField(8);
GridBagLayout gridbag = new GridBagLayout();
typePanel.setLayout(gridbag);
GridBagConstraints gbc = new GridBagConstraints();
typePanel.add(lblRadius_a);
typePanel.add(lblRadius_b);
typePanel.add(lblRadius_c);
typePanel.add(txtEllipsoidRadius_a);
typePanel.add(txtEllipsoidRadius_b);
typePanel.add(txtEllipsoidRadius_c);
typePanel.add(lblMeasure);
typePanel.add(cmbMeasure);
gbc.insets.top = 5;
gbc.insets.bottom = 5;
gbc.insets.left = 1;
gbc.insets.right = 8;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 0;
gbc.gridy = 0;
gridbag.setConstraints(lblRadius_a, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gridbag.setConstraints(txtEllipsoidRadius_a, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gridbag.setConstraints(lblRadius_b, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gridbag.setConstraints(txtEllipsoidRadius_b, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gridbag.setConstraints(lblRadius_c, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gridbag.setConstraints(txtEllipsoidRadius_c, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gridbag.setConstraints(lblMeasure, gbc);
gbc.gridx = 1;
gbc.gridy = 3;
gridbag.setConstraints(cmbMeasure, gbc);
}
【试验结果】
【试验小结】
经过这次试验我们大约了解了桥接模式,经过关联“抽象层次类”和“具体层次类”这一桥梁,将表示两个维度层类(数据结构)粘贴在一起,形成更大数据结构,而这种改变又不会对现有类产生影响,这种思绪终极想法是将软件设计抽象部分和实现部分分离,使它们全部能够独立改变。
访问者模式——计算机部件销售软件
【试验内容】
在例4.5设计中添加一个类SoundBox。该类实现接口ComputerParts,而且其它计算机部件类结构类似。
【添加代码】
1、添加SoundBox类:
public class SoundBox implements ComputerParts{
public static final String NAME = "SoundBox";
private final double PRICE = 127.00;
public static final String FEATURES = "SoundBox. X3K";
public String getName(){
return NAME;
}
public double getPrice(){
return PRICE;
}
public String getDescription(){
return FEATURES;
}
public void accept(Visitor v){
System.out.println("SoundBox has been visited.");
v.visitSoundBox (this);
}
}
2、 添加GUI:
(1)String[] compParts={ "Case","Motherboard","Microprocessor","Memory",
"DriveController","VideoCard","Fan","PowerSupply",
"HardDiskDrive","CDDrive","DVDDevice","Monitor",
"Keyboard","Mouse","SoundBox","Assembly","WholePC"};
(2)for(int k=11; k<17; k++)
add(0, k, cParts[k]);
(3)else if (source == cParts[14])
states[14] = state;
else if (source == cParts[15]){
if(state == SELECTED){
cParts[1].setSelected(true);
cParts[8].setSelected(true);
}
else if (state == DESELECTED){
cParts[1].setSelected(false);
cParts[8].setSelected(false);
}
states[15]=state;
}
else if (source == cParts[16]){
if(state == SELECTED){
cParts[0].setSelected(true);
for(int k=11; k<15; k++)
cParts[k].setSelected(true);
}
else if (state == DESELECTED){
cParts[0].setSelected(false);
for(int k=11; k<15; k++)
cParts[k].setSelected(false);
}
states[16]=state;
}
(4)else if ((m==14) && (states[14] == SELECTED)) {
part = new SoundBox();
msg.add("SoundBox");
}
3、添加Visitor:
public abstract void visitSoundBox (SoundBox e);
4、 添加PriceVisitor:
public void visitSoundBox (SoundBox e){
price = e.getPrice();
partsPrices.add(new Double(price));
total += price;
}
5、添加PartsInfoVisitor:
public void visitSoundBox (SoundBox e){
partInfo = e.getDescription();
allOders = allOders + "\n " + partInfo;
}
【试验结果】
【试验小结】
经过此次试验让我们愈加深刻了解了访问者模式,它在处理数据结构较稳定,不过作用于其上操作需要常常改变问题时时很有效,所以能够在不改变该结构体中类基础上定义一个新操作,这个试验就是如此,我们只需要添加新类,然后调用accept(pv)方法就能够。
试验四
策略模式——整数排序
【试验内容】
在例4.13设计中排序策略部分,添加一个类BidirectionaBubbleSort,进行“双向冒泡法排序”,方便扩展该排序系统功效。为了实现扩展功效,需要对应地修改Context类和用户图形界面类StrategyGUI。
【添加代码】
1、 添加BidirBubbleSort类:
public class BidirBubbleSort implements SortAlgorithm
{
public int[] sort(int[] nums,Context ct)
{
ct.startExecution();
int j;
int limit = nums.length;
int st = -1;
while(st < limit)
{
boolean flipped = false;
st++;
limit--;
for(j = st; j < limit; j++)
{
if(nums[j] > nums[j + 1])
{
int T = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = T;
flipped = true;
}
}
if(!flipped)
{
ct.endExecution();
return nums;
}
for(j = limit; --j >= st;)
{
if(nums[j] > nums[j + 1])
{
int T = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = T;
flipped = true;
}
}
展开阅读全文