• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to handle Exception when implementing Strategy Pattern  RSS feed

 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to create a program using strategy pattern to implement SOLID principle. The program goes , the user logged in   via email /  DBLogger (Database logger) / text file and if the calculated tax is negative or incorrect the program throws exception. The exception method occurs via  mimicing Textfile or DBLogger or email. Then how to throw it please show. I am unable to write it .

class ILogger



class TextFile



class Email




I Have to include exception here


Main () class that invokes the Tax Calculator
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think handlin exceptions is any different if you use different patterns?
Don't start interface names with I.
Don't start method names with CapitalLetters.
Don't use the new Date() constructor. Don't use the java.util.Date class at all. Use the new date-time API.
 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie . I want to know the syntax of how to include the syntax in here if the value of rate 'r' goes 0 or negative. Since i implemented strategy pattern for the if else statement, now how to include it here in Tax Calculator class
 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here for this code i have to include calculate class in new Tax Calculator.

 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your if-else-if statement in the exception block nullifies the use of the Strategy pattern, the point of which is to avoid such a construct. Go back and review what the context and motivation is for Strategy.
 
Sucheta Shrivastava
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Junilu . I strategy pattern avoids such usage of if else. hence in new Tax Calculator how to include the exception
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what you mean by "how to include the exception" -- try to describe what you're trying to do in a different way because that phrase doesn't make sense to me.

You have a method called calculateTax() that could throw a RuntimeException if the years parameter value is zero. You have various ways to log an exception, all of which implement the ILogger interface. If you're trying to  adhere to SOLID principles, which part of SOLID in particular do you think is being violated by your code right now? How does using Strategy help you stick to SOLID then?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!