Initial code:
public void updateState(final int state)
{
preSet();
itsState = state;
postSet();
}
public void setTitle(final String title)
{
preSet();
itsTitle = title;
postSet();
}
After my Command Pattern Implementation:
public void updateState(final int state)
{
CallableManager.doInTransaction(new Callable<Void>()
{
@Override
public Void execute()
{
itsHiddenNodes = hiddenNodes;
return null;
}
});
}
public void setTitle(final String title)
{
CallableManager.doInTransaction(new Callable<Void>()
{
@Override
public Void execute()
{
itsTitle = title;
return null;
}
});
}
This interface is created for pass method as parameter.
private interface Callable<T>
{
public T execute();
}
This class is created to manage command pattern.
private class CallableManager
{
public <T> static void doInTransaction(final Callable<T> callable)
{
preSet();
callable.execute();
postSet();
}
}
As you see, implementing command pattern does not look like very effective at least as line of code for this example. For this example, I implement command pattern to escape from repeated code and decrease the line of code. But as a result both of them are not provided for this example. Please give me some advice. How can I use command pattern effectively?
Aucun commentaire:
Enregistrer un commentaire