设计模式课后习题.docx
- 文档编号:7470561
- 上传时间:2023-01-24
- 格式:DOCX
- 页数:31
- 大小:20KB
设计模式课后习题.docx
《设计模式课后习题.docx》由会员分享,可在线阅读,更多相关《设计模式课后习题.docx(31页珍藏版)》请在冰豆网上搜索。
设计模式课后习题
建造者模式
课后第一题:
产品类:
publicclassGamePerson{
privateStringface;
privateStringgender;
privateStringcloth;
publicStringgetFace(){
returnface;
}
publicvoidsetFace(Stringface){
this.face=face;
}
publicStringgetGender(){
returngender;
}
publicvoidsetGender(Stringgender){
this.gender=gender;
}
publicStringgetCloth(){
returncloth;
}
publicvoidsetCloth(Stringcloth){
this.cloth=cloth;
}
}
抽象建造类:
publicabstractclassPersonCreate{
protectedGamePersonperson=newGamePerson();
publicabstractvoidcreateFace();
publicabstractvoidcreateGender();
publicabstractvoidcreateCloth();
publicGamePersongetPerson(){
returnperson;
}
}
具体建造者类:
publicclassPersonType1extendsPersonCreate{
publicvoidcreateFace(){
person.setFace("瓜子脸");
}
publicvoidcreateGender(){
person.setGender("美女");
}
publicvoidcreateCloth(){
person.setCloth("洛丽塔");
}
}
具体建造类:
publicclassPersonType2extendsPersonCreate{
publicvoidcreateFace(){
person.setFace("国字脸");
}
publicvoidcreateGender(){
person.setGender("帅哥");
}
publicvoidcreateCloth(){
person.setCloth("西装革履");
}
}
指挥者类:
publicclassGamePlayer{
privatePersonCreatepc;
publicvoidchoseType(PersonCreatepc){
this.pc=pc;
}
publicGamePersoncreate(){
pc.createCloth();
pc.createFace();
pc.createGender();
returnpc.getPerson();
}
}
测试类:
publicclassTest{
publicstaticvoidmain(String[]args){
PersonCreatepc=newPersonType1();
GamePlayergp=newGamePlayer();
gp.choseType(pc);
GamePersonperson=gp.create();
System.out.println("游戏人物特征:
");
System.out.println("长着一张"+person.getFace()+"穿着"+person.getCloth()+"的"+person.getGender());
}
}
课后第二题:
产品类:
publicclassComputer{
privateStringcpu;
privateStringstorage;
publicStringgetCpu(){
returncpu;
}
publicvoidsetCpu(Stringcpu){
this.cpu=cpu;
}
publicStringgetStorage(){
returnstorage;
}
publicvoidsetStorage(Stringstorage){
this.storage=storage;
}
}
抽象建造类:
publicabstractclassFactory{
protectedComputerc=newComputer();
publicabstractvoidinstallCpu();
publicabstractvoidinstallStorage();
publicComputergetComputer(){
returnc;
}
}
具体建造者类:
publicclassDesktopextendsFactory{
publicvoidinstallCpu(){
c.setCpu("AMD");
}
publicvoidinstallStorage(){
c.setStorage("8G内存");
}
}
具体建造类:
publicclassLaptopextendsFactory{
publicvoidinstallCpu(){
c.setCpu("intel");
}
publicvoidinstallStorage(){
c.setStorage("1G内存");
}
}
指挥者类:
publicclassUser{
privateFactoryf;
publicvoidbuy(Factoryf){
this.f=f;
}
publicComputercon(){
f.installCpu();
f.installStorage();
returnf.getComputer();
}
}
测试类:
publicclassTest{
publicstaticvoidmain(String[]args){
Factoryf=newLaptop();
Useru=newUser();
u.buy(f);
Computerc=u.con();
System.out.println(c.getCpu()+""+c.getStorage());
}
}
单例模式
课后第一题:
懒汉式模式:
publicclassSingletonWindowextendsJInternalFrame{
privatestaticSingletonWindowinstance=null;
privateSingletonWindow(){
super("内部窗口",true,true,true);
System.out.println("创建了一个内部窗体");
}
publicstaticSingletonWindowgetInstance(){
if(instance==null){
instance=newSingletonWindow();
}
else{
System.out.println("已经创建了一个内部窗体!
");
}
returninstance;
}
}
测试类:
publicclassMainextendsJFrame{
privatestaticfinallongserialVersionUID=1L;
privateJButtonbtnAdd;
privateJPanelbtnpl;
privateJDesktopPanedtp;
privateJInternalFrameitf;
publicMain(){
this.setSize(newDimension(600,700));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);;
this.setLocationRelativeTo(this);
this.setTitle("实验2");
this.setLayout(null);
this.dtp=newJDesktopPane();
this.dtp.setBounds(newRectangle(0,0,600,600));
this.btnpl=newJPanel();
this.btnpl.setBounds(newRectangle(0,600,600,100));
this.btnAdd=newJButton("添加一个内部窗体");
this.btnAdd.setBounds(newRectangle(10,10,150,30));
this.add(dtp);
this.add(btnpl);
this.btnpl.add(btnAdd);
this.btnAdd.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
itf=SingletonWindow.getInstance();
itf.setSize(200,200);
itf.setVisible(true);
dtp.add(itf);
}
});
}
publicstaticvoidmain(String[]args){
newMain();
}
}
适配器模式
课后第一题
目标抽象类:
publicabstractclassRobot{
publicabstractvoidrun();
publicabstractvoidcry();
}
适配者类:
publicclassDog{
publicvoidrun(){
System.out.println("狗跑");
}
}
publicclassBird{
publicvoidcry(){
System.out.println("鸟叫");
}
}
适配器类:
publicclassRobotAdapterextendsRobot{
privateBirdbird;
privateDogdog;
publicRobotAdapter(Birdbird,Dogdog){
this.bird=bird;
this.dog=dog;
}
publicvoidrun(){
System.out.print("机器人学");
dog.run();
}
publicvoidcry(){
System.out.print("机器人学");
bird.cry();
}
}
测试类:
publicclassTest{
publicstaticvoidmain(String[]args){
Birdbird=newBird();
Dogdog=newDog();
RobotAdapteradapter=newRobotAdapter(bird,dog);
adapter.run();
adapter.cry();
}
}
组合模式
课后习题一
publicabstractclassMyElement{
publicabstractvoideat();
publicabstractvoidadd(MyElementelement);
publicabstractvoidremove(MyElementelement);
}
publicclassAppleextendsMyElement{
publicvoideat(){
System.out.println("吃苹果");
}
publicvoidadd(MyElementelement){
}
publicvoidremove(MyElementelement){
}
}
publicclassBananaextendsMyElement{
publicvoideat(){
System.out.println("吃香蕉");
}
publicvoidadd(MyElementelement){
}
publicvoidremove(MyElementelement){
}
}
publicclassPearextendsMyElement{
publicvoideat(){
System.out.println("吃梨子");
}
publicvoidadd(MyElementelement){
}
publicvoidremove(MyElementelement){
}
}
publicclassPlateextendsMyElement{
privateArrayListlist=newArrayList();
publicvoideat(){
for(Objectobject:
list){
((MyElement)object).eat();
}
}
publicvoidadd(MyElementelement){
list.add(element);
}
publicvoidremove(MyElementelement){
list.remove(element);
}
}
测试类:
publicclassClient{
publicstaticvoidmain(String[]args){
MyElementobj1,obj2,obj3,obj4,obj5;
Plateplate1,plate2,plate3;
obj1=newApple();
obj2=newPear();
obj3=newBanana();
plate1=newPlate();
plate1.add(obj1);
plate1.add(obj2);
plate1.add(obj3);
obj4=newApple();
obj5=newBanana();
plate2=newPlate();
plate3=newPlate();
plate2.add(obj4);
plate2.add(obj5);
plate3.add(plate1);
plate3.add(plate2);
plate3.eat();
}
}
课后习题二
publicabstractclassAbstractFile{
publicabstractvoidadd(AbstractFilefile);
publicabstractvoiddelete(AbstractFilefile);
publicabstractvoidlookThrough();
publicabstractStringgetfileName();
}
publicclassImageFileextendsAbstractFile{
privateStringfileName;
publicImageFile(StringfileName){
this.fileName=fileName;
}
publicvoidadd(AbstractFilefile){
}
publicvoiddelete(AbstractFilefile){
}
publicvoidlookThrough(){
}
publicStringgetfileName(){
returnfileName;
}
}
publicclassTextFileextendsAbstractFile{
privateStringfileName;
publicTextFile(StringfileName){
this.fileName=fileName;
}
publicvoidadd(AbstractFilefile){
}
publicvoiddelete(AbstractFilefile){
}
publicvoidlookThrough(){
}
publicStringgetfileName(){
returnfileName;
}
}
publicclassVedioFileextendsAbstractFile{
privateStringfileName;
publicVedioFile(StringfileName){
this.fileName=fileName;
}
publicvoidadd(AbstractFilefile){
}
publicvoiddelete(AbstractFilefile){
}
publicvoidlookThrough(){
}
publicStringgetfileName(){
returnfileName;
}
}
publicclassFolderextendsAbstractFile{
privateArrayList
privateStringfileName;
publicFolder(StringfileName){
this.fileName=fileName;
}
publicvoidadd(AbstractFilefile){
list.add(file);
}
publicvoiddelete(AbstractFilefile){
list.remove(file);
}
publicvoidlookThrough(){
System.out.println("正在浏览"+fileName+"文件夹,有以下文件");
Iteratori=list.iterator();
if(!
(i.hasNext())){
System.out.println("暂无任何文件");
}
for(AbstractFileabstractFile:
list){
System.out.println(abstractFile.getfileName());
}
}
publicStringgetfileName(){
returnfileName;
}
}
测试类:
publicclassMain{
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
AbstractFileimg1=newImageFile("美女.jpg");
AbstractFilevedio=newVedioFile("xxx.avi");
AbstractFiletxt=newTextFile("凡人修仙传.txt");
AbstractFilefolder=newFolder("娱乐");
folder.add(img1);
folder.add(txt);
folder.add(vedio);
System.out.println("---------------------");
folder.lookThrough();
System.out.println("---------------------");
folder.delete(txt);
folder.delete(vedio);
folder.delete(img1);
System.out.println("---------------------");
folder.lookThrough();
}
}
装饰模式
课后习题一
抽象构建类:
publicinterfaceTransform{
publicvoidmove();
}
具体构建类:
publicclassCarimplementsTransform{
publicCar(){
System.out.println("变形金刚是一辆车");
}
publicvoidmove(){
System.out.println("在陆地上移动");
}
}
抽象装饰类:
publicclassChangerimplementsTransform{
privateTransformtransform;
publicChanger(Transformtransform){
this.transform=transform;
}
publicvoidmove(){
transform.move();
}
}
具体装饰类:
public
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 设计 模式 课后 习题
![提示](https://static.bdocx.com/images/bang_tan.gif)