In the first half of 2021, the real questions and answer analysis of software designers in the afternoon (5)

Read the following instructions and Java code, and write the words and sentences that should be filled in (n) in the corresponding column on the answer sheet.
【Explanation】
Cascade menu is a system function organization method often used in window-style software systems. The cascaded menu may contain a menu item (directly corresponding to a certain function), or it may be a submenu. Now the composite design mode is used to implement the cascaded menu, and the class diagram shown in Figure 6-2 is obtained.
insert image description here

import java.util.*;
abstract class MenuComponent{
    
    //构成层叠菜单的元素
    (1) String name;//菜单项或子菜单名称
    public void printName(){
    
    System.out.println(name);}
    public (2);
    public abstract boolean removeMenuElement(MenuComponent element);
    public (3);
}
 
Class Menu extends MenuComponent{
    
    
    public MenuItem(String name){
    
    this.name=name;}
    public boolean addComponent(MenuComponent element){
    
    return false;}//(2)
    public boolean removeComponent(MenuComponent element){
    
    return false;}
    public List<MenuComponent> getElement(){
    
    return null;}//(3)
}
 
Class MenuItem extends MenuComponent{
    
    
    public (4);
    public MenuItem(String name){
    
    
        this.name=name;
        this.elementList = new ArrayList<MenuComponent>();
    }
    public boolean addMenuElement(MenuComponent element){
    
    
        return elementList.add(element);
    }
    public boolean removeMenuElement(MenuComponent element){
    
    
        return elementList.remove(element);
    }
    public List<MenuComponent> getElement(){
    
    return elementList;}
}
Class CompositeTest{
    
    
    public static void main(String[] args){
    
     
        MenuComponent mainMenu = new Menu("Insert");
        MenuComponent subMenu = new Menu("Chart");
        MenuComponent element = new MenuItem("On This Sheet");
        (5);
        subMenu.addMenuElement(element);
        printMenus(mainMenu);
    }
    private static void printMenus(MenuComponent ifile){
    
    
        ifile.printName();
        List<MenuComponent> children = ifile.getElement();
        
        if(children==null) return;
        for(MenuComponent element:children){
    
    
            printMenus(element);
        }
    }
}

Reference answer:

(1)protected
(2)abstract boolean addMenuElement(MemuComponent element)
(3)abstract List<MenuComponent> getElement()
(4)ArrayList<MenuComponent> elementList
(5)mainMenu.addMenuElement(subMenu)

Answer analysis:
This question is a typical combination mode application.
First, according to the name marked as # in the class diagram (+ means public, - means private, # means protected), the modification of the name in (1) should be protected.
Then, according to the corresponding relationship between the abstract class and the implementation class, you can fill in the gaps in (2) and (3). These two missing methods are supplemented according to the following codes. Note that the method name must be modified with abstract, and note that there is no specific way to write the abstract method method body. Among them, fill in abstract boolean addMenuElement(MemuComponent element) in the blank of (2), and fill in abstract List<MenuComponent> getElement() in the blank of (3).
(4) An attribute is missing. According to the following constructor with the same name, you will find that this.name and this.elementList are passed as parameters here. Name can be inherited and used according to the parent class, while elementList needs to be defined, so this The missing parameter at is elementList, and the type is defined according to the assignment type later, that is, fill in ArrayList<MenuComponent> elementList in (4).
(5) empty is the assembly of the application of the combination mode. According to the following, it is known that the mainMenu object needs to be called for printing, and at this time the object is independent and needs to be assembled with other menus. The subMenu is assembled with the element below. Here, the subMenu needs to be Assemble to mainMenu, that is, fill in mainMenu.addMenuElement(subMenu) in (5) blank.

Guess you like

Origin blog.csdn.net/johnWcheung/article/details/127697666