• 软件设计与体系结构编程题汇总


    1. 现在需要开发一款游戏软件,请以单例模式来设计其中的 Boss 角色。角色的属性和动作可以任意设计。 要求:该 Boss 类可以在多线程中使用。( 8 分)
      在这里插入图片描述
    Public class Boss{
    	Private static Boss instance; //(2 分)
    	Private Boss(){ }//(2 分)
    	Public static Boss getInstance(){ // ( 2 分)
    		If(instance == null){
    			Synchronized(Boss.Class){ // ( synchronized 关键字, 2 分)
    				If(instance == null)
    					Instance = new Boss();
    			}
    		}
    		return instance;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    或者

    Public class Boss{
    	Private static Boss instance = new Boss();//(4 分)
    	Private Boss(){}// ( 2 分)
    	Public static Boss getInstance(){//2 分
    		Return instance;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. 一个农场公司,专门负责培育各种水果,有葡萄,草莓和苹果,请使用工厂方法,编写工厂类和主程序,并在主程序中来完成草莓生长状态的描述。( 8 分)

      在这里插入图片描述

      Public interface Factory{
      	Fruit build();
      }
      Public class AppleFactory implements Factory{
      	Public Fruit build(){
      		Return new Apple();
      	}
      }
      Public class GrapeFactory implements Factory{
      	Public Fruit build(){
      		Return new Grape();
      	}
      }
      Public class StrawberryFactory implements Factory{
      	Public Fruit build(){
      		Return new Strawberry();
      	}
      }
      Public class MainUI{ 
      	Public static void main(string[] str){
      		Factory fac = new StrawberryFactory();
      		Fruit ft = fac.build();
      		ft.plant();
      		Ft.grow();
      		Ft.harvest();
      	}
      }
      
      • 1
      • 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
    2. 给定如图所示的树形结构,请应用组合模式,在客户端完成数据的展示。具体要求如下:
      绘制组合模式的类图。( 4 分)
      编写简单元素和复杂元素的代码。( 4 分)
      在这里插入图片描述
      在客户端构造出树形数据并输出。( 4 分)
      提示:程序运行后,输出信息应为

      Root
      Leaf A
      Comp B
      Leaf BX
      Leaf BY
      Leaf C
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      1. 类图,类名不限,但必须将抽象的概念,以及Composite和Component之间的关系用正确的连线表示。(4分)
        在这里插入图片描述
        2) 简单元素、复杂元素(4分):
      class Leaf implements Component{
       String name;
       public Leaf(String name){this.name = name; }
       public void display(){
         System.out.println( name);
       }
       public void add(Component c){}
       public void remove(Component c){}
      }  
      class Composite implements Component{
       String name;
       ArrayList<Component> list = new ArrayList<Component>();
       public Composite(String name){
         this.name = name; 
       }
       public void display(){
         System.out.println(name);
         for(int i = 0 ; i < list.size() ;i ++)
         {
           list.get(i).display();
         }  
       }
       public void add(Component c){list.add(c);  }
       public void remove(Component c){list.remove(c);}
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25

      客户端( 4 分):

      public class Test {
       public static void main(String[] args){
         Component root = new Composite("Root");
         root.add(new Leaf("Leaf A"));    
         Component comp = new Composite("Comp B");
         root.add(comp);    
         comp.add(new Leaf("Leaf BX"));
         comp.add(new Leaf("Leaf BY"));    
         root.add(new Leaf("Leaf C"));    
         root.display();    
      	 }
       }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    3. 迭代器模式: 提供一种方法来访问聚合对象,而不用暴露这个对象的内部表示,其别名为游标 (Cursor) 。
      在这里插入图片描述

    CustomerManagerpublic   class   CustomerManager  {
      private  ArrayList<String>  list  =  new  ArrayList<String>();
      public   void  add(String str) {
        list .add(str);  }
      protected  ArrayList<String> getList() {
        return   list ;  }
      public  Itertator getASItertator() {
        return   new  AscIterator( this );  }
      public  Itertator getCharItertator(String s) {
        return   new  CharIterator( this , s);  }}
    Itertatorpublic   abstract   class   Itertator  
    {   public   abstract   boolean  hasNext();
      public   abstract  String next();}
    AscIteratorpublic   class   AscIterator   extends  Itertator {
     ArrayList<String>  list  =  new  ArrayList<String>();
      Iterator   iter ;
      public  AscIterator(CustomerManager cm) {
       ArrayList<String> ls = cm.getList();
        for  (String s : ls) {
          list .add(s);
         Collections. sort ( list );
          iter  =  list .iterator();    }  }
      public   boolean  hasNext() {
        return   iter .hasNext();  }
      public  String next() {
        return  (String)  iter .next();  }}
    CharIteratorpublic   class   CharIterator   extends  Itertator {
     ArrayList<String>  list  =  new  ArrayList<String>();
      Iterator   iter ;
      public  CharIterator(CustomerManager cm, String str) {
       ArrayList<String> ls = cm.getList();
        for  (String s : ls) {
          if  (s.startsWith(str)) {
            list .add(s);      }
          iter  =  list .iterator();    }  }
      public   boolean  hasNext() {
        return   iter .hasNext();}
      public  String next() {
        return  (String)  iter .next();  }}
    主类
    public   class  Client {
      public   static   void  main(String[] args) {
       CustomerManager cm= new  CustomerManager();
       cm.add( "zhangsan" );
       cm.add( "lisi" );
       cm.add( "brown" );
       cm.add( "alien" );
       Itertator iter;
       iter=cm.getCharItertator( "a" );
        while (iter.hasNext()){
         String str=iter.next();
         System. out .println(str);    }  }}
    
    • 1
    • 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
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
  • 相关阅读:
    【微信测试号实战——02】编写你独有的微信消息模板
    智慧城市项目建设介绍
    基于 ANFIS 的非线性回归(Matlab代码实现)
    ubuntu中使用QT、C++使用redis、hiredis记录
    快速入门Servlet
    数据结构系列学习(四) - 单向循环链表(Circular Linked List)
    mysql中的函数
    水果店销售技巧有哪些,水果店销售说话技巧有哪些
    【设计模式】八、桥接模式
    vue制作页面水印
  • 原文地址:https://blog.csdn.net/L6666688888/article/details/128074467