Need Direction/Advice
I want to understand something that I haven't quite been able to yet. I have skeleton code below showing my base object class and a holder class for a map of those objects.
class MyObjectMap{
private Map objectsByName;
public MyObjectMap(){
objectsByName =new HashMap();
}
publicvoid addObject(MyObject obj)throws ObjectExistsException{
String key =new String(obj.getName());
if(objectsByName.containsKey(key)){
thrownew ObjectExistsException(key);
}else{
objectsByName.put(key,obj);
}
}
publicvoid updateObject(String name,double value)throws ObjectNotExistException{
if(objectsByName.containsKey(name)){
MyObject objPointer = (MyObject)objectsByName.get(name);
objPointer.setPrice(value);
}elsethrownew ObjectNotExistException(name);
}
}
class MyObject{
private String name;
privatedouble price;
public MyObject(String name,double price){
this.name=name;
this.price=price;
}
public String getName(){
return this.name;
}
publicvoid setPrice(double price){
this.price=price;
//Notify all Objects that need to update this here
}
}
//I didn't include my exceptions, b/c they aren't relevant.
This is what I want to know: In my MyObject class, I have a setPrice(double price) method where it updates the price for that object. Let's say that I have a socket that recieves updates for object price's. In that socket thread it it sends MyObjectMap.updateObject(String name, double value) which updates the price for that particular object. If there are several other objects in my program that need to reflect the new updated price for that object, what is the best method to do this? If I need to use listeners, what is the best approach. I have never really used listeners before other than swing listeners. I suppose this would have to be a custom listener? What steps need to be taken to implement this?
Thanks
[3732 byte] By [
litesouta] at [2007-11-26 18:36:58]

