Create another object

In java, I can have an a object with a set of properties like getColor. Then I could make a second object b (possibly of the same class) that behaves similarly to object a in the sense that it does the same thing as a, unless one of its values ​​is changed (and not changed) )

a = new Foo()
b = new FooWhichRefersToAnotherFoo(a)

a.getColor() # returns blue
b.getColor() # returns blue
a.setColor(red)
b.getColor() # returns red
b.setColor(pink)
b.getColor() # returns pink
a.getColor() # returns red (unchanged)
b.deleteChangeToColor()
b.getColor() # returns red as it is now pointing back to a

I would suggest that it would be some kind of tree hierarchy, as if I were pointing b to b pointing to a, the object would process the chain until the first specified value or the default value of the original non-screen object.

, java? , , , , , , , .

, , , , ,

class Foo {
  Color color = new Color("red");
  Color getColor() { color }
}

class FooInstance extends Foo {
  Foo parent = null;

  FooInstance(Foo parent) {
    this.parent = parent;
  }

  Color getColor() {
    if (color == null) return parent.getColor();
    else return color;
  }
}

, , , javabeans - . , , , , , ?

+3
4

, , . , a/b/c , , . , . - , , . , , :

Color getColor() {
    if (color == null) return parent.getColor();
    else return color;
}

. , . Foo FooInstance :

class Foo {
    Foo parent = null;
    Color color;

    Foo(Foo parent) {
        this.parent = parent;
    }

    Color getColor() {
        //If there is no parent we have to return color regardless.
        if (parent == null) {
            return color;
        }

        // If there is a parent we can choose which to return.
        return color == null ? parent.getColor() : color;
    }

    void setColor(Color color) {
        this.color = color;
    }
}

- FooInstance, , , .

API, ​​ . .

+1

java.util.Properties, ​​ :

package stackoverflow;
import java.util.Properties;

public class Main {
    static Properties propsBase;
    static Properties propsOverlay;
    static Properties propsOverlayOverlay;

    public static void main(String[] args) {
        propsBase = new Properties();
        propsOverlay = new Properties(propsBase);
        propsOverlayOverlay = new Properties(propsOverlay);

        propsBase.setProperty("key1", "value1");
        propsBase.setProperty("key2", "value2");
        debugAllProps();

        propsOverlay.setProperty("key1", "overlayValue1");
        debugAllProps();

        propsOverlayOverlay.setProperty("key1", "overlayOverlayValue1");
        debugAllProps();

        propsOverlayOverlay.remove("key1");
        debugAllProps();

        propsOverlay.remove("key1");
        debugAllProps();
    }

    private static void debugAllProps() {
        printProps("propsBase", propsBase);
        printProps("propsOverlay", propsOverlay);
        printProps("propsOverlayOverlay", propsOverlayOverlay);
        System.out.println("------------------------------------------------");
    }

    private static void printProps(String desc, Properties props) {
        System.out.printf("%-25s", desc + " sees:");
        for (String key : props.stringPropertyNames()) {
            System.out.printf(" %s=%s", key, props.getProperty(key));
        }
        System.out.println();
    }
}


:

propsBase sees:           key2=value2 key1=value1
propsOverlay sees:        key2=value2 key1=value1
propsOverlayOverlay sees: key2=value2 key1=value1
-------------------------------------------------
propsBase sees:           key2=value2 key1=value1
propsOverlay sees:        key2=value2 key1=overlayValue1
propsOverlayOverlay sees: key2=value2 key1=overlayValue1
-------------------------------------------------
propsBase sees:           key2=value2 key1=value1
propsOverlay sees:        key2=value2 key1=overlayValue1
propsOverlayOverlay sees: key2=value2 key1=overlayOverlayValue1
-------------------------------------------------
propsBase sees:           key2=value2 key1=value1
propsOverlay sees:        key2=value2 key1=overlayValue1
propsOverlayOverlay sees: key2=value2 key1=overlayValue1
-------------------------------------------------
propsBase sees:           key2=value2 key1=value1
propsOverlay sees:        key2=value2 key1=value1
propsOverlayOverlay sees: key2=value2 key1=value1

:

  • java.util.Properties, String.

  • , , java.util.Properties, , Hashtable, .

- , , , java.util.Properties, , Properties . java.util.Properties, src.zip, JDK.

  • ,
  • , - , "". , a HashMap.
  • java.util,
+1

, , CSS, , . , . IMO, Foo FooInstance - , , , HashMap . , . - :

public class Foo {
    private Foo parent;
    private HashMap<string, Object> propertyValues = new HashMap<string, Object)>();

    public Foo() {
    }

    public Foo(Foo parent) {
        this.parent = parent;
    }

    protected Object getProperty(string propertyName) {
        if (properties.containsKey(propertyName))
            return properties.get(propertyName);
        else if (parent != null)
            return parent.getProperty(propertyName);
        else
            return null;
    }

    protected void setProperty(string propertyName, value) {
        properties.put(propertyName, value);
    }

    public Color getColor() {
        return (Color)getProperty("color");
    }

    public void setColor(Color color) {
        setProperty("color", color);
    }
}
0

I would just do it using the Decorator template. These two classes do not have to be the same, but they will need to implement a common interface and take the other in the constructor, and then pass the getter to the side in the wrapped class (s) until one is overridden in the package class. In this example, I used only one class for simplicity, but could be flexible to work with any Colorful. Starting from the test:

import junit.framework.TestCase;

import java.awt.*;

public class CascadingTest extends TestCase {

    public void testCascade() throws Exception {
        Colorful a = new Foo();
        a.setColor(Color.RED);
        assertEquals(Color.RED, a.getColor());

        Colorful b = new Foo(a);
        assertEquals(Color.RED, b.getColor());

        b.setColor(Color.PINK);
        assertEquals(Color.PINK, b.getColor());

        b.setColor(null);
        assertEquals(Color.RED, b.getColor());
    }
}

import java.awt.*;

public interface Colorful {
    Color getColor();
    void setColor(Color color);
}

import java.awt.*;

public class Foo implements Colorful {

    private Color color;
    private Colorful parent;

    public Foo() {}

    public Foo(Colorful parent) {
        this.parent = parent;
    }

    public Color getColor() {
        if (parent != null && this.color == null) {
            return parent.getColor();
        } else {
            return color;
        }
    }

    public void setColor(Color color) {
        this.color = color;
    }
}
0
source

Source: https://habr.com/ru/post/1774189/


All Articles