类设计问题[可选实现]

发布于 2024-11-06 06:17:18 字数 262 浏览 2 评论 0原文

我想知道如何设计一个系统,其中我有一个 Super 类和几个 Super 的子类类(比如说 Sub1、Sub2、Sub3),并且我想要一个 Cool 类。现在我想要有两件事:

  • Sub1和Sub2可以是Cool的,Sub3永远不可能是Cool的。
  • 我必须能够有一个列表,其中可以有 Sub1 和 Sub2,如果它们很酷的话。例如,如果我创建了 Sub1 的对象,并且很酷,我可以将其放入列表中,如果不是,则不能放入列表中。

    有什么建议吗?提示?

  • I was wondering how to design a system in which I have a class Super and a couple of classes that are subclasses of Super (let's say Sub1, Sub2, Sub3) and I want a class Cool. Now there are two things I want to have:

  • Sub1 and Sub2 can be Cool's, Sub3 can never be cool.
  • I must be able to have a List in which there can be Sub1's and Sub2's, if they are cool. If for example I make an object of Sub1 and it is cool I can put it in the list, if it's not it cannot be in the list.

    Any suggestions? Hints?

  • 如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(6

    浪荡不羁 2024-11-13 06:17:18

    阿恩的回答有点符合你的要求,但我发现它过于复杂。也许我错过了什么?为什么不只是:

    class Super { }
    
    interface Cool { boolean isCool(); }
    
    class CoolImpl extends Super implements Cool {
        private boolean cool;
        public CoolImpl(boolean cool) { this.cool = cool; }
        public boolean isCool() { return this.cool; }
    }
    
    class Sub1 extends CoolImpl { }
    class Sub2 extends CoolImpl { }    
    class Sub3 extends Super { }
    
    class CoolList extends ArrayList<Cool> {
        public boolean add(Cool cool) {
            if (!cool.isCool()) {
                return false;
            }
            return super.add(cool);
        }
    }
    

    Arne's answer kind of does what you want, but I find it overly complicated. Maybe I'm missing something? Why not just:

    class Super { }
    
    interface Cool { boolean isCool(); }
    
    class CoolImpl extends Super implements Cool {
        private boolean cool;
        public CoolImpl(boolean cool) { this.cool = cool; }
        public boolean isCool() { return this.cool; }
    }
    
    class Sub1 extends CoolImpl { }
    class Sub2 extends CoolImpl { }    
    class Sub3 extends Super { }
    
    class CoolList extends ArrayList<Cool> {
        public boolean add(Cool cool) {
            if (!cool.isCool()) {
                return false;
            }
            return super.add(cool);
        }
    }
    
    嘿咻 2024-11-13 06:17:18

    也许是这样的:

    class Super {}
    
    interface Cool { boolean isCool(); }
    
    class IsCool implements Cool {
        public boolean isCool() { return true; }
    }
    
    class NotCool impolements Cool {
        public boolean isCool() { return false; }
    }
    
    interface CoolSupporter {
        boolean isCool();
        Cool getCool();
    }
    
    class Sub1 extends Super implements CoolSupporter {
        private Cool cool;
        public Sub1() { this(new NotCool()); }
        public Sub1(Cool cool) { this.cool = cool; }
        public boolean isCool() { this.cool.isCool(); }
        public Cool getCool() { return this.cool; }
    }
    
    class Sub2 extends Super implements CoolSupporter {
        private Cool cool;
        public Sub1() { this(new NotCool()); }
        public Sub1(Cool cool) { this.cool = cool; }
        public boolean isCool() { this.cool.isCool(); }
        public Cool getCool() { return this.cool; }
    }
    
    class Sub3 extends Super {}
    
    class CoolList {
        private List<CoolSupporter> list = new ArrayList<CoolSupporter>();
        public void add(CoolSupporter coolSupporter) {
            if (coolSupporter.isCool()) {
                list.add(coolSupporter);
            } else {
                throw new UncoolException();
            }
        }
    }
    

    Maybe something like this:

    class Super {}
    
    interface Cool { boolean isCool(); }
    
    class IsCool implements Cool {
        public boolean isCool() { return true; }
    }
    
    class NotCool impolements Cool {
        public boolean isCool() { return false; }
    }
    
    interface CoolSupporter {
        boolean isCool();
        Cool getCool();
    }
    
    class Sub1 extends Super implements CoolSupporter {
        private Cool cool;
        public Sub1() { this(new NotCool()); }
        public Sub1(Cool cool) { this.cool = cool; }
        public boolean isCool() { this.cool.isCool(); }
        public Cool getCool() { return this.cool; }
    }
    
    class Sub2 extends Super implements CoolSupporter {
        private Cool cool;
        public Sub1() { this(new NotCool()); }
        public Sub1(Cool cool) { this.cool = cool; }
        public boolean isCool() { this.cool.isCool(); }
        public Cool getCool() { return this.cool; }
    }
    
    class Sub3 extends Super {}
    
    class CoolList {
        private List<CoolSupporter> list = new ArrayList<CoolSupporter>();
        public void add(CoolSupporter coolSupporter) {
            if (coolSupporter.isCool()) {
                list.add(coolSupporter);
            } else {
                throw new UncoolException();
            }
        }
    }
    
    静水深流 2024-11-13 06:17:18

    您可以创建一个标记界面,说很酷。
    让 Sub1 和 Sub2 类实现这个接口
    在添加到列表之前检查是否很酷,

    这可能会有所帮助。

    You can create an marker interface say cool.
    Let class Sub1 and Sub2 implements this interface
    and before adding to the list check for instance of cool

    may be this can help.

    与风相奔跑 2024-11-13 06:17:18

    你不能让一个类选择性地属于 Java 中的某个类型。尽管您可以对 Sub1 进行子类化,其中一个子类实现了 Cool 接口,而另一个则没有:

    class Super { }
    
    interface Cool { }
    
    class Sub1 extends Super { }
    class Sub1Cool extends Sub1 implements Cool { }
    
    class Sub2 extends Super { }
    class Sub2Cool extends Sub2 implements Cool { }
    
    class Sub3 extends Super { }
    
    class CoolList extends ArrayList<Super> {
        public boolean add(Super sup) {
            if (!(sup instanceof Cool)) {
                return false;
            }
            return super.add(cool);
        }
    }
    

    您也可以放弃 Cool 概念并使用访问者模式:

    class Super { 
        public boolean addTo(List<Super> coolList) {
            if (canBeAddedToCoolList()) {
                return coolList.add(this);
            }
            return false;
        }
    
        protected boolean canBeAddedToCoolList() {
            return false;
        }
    }
    
    class Sub1 extends Super { 
    
        protected boolean canBeAddedToCoolList() {
            // check logic to allow/disallow addition
        }
    }
    

    You can't have a class optionally belonging to a type in Java. Though you may subclass Sub1, with one subclass implementing an interface Cool and the other not:

    class Super { }
    
    interface Cool { }
    
    class Sub1 extends Super { }
    class Sub1Cool extends Sub1 implements Cool { }
    
    class Sub2 extends Super { }
    class Sub2Cool extends Sub2 implements Cool { }
    
    class Sub3 extends Super { }
    
    class CoolList extends ArrayList<Super> {
        public boolean add(Super sup) {
            if (!(sup instanceof Cool)) {
                return false;
            }
            return super.add(cool);
        }
    }
    

    You might also discard the Cool concept and use a visitor pattern:

    class Super { 
        public boolean addTo(List<Super> coolList) {
            if (canBeAddedToCoolList()) {
                return coolList.add(this);
            }
            return false;
        }
    
        protected boolean canBeAddedToCoolList() {
            return false;
        }
    }
    
    class Sub1 extends Super { 
    
        protected boolean canBeAddedToCoolList() {
            // check logic to allow/disallow addition
        }
    }
    
    黯淡〆 2024-11-13 06:17:18

    IMO,您需要有一个重写的列表(比如 MyList,它重写了 add())。

    在 add() 中,检查要添加的对象是否是 Cool,如果是,则将其添加到列表中。如果不是,那么就优雅地忽略它。

    这有帮助吗?

    IMO, you need to have a overrided List (Say MyList, that overrides add()).

    In add(), Check if the object you are adding is Cool, if it is so, then add it part of the list. If not then just gracefully disregard it.

    Does this help?

    恍梦境° 2024-11-13 06:17:18

    管理此问题的最简单方法是进一步子类化 Sub1(CoolSub1 和 NotCoolSub1)和 Sub2(CoolSub2 和 NotCoolSub2)。

    然后,CoolSub1 和 CoolSub2 可以实现 Cool(Cool 应该是一个接口,而不是一个类)。

    然后,您可以定义

    List<Cool>
    

    哪些接受 Sub1 和 Sub2 的实现,但前提是它们实现了 Cool。

    The simplest way you can manage this is to further subclass Sub1 (CoolSub1 and NotCoolSub1) and Sub2 (CoolSub2 and NotCoolSub2).

    CoolSub1 and CoolSub2 can then implement Cool ( Cool should be an interface and not a class)

    You can then define

    List<Cool>
    

    which will accept implementations of Sub1 and Sub2, but only if they implement Cool.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文