西南科技大学计算机学院 《面向对象系统分析和设计》实验报告
2) 实现代码:
public class Client {
public static void main(String[] args){ IFactory lf = new HpFactory(); Laptop tp = lf.createLaptop(); tp.show();
lf = new AcerFactory(); tp = lf.createLaptop(); tp.show();
lf = new LenovoFactory(); tp = lf.createLaptop(); tp.show();
lf = new DellFactory(); tp = lf.createLaptop(); tp.show(); }
}
public interface IFactory {
public Laptop createLaptop(); }
public class AcerFactory implements IFactory { public Laptop createLaptop(){ return new AcerLaptop(); } }
public class DellFactory implements IFactory {
9
西南科技大学计算机学院 《面向对象系统分析和设计》实验报告 public Laptop createLaptop(){ return new DellLaptop(); } }
public class HpFactory implements IFactory { public Laptop createLaptop(){ return new HpLaptop(); } }
public class LenovoFactory implements IFactory { public Laptop createLaptop(){ return new LenovoLaptop(); } }
public abstract class Laptop { public void show(){}; }
public class AcerLaptop extends Laptop { public void show(){
System.out.println(\); } }
public class DellLaptop extends Laptop { public void show(){
System.out.println(\); } }
public class HpLaptop extends Laptop { public void show(){
System.out.println(\); } }
public class LenovoLaptop extends Laptop { public void show(){
System.out.println(\);
10
西南科技大学计算机学院 《面向对象系统分析和设计》实验报告 } }
3) 实现结果:
6. 某软件公司欲开发一套界面皮肤库,可以对桌面软件进行界面美化。不同的皮肤将提供视觉效果不同的按钮、文本框、组合框等界面元素,其结构如下图所示:
浅绿色按钮Spring风格绿色边框文本框绿色边框组合框皮肤库浅蓝色按钮Summer风格蓝色边框文本框蓝色边框组合框 该皮肤库需要具备良好的灵活性和可扩展性,用户可以自由选择不同的皮肤,开发人员可以在不修改既有代码的基础上增加新的皮肤。试使用抽象工厂模式设计该皮肤库,绘制类图并编程模拟实现。
1) 类图
11
西南科技大学计算机学院 《面向对象系统分析和设计》实验报告
2) 实现代码:
public class Client {
public static void main(String[] args) {
SpringSkinFactory skinFactory = new SpringSkinFactory(); skinFactory.createButton().action(); skinFactory.createTextbox().action(); skinFactory.createCombobox().action(); }
}
public interface SkinFactory {
public AbstractButton createButton(); public AbstractTextbox createTextbox(); public AbstractCombobox createCombobox();
}
public class SpringSkinFactory implements SkinFactory { public AbstractButton createButton(){ System.out.println(\生成 green button\); return new GreenButton(); }
public AbstractTextbox createTextbox(){ System.out.println(\生成 green textbox\); return new GreenTextbox(); }
public AbstractCombobox createCombobox(){ System.out.println(\生成 green combobox\); return new GreenCombobox(); }
}
public class SummerSkinFactory implements SkinFactory { public AbstractButton createButton(){
System.out.println(\生成 blue button\); return new BlueButton();
12
相关推荐: