Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You appear to have a class with a lot of attributes and no easy way of setting them all. You have now run in to a problem where you need a class with an additional attribute but you have to deal with that mess of a base class.</p> <p>I suggest that, instead of creating a subclass and casting, you create a wrapper class around the ugly one:</p> <pre><code>public class BigDumbClass { // A lot of attributes // No Constructor // No init method } public class Wrapper { private BigDumbClass base; private boolean selected; public Wrapper(BigDumbClass base) { this.base = base; this.selected = false; } //getters and setters } </code></pre> <p>Now when you have to create that new list you can wrap everything in the old list</p> <pre><code>List&lt;BigDumbClass&gt; oldList = someData(); List&lt;Wrapper&gt; wraps = aNewList(); for (BigDumbClass bigDumb : oldList) { Wrapper wrap = new Wrapper(bigDumb); if (someCondition()) { wrap.setSelected(true); } wraps.add(wrap); } </code></pre> <p>Ideally, BigDumbClass would implement an interface that Wrapper could also implement, allowing the wrapper to defer all of the calls to the instance it has wrapped. </p> <pre><code>public class BigDumbClass implements SharedInterface { // All the stuff outlined above } public class Wrapper implements SharedInterface { // All the stuff outlined above // Methods defined in SharedInterface public void doSomething() { base.doSomething(); } } </code></pre> <p>Otherwise, you can provide a getter to the instance and access it directly.</p> <pre><code>BigDumbClass base = wrapper.getBase(); base.doSomething(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload