2

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?

6
  • Patterns are not about reducing lines of code. Why are you trying to use this pattern here? Commented Jan 14, 2019 at 21:05
  • I know but I expect at least decrease the repeat of codes. Commented Jan 14, 2019 at 21:10
  • I'd suggest you review this primer. I think it might answer your question. Commented Jan 14, 2019 at 21:39
  • @Sha and that is happening. Now the logic to doInTransaction is in a single place, if you need to change it you'll have to change it in a single place. Commented Jan 14, 2019 at 22:33
  • Having a pattern without knowing what you want to achieve with it makes it hard to come up with a proper implementation. What is the idea behind updateState and setTitle ? Commented Jan 14, 2019 at 22:40

1 Answer 1

4

A lambda can reduce the code duplication considerably. Also, Runnable seems like a more appropriate interface than Callable, since you are not returning a value.

public class MainJava {
    private int state;
    private String title;

    public static void main(String... args) {
        MainJava mj = new MainJava();
        mj.setState(42);
        mj.setTitle("My Title");
    }

    public void setState(int state) {
        doInTransaction(() -> this.state = state);
    }
    public void setTitle(String title) {
        doInTransaction(() -> this.title = title);
    }

    private void doInTransaction(Runnable runnable) {
        preSet();
        runnable.run();
        postSet();
    }

    private void preSet() {
        System.out.println("preset");
    }
    private void postSet() {
        System.out.println("post-set");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.