I have a if else statement which might grow in the near future. Since, this is already looking messy, I think it would be better if I can apply any design patterns here. I looked into Strategy pattern but I am not sure if that will reduce if else condition here. Any suggestions? Answer This is a classic Replace Condition
Tag: design-patterns
Handling an exception as a method argument
I am looking for a design pattern to handle Exception instances received as method arguments. To put some context into the question, I am using GWT and have various asynchronous handlers that usually come in a form similar to: So, the onFailure method receives a Throwable instance that I need to handle. Now, I have a number of exceptions I
Abstract class extends concrete class
I earlier learned that abstract class can extend concrete class. Though I don’t see the reason for it from JAVA designers, but it is ok. I also learned that abstract class that extends concrete class can make overriden methods abstract. Why? Can you provide with use case where it is useful? I am trying to learn design patterns and I
How to make clear that a parameter of a method serves as a return value?
Is there a way to show that a parameter of a method serves as return value? If I want to return one value it is no problem: public Set addIntegerToSet(final int i) { Set<Integer&…
Java object, changed fields listener, design-pattern
There is a class: Then we update some fields How can I know which fields of MyClass were tried to be changed (invoked, in the example above field1 and field2)? What is the best solution for that? Maybe some design-pattern? One option is to create HashSet of changed fields for this object as an additional property and update it inside
What are worker threads, and what is their role in the reactor pattern?
I’m trying to understand the Reactor pattern (concurrent), but in many examples they are talking about ‘worker threads’. What are worker threads? In what way do they differ from ‘normal’ threads? And what is their role in the reactor pattern? Answer The Reactor pattern is used with worker threads to overcome a common scenario in applications: You need to do
What are good light-weight design patterns for avoid nulls in Java?
Obviously one of the greatest banes of Java programming is nulls and null-pointer exception. What design patterns are there that don’t add too much to your code but reduce the problem of sand null-pointer exceptions? Answer Null Object pattern. Look at Optional class from google-guava library. Their wiki has an article on using and avoiding nulls.
The builder pattern and a large number of mandatory parameters
To date I use the following implementation of the builder pattern (as opposed to the implementation described here): This works well for most situations I’ve encountered where I need to build up a complex object with a variety of required/mandatory and optional parameters. However, I’ve been struggling lately to understand how the pattern is of any benefit when all your
Head First Design Patterns – Combined Pattern
I’m reading the chapter 12 on the Combined Pattern in Head First Design Patterns. On page 541,the sample DJView,it cant’t run correctly in my computer.when i press the ‘start’, the program only sounds once rather than circularly . i’m not sure whether because of the environment of my system. but if i add one line code in the method meta
Android MVVM Design Pattern Examples
I currently do a lot of WPF development and have started creating some basic Android apps. When creating WPF apps I often use MVVM, normally using Prism, and would like to know if there are any examples of MVVM for the Android platform? Answer I am the developer of Android-Binding. Like @Brentley said, it’s a very new project but I