I am working on a paint type application in android. I got toolbar on left contains tools like undoCut, redoCut, box, frame, cutting, coloring, next, previous etc. I wanted to know what is the best way(design pattern) to implement such behavior. How to to perform action against each tool. Enable/Disable tools on some condition.enter image description here Like in the attached image there is a left toolbar in which some controls get enabled and disable based on some condition. Like photoshop do it. Till now what i am doing is stored the toolbar info in array and start writing if statements like
if (modelsView.getchildCount() > 0 && modelsPanelSelected) tools.hcutBtn.setEnabled(true);. But this idea is not good design pattern i guess because there would be more than 40 if statements if i start catering each scenario. So i want to know what could be best possible option do i avail to achieve this ? I am not asking to give me code but a good possible design pattern to cater this scenario.


As per the requirement given, it seems, some of behavioural patterns can be considered.

  1. Oberserver Pattern
  2. Strategy Pattern
  3. State Pattern

Generally, it is also advisable to think if pattern is really needed, else those becomes overkill.

| improve this answer | |
  • ++ State and Observer instantly jumped to my mind as well. You could flush out how and why these may help OP though. – RubberDuck Mar 19 '16 at 18:03

If you have created view sub-class for each tool than it would be easy to maintain the enable/disable condition. Just maintain last selected tool, when you select new tool, unselect previous one(with yes/no kind of setter). Observer and strategy patterns may be overkill but if you wish you can look into them.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.