Reputation: 86757
How can I call a method from a class that is not an object within another class, and has nothing in common with this other class?
In my case:
class GridUI {
com.google.gwt.user.cellview.client.DataGrid grid;
public void refresh() {
dataGrid.redraw();
}
}
class SomeBackendService() {
public foo() {
//have to trigger refresh of a specific grid
}
}
One possibility might be to make the refresh()
and grid
static
. But that's bad design, and I cannot use this approach as I want to use several implementations of GridUI
.
So, how can I refresh a certain gridclass in my app from any service that does not contain this grid as an object??
Upvotes: 0
Views: 150
Reputation: 1785
Just create and fire an Event
for it in your service and make your grid register for that Event
. It's probably best to use an EventBus
.
Using a static Map<String, Grid>
as was suggested in the accepted answer will work but it's not proper. You risk making mistakes and it's not as easy to manage when the number of grids increases.
The EventBus
approach is more work upfront but in the end it's a better approach. You'll be able to reuse the EventBus
throughout your application. It really helps keep your coupling down. You'll also easily be able to get different objects act on the same Event
with little effort.
Upvotes: 4
Reputation: 1851
Sorry for not answering that long time, i was in vacation.
Interfaces are some kind of classes. But they do not implement any method, they have empty method bodies.
Each class, which implements an interface usually MUST implement its methods.
In C# You would Do :
enter code here
interface IMyInterface
{
void PleaseImplementMe();
}
class clMyInterface :IMyInterface
{
void IMyInterface.PleaseImplementMe()
{
MessageBox.Show("One implementation");
}
}
end
Please let me know, whether this is what can help You or not.
Upvotes: -2
Reputation: 29824
Alternatively create a components registry (basically a Map<String,Grid>
), then fetch the grid from SomeBackendService
using its id as key in the registry. (I guess you know which grid you want to refresh, right?)
Be careful with registries though:
Upvotes: 0