Good patterns for loose coupling in Java?
Posted
by Eye of Hell
on Stack Overflow
See other posts from Stack Overflow
or by Eye of Hell
Published on 2010-03-22T14:33:51Z
Indexed on
2010/03/22
14:41 UTC
Read the original article
Hit count: 388
java
Hello.
I'm new to java, and while reading documentation so far i can't find any good ways for programming with loose coupling between objects. For majority of languages i know (C++, C#, python, javascript) i can manage objects as having 'signals' (notification about something happens/something needed) and 'slots' (method that can be connected to signal and process notification/do some work). In all mentioned languages 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
method of Object2
will be called. Such loose coupling technique is often referred as 'delegates', 'signal-slot' or 'inversion of control'. Compared to interface pattern, technique mentioned don't need to group signals into some interfaces. Any object's methods can be connected to any delegate as long as signatures match ( C++Qt even extends this by allowing only partial signature match ). So i don't need to write additional interface code for each methods / groups of methods, provide default implementation for interface methods not used etc.
And i can't see anything like this in Java :(. Maybe i'm looking a wrong way?
© Stack Overflow or respective owner