Design patterns that can replace if statements

Our application becomes complex, it has mainly 3 threads and has to be processed based on one of three types. Many of these features overlap.

Thus, at present the code fully contains if-else statements, it is all messed up and not organized. How to make a template so that 3 threads are clearly separated from each other, but use the power of reuse.

Please provide some thoughts, this is an MVC application where we need to produce and consume web services using jaxb technology.

Maybe you can consider the application as a single object as an input signal, for which it is necessary to implement various strategies based on the value of the execution time.

+6
source share
4 answers

After a long time, I find that open source frameworks such as drools are a great alternative to my requirements.

0
source

Your question is very broad and almost impossible to answer without any description or overview of the structure of your application. However, I was in a similar situation, and this is the approach I took:

Replace conditions with polymorphism where possible

it has basically 3 threads and should be processed based on this one of 3 types. Many of these features overlap.

You say that your project has 3 main threads, and most of the code overlaps each other. This sounds to me like a strategy template:

You declare an interface that defines the tasks performed by the thread.

public interface Flow{ public Data getData(); public Error validateData(); public void saveData(); public Error gotoNextStep(); } 

An abstract class is created that provides an implementation that is common to all three threads. (The methods in this abstract class need not be final, but you definitely want to consider it carefully.)

 public abstract class AbstractFlow{ private FlowManager flowManager public AbstractFlow(FlowManager fm){ flowManager = fm; } public final void saveData(){ Data data = getData(); saveDataAsXMl(data); } public final Error gotoNextStep(){ Error error = validateData(); if(error != null){ return error; } saveData(); fm.gotoNextStep(); return null; } } 

Finally, you create 3 specific classes that extend from the abstract class and define a specific implementation for the given stream.

 public class BankDetailsFlow extends AbstractFlow{ public BankDetailsData getData(){ BankDetailsData data = new BankDetailsData(); data.setSwiftCode(/*get swift code somehow*/); return data; } public Error validateData(){ BankDetailsData data = getData(); return validate(data); } public void onFormSubmitted(){ Error error = gotoNextStep(); if(error != null){ handleError(error); } } } 
+7
source

You did not specify what your if-else . Let's say they filter ing depending on some value .

If I understand your question correctly, you want to see the Factory template .

This is a clean approach that is easy to maintain and produces readable code. Adding or removing a filter also simple: just remove the class and remove it from the FilterFactory hashmap.

Create Interface: Filter

  public interface Filter { void Filter(); } 

Create a Factory that returns the correct filter according to your value . Instead of if-else you can now simply use the following:

  Filter filter = FilterFactory.getFilter(value); filter.filter(); 

One common way to write FilterFactory is to use a HashMap inside it.

 public class FilterFactory{ static HashMap<Integer, Filter> filterMap; static{ filterMap = new HashMap<>(); filterMap.put(0,new Filter0()); ... } // this function will change depending on your needs public Filter getFilter(int value){ return filterMap.get(value); } } 

Create your three (in your case) Filters like this: (with meaningful names)

 public class Filter0 implements Filter { public void filter(){ //do something } } 

NOTE. . Since you want to reuse some methods, create the FilterUtility class and make all your filters an extension of this class so that you can use all the functions without overwriting them.

+7
source

Let's take an example, suppose you have a Data model [which has some attributes and getters, setters, optional methods]. In the context of a mobile application, in particular, an Android application, there can be two modes: Off-line or On-line. If the device is connected to a network, data is sent to the network stored in the device’s local database. In procedural form, someone can define two models as OnlineData, OfflineData and write the code as [The code is not exact, its like a pseudo-code]:

 if(Connection.isConnected()){ OnlineData ond=new OnlineData(); ond.save();//save is called which stores data on server using HTTP. } else{ OfflineData ofd=new Onlinedata(); ofd.save();//save is called which stores data in local database } 

A good approach to implementing this is to use the principles of OOPS:

The program for the interface is not running

Let's see how to do it. I am just writing code snippets that will more effectively represent what I mean. The following are snippets:

 public interface Model { long save();//save method //other methods ..... } public class OnlineData extends Model { //attributes public long save(){ //on-line implementation of save method for Data model } //implementation of other methods. } public class OfflineData extends Model { //attributes public long save(){ //off-line implementation of save method for Data model } //implementation of other methods. } public class ObjectFactory{ public static Model getDataObject(){ if(Connection.isConnected()) return new OnlineData(); else return new OfflineData(); } } 

and here is the code your client class should use:

 public class ClientClass{ public void someMethod(){ Model model=ObjectFactory.getDataObject(); model.save();// here polymorphism plays role... } } 

It also follows:

Single Responsibility Principle [SRP]

because On-line and Off-line are two different responsibilities that we can integrate into Single save () using the if-else statement.

+3
source

All Articles