if you only had one one price for the type you could make the price static. i.e. only one instance for all instances and they would all know its new value.
I'm not understanding what you are saying...
> if you only had one one price for the type you could
> make the price static. i.e. only one instance for all
> instances and they would all know its new value.
I think you are saying make setPrice static so it can be called directly such that I can do MyObject.setPrice(...) without creating the object first? Why would I do that if I have 10 MyObjects all with different names and prices?
Edit: If I don't make the setPrice method static and just make price static, how would I update the price if it is object specific? And how would that update other objects with that price? Let's say I have a JTable and a JTextBox displaying the current price for a specific MyObject. How do I udpate those components with the new updated price?
Or maybe I just didn't understand your suggestion
Wouldn't I have to register some sort of custom listener to that specific MyObject?
are you saying that one objects price effects another? so that the price of apples depends on the price or oranges? if so. in your apples get price method build into the logic an apples get price method call so that it checks each time (dynamically) when calculating the price of apples.
Like this?
public double getApplesPrice()
{
applesPrice = getOrangePrice() + 0.50;
return applesPrice;
}
No, not at all. All MyObjects are different. For my question, what those objects are doesn't matter.
Let's say here are MyObects(name, price):
Apples, 0.50
Motorcycle, 1500
pen, .02
The socket listens for updates such that if all of a sudden the socket thread gets an update for motorcycle, it updates the price for motorcycle.
Let's say I have other objects (I'll use a component again, i.e JTextBox) that reflects the current price in real time. When I creat the textbox, I tell it which MyObject name to "listen" to for price updates. So, for argument sake (b/c I don't know) I register a listener for the MyObject with the name "motorcycle" so that when the price is updated for that MyObject it updates any other objects "listening" to that particluar price update.
and would the user fire an event or would the price change another way? you could take a look at using threads to check the text in the textbox every second or so and if there is a change update the other value in the map or wherever.
if the user fires the event say by pressing enter then just call the setprice method with the value of the texfield.
The user wouldn't fire the event. The user actually has no control over it. The socket thread that recieves the update, updates the appropriate MyObject's price. When the price is updated, that is what fires the event. I have to believe that there is some sort of solution that can handle this fairly easy, I just can't seem to grasp it.
i.e. here is a user incorporated example:
No matter what, the socket is going to recieve updates on objects and their price.
Now the user chooses to open a JTextBox and wants the price of the "Motorcycle" object to continually be updated on there.
The user then opens 10 more JTextBox and all of them to be set to listen for "Motorcycle" price updates.
Now you have 11 JTextBoxes "registered" to "listen" for motorcycle price updates as they come in.
When the motorcycle object's price gets updated from the socket thread, it then in turn, updates any other object(in this example, a JTextBox) with the updated price.
Take a look at the Observer pattern in Java. It can be used to update all relevant textfields at the same time. http://java.sun.com/j2se/1.4.2/docs/api/java/util/Observer.html http://java.sun.com/j2se/1.4.2/docs/api/java/util/Observable.html
Yes, I am familiar with the Observer pattern, but is it the best solution? Isn't the Observer/Observable really just an extension of a Listener?
Well it's usually the best solution for updating multiple views which is what I think your trying to do.
First off, I appreciate your time with this.
I hope you don't mind me talking this through with you, but if I may brainstorm out loud for a bit...
My base object, MyObject, will extend Observable.
In that class, I will have the method addObserver(Object o)
In the setPrice method, I add setChanged() followed by notifyObservers()
When a user opens a new component such as a JTextBox for a specific Object (ie motorcycle), the class that component is in will implement Observer and that component will.... do what?
How do you tell that specific component to register as an Observer.
Like a listener... jTextBox.addCustomListener(this)
Can you do... jTextBox.addObserver(new Observable(MyObjectMap.getMyObject("Motorcycle"));
[EDIT] Okay that last part made no sense
I guess after I initialize the component I can do the following:
MyObject myObject = MyObjectMap.getMyObject("Motorcycle");
myObject.addObserver(jTextBox);
Does this sound right?
Message was edited by:
litesout
just make the price field static not the method i.e private static int price = 0;then price is shared between all instances of the class, the return method doesn`t need to be static.
> just make the price field static not the method i.e
>
>private static int price = 0;
> then price is shared between all instances of the
> class, the return method doesn`t need to be static.
That's a bad advice. He doesn't want to have the same price on all products.
Kaj
> Let's say that I have a socket that recieves updates for object prices.
Socket? Who does raw socket programming? With RMI, EJBs, web services, and god knows what else out there, why do you feel like you have to go to this low level?
If you insist on going this route, you'll have to write your own remote Observer/Observable. I can see how you might create a firestorm of network traffic.
It seems to me that you've done a poor job of abstracting what you're really trying to do. Why should the object and the map be separated on the network? shouldn't all this be a single unit of work in one address space?
i'd bet a great deal of money that you're overcomplicating what is a simple problem.
%
feels like your object map is nothing more than an in-memory database. all you're doing is changing the state of a model object and asking that it be reflected in the in-memory database.%
> Socket? Who does raw socket programming? I do I do *waves hand*
kajbja at 2007-7-21 17:25:48 >

> > Socket? Who does raw socket programming? > > I do I do *waves hand*Poor blighter. 8)%
It just doesn't feel like a good abstraction to me. I'd have to hear more about the real problem, but this appears to be too low-level IMO.%
> It just doesn't feel like a good abstraction to me.> I'd have to hear more about the real problem, but> this appears to be too low-level IMO.> > %I'm implementing binary financial protocols so I need to deal with raw sockets.
kajbja at 2007-7-21 17:25:48 >

> I'm implementing binary financial protocols so I need
> to deal with raw sockets.
That's the one justification that makes sense to me - a proprietary protocol. When you use a raw socket you're taking care of all the protocol details, too.
I'd be surprised if the OP's problem couldn't be served by an existing protocol (e.g., RMI, HTTP, etc.)
%
--taken from OP
> Let's say that I have a socket that recieves updates
> for object price's........
duffymo, you're getting all wrapped up on something that doesn't matter.
I don't care about the network or what I am using.This has absolutely nothing with my original question. I should have said that "My data is constantly being updated from some vague network protocol because ultimately that it has nothing to do with what I am trying to learn by using this example".
Observer/Observable worked just fine with what I wanted to do.
> duffymo, you're getting all wrapped up on something
> that doesn't matter.
I decide what matters to me. You don't control the thread of discussion here any more than I do.
> I don't care about the network or what I am using.
And I don't really care about your problem. I'm discussing another topic of interest.
> This has absolutely nothing with my original
> question. I should have said that "My data is
> constantly being updated from some vague network
> protocol because ultimately that it has nothing to
> do with what I am trying to learn by using this
> example".
Fine. I'm no mind reader, and I'm not a paid consultant. I discuss what I find interesting here. If it happens to be your problem, and the words are helpful to you, that's great.
> Observer/Observable worked just fine with what I
> wanted to do.
Excellent.
%