Good patterns for free communication in Java?

I am new to java, and while I am reading the documentation, I cannot find any good ways to program with free communication between objects. For most languages ​​that I know (C ++, C #, python, javascript), I can manage objects as having “signals” (notification of something happening / something is needed) and “slots” (a method that can be connected to a signal and process notification / to do some work). In all the languages ​​mentioned, I can write something like this:

Object1 = new Object1Class(); Object2 = new Object2Class(); Connect( Object1.ItemAdded, Object2.OnItemAdded ); 

Now, if object1 calls / emits ItemAdded , the OnItemAdded Object2 method OnItemAdded Object2 . This loose-call technique is often called "delegates," "signal slot," or "control inversion." Compared to an interface, the technology mentioned does not require signal grouping into some interfaces. Any object methods can be associated with any delegate if the signatures match (C ++ Qt even extends this, allowing only partial signature matching). Therefore, I do not need to write additional interface code for each method / groups of methods, provide a standard implementation for interface methods that are not used, etc.

And I don't see anything like it in Java :( Maybe I was looking for the wrong way?

+4
source share
5 answers

You should look at the Observable and Observer class in java to achieve the type of signal behavior. The main idea is to force the observer to take some action when there is a change in the observed object. The classes are the java.util.Observable that you are trying to transmit, which should send a signal, should expand.

The interface is java.util.Observer, which your observer classes must implement in order to act on the signal

+6
source

AspectJ can give you the behavior you are looking for. I did not touch him after a few years, but I remember that he was powerful as hell. But if you are new to Java ... I'm not going to say "Stay away" because, hey, you need to learn someday. Just be careful. As far as I remember, AspectJ doesn't just give you enough rope to hang yourself; It gives you enough hogtie and hang your whole family in the first place. Including your cats.

+3
source

One of the possibilities:

"Slots" implements some interface, f ex INotifiable . Signaling objects have a list of INotifiable objects and a void Register(INotifiable n) method.

If you want to signal slots, scroll through the list with all INotifialbes and call the event method, which you define yourself.

+1
source

You can have one object that implements a suitable EventListener , and another object - the fire of such events. See Writing event listeners for more information.

+1
source

You can find a ton of information about module relationships, since for java I would distinguish the following levels of coupling at the language level:

  • The static dependence of the method is high connection, the user code knows about the exact implementation.
  • The new instance creation dependency (A creates an instance of B and uses its behavior) is high, B cannot be replaced
  • dependence on reflection - high connection, A knows about the exact implementation of B
  • Inheritance of inheritance (class B extends A) - high relationship, any change in the parent class affects the subclass
  • Distortion of an instance of a class (class A has a reference to an instance of class B) - medium, user code is limited to a certain hierarchy of classes, this is an environment since java does not support multiple inheritance
  • the dependency of an instance of an interface (class A has a reference to an instance of interface B) is low because java supports the implementation of several interfaces.

An easy way to test your implementation in high connection is to write tests for it.

+1
source

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


All Articles