-3

The following situation:

I have three classes A, B and C. A is some sort of parent which means A has a reference to the instance of B as well as C. B does not have a reference to the instance of C and C has no reference to the instance of B.

B has a Object (in this case a TextArea) which needs to be accessed by C. C has some text (as a result from some other methods) which should be added to the Object (TextArea) from B.

B should implement a public functional interface. My boss said I should be able to achieve my goal that way. Unfortunately I cannot ask him about hit because he went on vacation. He just left a note.

EDIT: Found the solution to my problem. The following works just fine.

Class A:

package de.test;

public class A {
    static B b = new B();
    static C c = new C();

    private static void doSomething() {
        c.setChangeString(b.changeString());
    }

    public static void main(String... args) {
        doSomething();
        c.changeText();
        System.out.println(b.string);

    }

}

Class B:

package de.test;

public class B {

    @FunctionalInterface
    public interface ChangeString {
        void setString(String string);
    }

    String string = "nein";

    public ChangeString changeString() {
        return (string) -> this.string = string;
    }

}

Class C:

package de.test;

import de.test.B.ChangeString;

public class C {
    public ChangeString changeString;

    public void changeText() {
        this.changeString.setString("YES");
    }

    public void setChangeString(ChangeString changeString) {
        this.changeString = changeString;
    }
}
PeterPan
  • 684
  • 1
  • 10
  • 27
  • 2
    To do what? Why try to implement a functional interface here? For what purpose? Show what you want to use it for. – Carcigenicate Feb 22 '18 at 16:22
  • To be able to write into that TextArea. C doesn't know the B and B has the TextArea. My boss wants it that way. – PeterPan Feb 22 '18 at 16:23
  • 1
    I don't think a functional interface would be necessary here unless you needed to use lambdas for some reason. Why not just create a normal interface called `Writable` (or something), give it a method called `setText`, then have B implement it? C has to know *something* about B, or they can't interact. As long as C knows that B implements a certain interface, that should suffice. – Carcigenicate Feb 22 '18 at 16:25
  • You'll need to give us more to go on, the above is far too vague. What do you mean by "C doesn't know about B"? Doesn't know the type? Doesn't have a reference to the instance? Code is worth 1024 words. – T.J. Crowder Feb 22 '18 at 16:25
  • I edited my question. – PeterPan Feb 22 '18 at 16:30
  • Why is _interface_ qualified with _functional_? – Sotirios Delimanolis Feb 22 '18 at 16:41
  • Your question is still not well constructed. Lay out the problem before the sharing the code. Beyond the ["How to ask"](https://stackoverflow.com/help/how-to-ask) advice, you also need to start by stating the question in a way that focuses on what gap remains _after_ your research. Describe [step like these](https://hackernoon.com/how-to-debug-any-problem-ac6f8a867fae) that you have done thus far, for code, conditions, and errors. State 'obvious' context that you already know, [so that people understand what you have tried](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) – New Alexandria Feb 22 '18 at 17:16
  • @Carcigenicate I added a solution to my question. That way it works. – PeterPan Feb 22 '18 at 17:21
  • @PeterPan That strikes me as an abuse of Functional Interfaces. I can't see, in this example, any gain over just using a vanilla Interface. – Carcigenicate Feb 22 '18 at 17:26
  • @Carcigenicate Isn't every interface with just one method automatically a functional interface? – PeterPan Feb 22 '18 at 17:27
  • @PeterPan Technically yes, but usually you only specifically label an Interface as Functional if you intend to use it for lambda creation. "Abuse" was too strong of a word in retrospect. I think specifying "functional" confuses the issue, since really you just needed to create a plain interface. – Carcigenicate Feb 22 '18 at 17:30
  • @Carcigenicate Okay. Is there a name/word if interfaces are used like this? – PeterPan Feb 22 '18 at 17:31
  • @PeterPan Just an "interface"? And I should clarify since I was in a hurry in my last comment. Not all interfaces that only specify one method are necessarily Functional Interfaces. Unless you need the compiler checks outlined [here](https://stackoverflow.com/questions/36881826/what-is-use-of-functional-interface-in-java-8/36882003), having the annotation is probably more of a Red Herring than anything to people reading your code. – Carcigenicate Feb 22 '18 at 17:42

1 Answers1

1

I would suggest that you need an intermediary Observer that broadcasts changes to interested listeners.

Components can't know about each other, but the Observer pattern makes it possible to keep them decoupled.

Somebody has to know about the relationship, but this pattern allows you to centralize it outside the objects themselves.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Thank you for your answer. I'll look into that. – PeterPan Feb 22 '18 at 16:32
  • Do you have any idea on the functional interface thing though? – PeterPan Feb 22 '18 at 16:32
  • @PeterPan It should be asked: do you know what a "functional interface" is? Unless there's some part of the use-case that you haven't mentioned, I can't see how they'd be useful, or even applicable here. – Carcigenicate Feb 22 '18 at 16:57
  • I don't know if you mean the functional interfaces built into the JDK 8 java.util.function package. I would recommend that you stop jumping to a solution and think about your problem more carefully. You don't want to fall prey to Small Boy With A Hammer syndrome. Sounds like your pointy haired boss already has. – duffymo Feb 22 '18 at 16:59
  • @duffymo I added a solution to my question. That way it works. – PeterPan Feb 22 '18 at 17:21
  • "Solution"? Hardly. Good luck. – duffymo Feb 22 '18 at 18:20