How Terracotta knows that object is changed inside one JVM to propagate this change to another JVM? I.e. how can I code similar behavior?
I want to be able to watch changes in instances of some classes, without changing them at all.
It uses instrumentation. It's explained (in high level) in the terracotta wiki
Shouldn't a observer pattern help. If you implement your class as observable and have an oberver , the observer will be notified when ever the observable class goes through some change.
Related
I started learning Kotlin few weeks ago and I need help understanding the basics.
What is a listener? For example, what is RatingBar's listener? How do I find a listener of other widgets?
What are the parameters? Again, what is RatingBar's parameters? How do I find the parameters of other widgets?
What is View.____?
If you're writing a component that handles events of some kind, you'll probably want a way to inform other components about those events. There are lots of ways to do this (in a software design sense), and one of those is the idea of a listener.
The basic idea is that components can register themselves as listeners, and when a relevant thing happens, they get a callback. The component generating the events doesn't need to know what those listeners are, it just needs to hold a reference to one (or more) and call some callback function when the event happens. The key thing here is that the component doesn't have a listener, you have to provide them. You're hooking components up.
So a typical way to do this is for a component to implement a callback interface, which basically says "I have a function called this with these parameters" and the event component can just call that function with the details when an event happens. You could also create an object that implements that interface, or in Kotlin's case you can just pass a function in that matches the signature of the callback function - basically providing a block of code that says "when the event happens, do this stuff in response". That's typically how you define behaviour, like setting an on-click listener - you write a block of code to execute when the user clicks a thing, and when the event happens, that code gets run.
I hope that makes sense generally - if any of it's confusing (since you're new) I'd recommend reading the basic trails in the Java Tutorials (which Kotlin is based on) to get a handle on interfaces and the like.
As for the other questions... if you're asking what the parameters are when you construct a RatingBar or whatever, the best place to look is the documentation (although you generally won't be constructing Views in code anyway, just adding them to a layout XML file) - Android Studio should pop up some hints too (you can hit ctrl+q for documentation on the thing your cursor is currently over, or ctrl+p for parameter hints).
If you're asking what a View is, it's the base class for Android's UI components, it's really anything that's able to draw itself and be included in a layout. It has a lot of methods because it has a lot of functionality built in to handle all these UI responsibilities.
If you're asking what View.something is, that's a static method or field defined on the View class - if you don't know what that means, read the Java tutorial link I included! You'll typically be referencing things like View.VISIBLE which is a set value you provide to a View's setVisibility method - it knows what those values mean and what to do with them
What is the need for Action classes to be serializable? When and how does it happen, if at all.
As far as I can tell, it doesn't need to be Serializable, and it was a mistake to make ActionSupport implement that interface.
Here is the best reasoning I have found on the subject (taken from here):
It's very common in web frameworks to use Serializable objects for a
couple or reasons, such as being able to preserve state across a
server restart and for shipping objects around in a cluster.
With that said, (IMHO) I believe it was a design mistake to have
ActionSupport implement Serializable. I don't believe that either of
the above really apply to Action objects since they are short-lived.
The choice of making Actions Serializable should have been left to
the developer and not "forced" by the framework.
This question might not be relevant anymore but I thought this might help.
From Sun developer network:
Object serialization is the process of saving an object's state to a sequence
of bytes, as well as the process of rebuilding those bytes into a live object
at some future time.
So why you might want to serialize your objects? That's when you need to persist their state
so you can use them later or in another JVM. The JVM might be on the same machine or over the network on another machine. I think that's the same case for ActionSupport class. If you extend ActionSupport you'll get the chance to serialize your action and send it over the network to be used in another JVM.
i don't know why it must be so. but action classes must extends ActionSupport. and according to http://struts.apache.org/2.0.6/struts2-core/apidocs/com/opensymphony/xwork2/ActionSupport.html , ActionSupport implements Serializable. so the answer to the when question, it happens all the time :p
I am designing a game with several levels. I have a CCLayer defined as a singleton (called MasterScene) where I handle the pause page, transition page, player's score banner,... all the things common to all levels.
So in each level, when the user pushes the pause button, a call is made to the singleton to display the CClayer corresponding to the pause page. My problem is that I want to know who called the singleton (which level) t. Is there a way of doing that ?
Thanks
Without knowing more about your application's architecture, I'd suggest three possible approaches:
Pass the level number (or pointer to the level object, or whatever) as a parameter to the singleton's methods.
Have the object keep track of which level is the current one, so that it already knows. (Obviously, this assumes that only the current level can be calling these methods. But I'm not sure why multiple levels would have available pause buttons.)
Don't make this object a singleton at all. Create an instance for each level. Is there really application-global state that this object needs to track? If you're using the MasterScene to encapsulate the behavior, but not global state, then have multiple instances of that class around doesn't really hurt anything (or consume much in devices resources).
There's no general way to locate the source of a message the way you are asking for.
There are however alternative architectures for your app which might solve this problem and I encourage you to consider them. What you're describing sounds like a mess of interdependent classes. All of your levels are aware of and use this MasterScene singleton and now you're trying to make the singleton aware of every level as well? Every piece of your applications shouldn't need to be aware of every other.
You can pass it in as an argument, e.g.
#implementation Level30
-(void) pause;
{
[[MasterScene getSingleton] pauseWithLevel:self];
}
#end
I've been trying to implement a simple component-based game object architecture using Objective-C, much along the lines of the article 'Evolve Your Hierarchy' by Mick West. To this end, I've successfully used a some ideas as outlined in the article 'Objective-C Message Forwarding' by Mike Ash, that is to say using the -(id)forwardingTargetForSelector: method.
The basic setup is I have a container GameObject class, that contains three instances of component classes as instance variables: GCPositioning, GCRigidBody, and GCRendering. The -(id)forwardingTargetForSelector: method returns whichever component will respond to the relevant selector, determined using the -(BOOL)respondsToSelector: method.
All this, in a way, works like a charm: I can call a method on the GameObject instance of which the implementation is found in one of the components, and it works. Of course, the problem is that the compiler gives 'may not respond to ...' warnings for each call. Now, my question is, how do I avoid this? And specifically regarding the fact that the point is that each instance of GameObject will have a different set of components? Maybe a way to register methods with the container objects, on a object per object basis? Such as, can I create some kind of -(void)registerMethodWithGameObject: method, and how would I do that?
Now, it may or may not be obvious that I'm fairly new to Cocoa and Objective-C, and just horsing around, basically, and this whole thing may be very alien here. Of course, though I would very much like to know of a solution to my specific issue, anyone who would care to explain a more elegant way of doing this would additionally be very welcome.
Much appreciated, -Bastiaan
I don't think that sending the container object all of its components' messages is what Mick West was suggesting--that doesn't help to remove the idea of a "monolithic game entity object".
The eventual goal is to have the components communicate directly with one another, with no container object at all. Until then, the container object acts as glue between old code that expects a single object for each game entity and the new component-to-component system.
That is, you shouldn't need to use message forwarding at all in the final product, so ignoring the warnings, or declaring variables as id for now to quiet them, isn't all that ugly. (The plan as laid out by the article is to eventually remove the very code that is causing your warnings!)
A simple way to have those warnings disappear would be to declare the instance variables of type id
That way the compiler assumes you know what you're doing regarding the type of the object and that the object will respond to whatever messages you send to it, or if it doesn't you don't care.
Override your GameObject's -respondsToSelector: method. Your implementation should in turn send a respondsToSelector: message to each of its instances, and return YES if any one of them returns YES.
You can use type of id - or you could invoke the methods using performSelector methods, or create an NSInvocation if the arguments are complex. This is all just a way of getting around compiler warnings, however. If your objects respond to several methods, then possibly declaring a protocol might help, although the same caveat applies.
Another option if I understand the problem correctly is to implement a protocol. This is link an interface in java and variables can be declared like this:
id anObjectRef
That way the compiler understands that the object referred to by anObjectRef conforms to the protocol.
There are also methods that can tell you if an particular object conforms to a specific protocol before you cast or assign it.
I am looking to write a plugin controller in Cocoa that loads bundles, and exposes a specific set of methods for the plugins to call.
My question is this: is it possible to know (any) info about the object that called a method in the controller. When an instantiated plugin calls a method in my plugin controller, I would like to know which of the plugin instances called the method, without having to rely on the plugin sending a pointer to itself as a parameter (I could always validate the pointer they send, but I want to keep the API methods as simple as possible).
There may be no perfect solution (and there are simple workarounds), but it's always good to learn some new tricks if possible (or the reasons why it's impossible).
Thanks in advance.
It's not possible without walking the stack, and possibly not even then. There's not even a guarantee that a given message was sent from within a method — and even if it was, it may not be the method that you think of as being the "caller." For example, assuming your plugins have access to Cocoa, your controller methods could be called by an NSTimer.
In general, this is not practical. The normal way to accomplish this is to have objects pass themselves around. If you're trying to do this for security reasons, you'll want a much more robust solution anyway, because Cocoa's object model was not designed with that in mind. It's way too easy for objects to lie about who and what they are.
Well, you could throw an exception, catch it and examine its stacktrace.
Assuming that Objective-C supports exceptions, of course.
Sending a reference to the calling object is how this is usually done. As an alternative, you could have your host code provide a proxy object for plugins to talk to. As each plugin is loaded, create a new proxy object for each to talk to.