【问题标题】:How to increase String input for every time component method is called每次调用组件方法时如何增加字符串输入
【发布时间】:2023-02-07 04:42:04
【问题描述】:

我刚开始在大学学习编码(主要是 Java)大约 6 个月,现在我们只介绍了复合设计模式的部分(包括组件、叶和复合类)。

我的课程问题要求我使用下面的组件接口制作一个简单的导演程序,我可以创建/添加/删除文件/目录。

public interface Component {
    public String getName();
    public int getSize();
    public int getCount();
    public String display(String prefix);
    public Component search(String name);
}

下面是我写的用作 Leaf 的 File 类

public class File implements Component { 
    private String name;
    private int size;
    public File(String name, int size){
        this.name = name;
        this.size = size;
    }
    
    @Override
    public String getName() {
        return this.name;
    }
    
    @Override
    public int getSize() {
        return this.size;
    }
    
    @Override
    public int getCount() {
        return 1;
    }
    
    @Override
    public String display(String prefix) {
        return this.name + " " + "(" + this.size + ")" + System.lineSeparator();
    }
    
    @Override
    public Component search(String name) {
        if(this.name.equals(name)){
            return this;
        }
        else {
            return null;
        }
    }

下面是组件类 - 目录类。我将 * 行放在有问题的方法显示上方。

import java.util.ArrayList;

public class Directory implements Component {
    static int count = 0;
    private String name;
    //Array List to store the leafs
    public ArrayList<Component> children;

    public Directory(String name){
        this.name = name;
        this.children = new ArrayList<>();
    }

    //Method to add leaf to Directory
    public void add(Component component){
        children.add(component);
    }

    //Method to remove leaf from Directory
    public void remove(Component component){
        children.remove(component);
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public int getSize() {
        int size = 0;
        for(Component component : children) {
            size += component.getSize();
        }
        return size;
    }

    @Override
    public int getCount() {
        int count = 0;
        for(Component component : children) {
            count += component.getCount();
        }
        return count;
    }

//***********************************
    @Override
    public String display(String prefix) {
        String totalString = name + ": (count=" + getCount() + ", size=" + getSize() + ")" + System.lineSeparator();

        for (Component component : children) {
            totalString = totalString + prefix + component.display(prefix + prefix);
        }
        return totalString;
    }

//*************************************
    @Override
    public Component search(String name) {
        for(Component component: children) {
            if (component.search(name) != null) {
                return component;
            }
        }
        return null;
    }
}

我的目标是,当我应用显示方法时,每次组件有孩子时,显示方法中的前缀应该增加 1。为了澄清,下面是我的测试方法

public class Test3 {

    public static void main(String[] args) {
        File holiday = new File("family-holiday",201);
        File wallpaper = new File("wallpaper", 421);
        Directory pictures = new Directory("pictures");
        Directory personal = new Directory("personal");
        Directory misc = new Directory("misc");
        Directory dog = new Directory("dog");
        dog.add(wallpaper);
        personal.add(holiday);
        personal.add(misc);
        pictures.add(personal);
        misc.add(dog);

        System.out.print(pictures.display("+"));
    }
}

结果应该是

pictures: (count=2, size=622)
\+personal: (count=2, size=622)
\++family-holiday (201)
\++misc: (count=1, size=421)
\+++dog: (count=1, size=421)
\++++wallpaper (421)

但是,由于组件有子组件时的显示方法,它会调用 (prefix + prefix) 直到第三个目录/文件,前缀每次都会加倍而不是增加一个。下面是我得到的结果

pictures: (count=2, size=622)
\+personal: (count=2, size=622)
\++family-holiday (201)
\++misc: (count=1, size=421)
\++++dog: (count=1, size=421)
\++++++++wallpaper (421)

我尝试了多种方法,我可以在下面想到

  • 在文件显示中添加前缀:如果目录中有目录,这显然不起作用

  • 使用静态参数来计算被调用方法的次数,因为前缀的使用次数取决于文件/目录所在的级别,这不适用于方法使用次数的完整计数.

  • 在许多不同的地方添加前缀 .. :) 仍然效果不佳。我可以想象它必须是我在循环再次调用显示时将前缀传递给的方式,但我无法弄清楚它是什么......

如果您能建议或指导我可以做什么或我应该阅读什么来帮助解决这个问题,那将非常有帮助。如果有类似的主题,我会提前道歉,因为我目前对关键字的了解找不到它们,如果你能指导我,我将非常感谢

【问题讨论】:

  • 你可以明白为什么,对吧?当您有 prefix='++' 时,您将传递 prefix+prefix 或 ++++。将“深度”作为整数传递以描述您的深度,然后通过多次重复“+”来构建前缀会是一个更好的计划。然后你只需传递depth+1。
  • 嗨蒂姆,感谢您的反馈。事实上,我明白前缀 + 前缀不是最好的方法..只是我能想到的唯一方法!由于本练习的目的是实现给定的接口(按问题修复),我不认为我可以更改指定的显示方法以具有额外的输入?如果可能的话,那肯定有助于解决问题。如果您有任何其他想法,请告诉我!非常感谢您的回答。
  • 我告诉过你怎么做。不要传递前缀字符串,传递当前深度。让您的“显示”功能接受int depth。现在,它可以通过多次重复“+”来构造前缀字符串。
  • 您使用的是哪个 Java 版本?

标签: java string composite


【解决方案1】:

正如我所说,只需将深度作为整数传递并在显示函数中构造前缀。

    @Override
    public String display(int depth) {
        String prefix = '+'.repeat(depth);
        String totalString = name + ": (count=" + getCount() + ", size=" + getSize() + ")" + System.lineSeparator();

        for (Component component : children) {
            totalString = totalString + prefix + component.display(depth + 1);
        }
        return totalString;
    }

...

        System.out.print(pictures.display(1));

【讨论】:

    猜你喜欢
    • 2021-01-03
    • 1970-01-01
    • 2020-07-10
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    相关资源
    最近更新 更多