开发者

Good patterns for loose coupling in Java?

开发者 https://www.devze.com 2022-12-24 18:41 出处:网络
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'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 ment开发者_JAVA技巧ioned 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?


You should look at Observable and Observer class in java to achieve signal sort of behavior. The main idea is to make the observer do some action when there is a change in the observable object Classes are java.util.Observable which you object which has to send the signal needs to extend.

Interface is java.util.Observer which your observer classes should implement to act on the signal


AspectJ might give you the behavior you're looking for. I haven't touched it in several years, but I remember it being powerful as hell. But if you're a Java newbie ... I'm not gonna say "Stay away," because hey, you have to learn sometime. Just be careful. As I recall, AspectJ doesn't merely give you enough rope to hang yourself; it gives you enough to hogtie and hang your entire family first. Including your cats.


You could find bunch of information about modules coupling, as for java I would distinguish next levels of coupling in language level:

  1. Static method dependency – high coupling, user code knows about exact implementation
  2. New instance creation dependency (A creates instance of B and uses it's behavior) - high, B can't be substituted
  3. Reflection dependency – high coupling, A knows about exact implementation of B
  4. Inheritance dependency (class B extends A)– high coupling, any change in parent class affects sub classes
  5. Class instance dependency (class A has reference to instance of class B)– medium, user code tight to particular class hierarchy, it's medium since java doesn’t support multiple inheritance
  6. Interface instance dependency (class A has reference to instance of interface B)- low, since java supports multiple interface implementation.

Easy way to test your implementation on high coupling is to write tests for it.


You can have one object implement a suitable EventListener and have the other object fire such events. See Writing Event Listeners for more.


One possibility is:

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

When you want to signal the slots, loop through the list with all the INotifialbes and invoke the the event method that you define yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消