设计模式课后习题Word文档格式.docx
《设计模式课后习题Word文档格式.docx》由会员分享,可在线阅读,更多相关《设计模式课后习题Word文档格式.docx(28页珍藏版)》请在冰豆网上搜索。
publicabstractclassPersonCreate{
protectedGamePersonperson=newGamePerson();
publicabstractvoidcreateFace();
publicabstractvoidcreateGender();
publicabstractvoidcreateCloth();
publicGamePersongetPerson(){
returnperson;
具体建造者类:
publicclassPersonTypelextendsPersonCreate{
publicvoidcreateFace(){
person.setFace(”瓜子脸"
);
publicvoidcreateGender(){person.setGender("
美女"
publicvoidcreateCloth(){
person.setCloth("
洛丽塔"
具体建造类:
publicclassPersonType2extendsPersonCreate{
publicvoidcreateFace(){person.setFace("
国字脸"
publicvoidcreateGender(){person.setGender(”帅哥"
西装革履"
指挥者类:
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();
KM穿着
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{
intel"
1G内存"
publicclassUser{
privateFactoryf;
publicvoidbuy(Factoryf){
this.f=f;
publicComputercon(){
f.installCpu();
f.installStorage();
returnf.getComputer();
publicstaticvoidmain(String[]args){
Factoryf=newLaptop();
Useru=newUser();
u.buy(f);
Computerc=u.con();
System.out.println(c.getCpu()+"
"
+c.getStorage());
单例模式
懒汉式模式:
publicclassSingletonWindowextendsJinternalFrame{
privatestaticSingletonWindowinstanee=null;
privateSingletonWindow(){
super("
内部窗口"
true,true,true);
System.out.println(”创建了一个内部窗体”);
publicstaticSingletonWindowgetlnstance(){
if(instanee==null){
instanee=newSingletonWindow();
else{
System.out.println(”已经创建了一个内部窗体!
”);
returninstanee;
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);
publicvoidactionPerformed(ActionEventargO){
itf=SingletonWindow.getInstanee();
itf.setSize(200,200);
itf.setVisible(true);
dtp.add(itf);
});
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(){
机器人