开发者

Java basic application architecture

开发者 https://www.devze.com 2023-02-04 18:58 出处:网络
I\'ve just moved into java (from AS3) and I understand the syntax and all the basics, but I can\'t find any tutorials or books on basic program architecture.

I've just moved into java (from AS3) and I understand the syntax and all the basics, but I can't find any tutorials or books on basic program architecture.

For example,

ObjectA has a property: ObjectB. 
ObjectB has a property: x
ObjectB.x changes at some point. 

How can I have Object A 'catch' or 'hook into' or 'liste开发者_StackOverflow社区n for' the change in ObjectB.x?

In AS3, I would make ObjectB an event Dispatcher and have ObjectA listen for some custom Event.

Or in the Flex Framework I would make a changeWatcher.

How is this done in Java and where can I find these architectual basics?


Check out the JavaBeans specification, it includes guidelines on how to create events and listeners so that they conform to the standard (it was standardized because Sun thought there was going to be some Delphi-like market in GUI widgets which never materialized, but it doesn't hurt to have a standard way of doing things). Here's a tutorial on writing event listeners.


If you really need to, you can create an Event and listen to it.

In your ObjectB.setX() method, you can fire your Event to let ObjectA know the value has been changed.

Here is an article that describes how to do it : http://www.javaworld.com/javaqa/2000-08/01-qa-0804-events.html

But I'm not sure I would do it this way. In my ObjectB.setX(), I would look for an ObjectA parent, and if it exists, use a function defined on ObjectA.


You may want to look at observer pattern


In Java bean world, observer pattern is implemented using property change listener This java tutorial provides a good example. Ignore the Java bean GUI part of it and focus on the code.

0

精彩评论

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

关注公众号