Which design pattern I should use? [closed]-Collection of common programming errors
I personally will go for decorator instead of strategy. IMHO, strategy are used more for configuration / logic flow, not situational-handling (in this case, connection). This is opinion-based though.
Moreover, when doing something like this:
if(connectionAvailable()){
// insert 1
} else {
// insert 2
}
Can has race condition between connectionAvailable and insertion. It may be little though. This is my suggested design:
interface IFormSubmitter {
public void submitForm(FormData data);
}
public class SqlLiteFormSubmitter : IFormSubmitter{
public void submitForm(FormData data){
//insert into sqlite
}
}
public class DatabaseFormSubmitter : IFormSubmitter{
public class DatabaseFormSubmitter(IFormSubmitter reservedSubmitter){
// property assignment
}
public void submitForm(FormData data){
try{
// insert into database
}
catch(SQLException ex){
if( isConnectionError(ex) ){
reservedSubmitter.submitForm(data);
}
else{
throw e;
}
}
}
}
The hard part is, you need to define whether your exception thrown is based on connection or not, based from caught exception object.