How to reduce duplication in code - If statements vs separate class
Let's say you have a website that lets you build an account with up to three different account owners. Each entry page for the owner is a separate aspx page. The first person has very different business rules than the second and third owners. The first will have more fields as well and different ones will be required for them but not for the others. I am using the MVP (Model View Presenter) pattern.
I can either do
A. Make three separate Views and have them all use one Presenter th开发者_StackOverflow中文版at has special IF statements or Switch statements to see if you are dealing with a certain owner and if so to act a certain way.
OR
B. Make three separate Views and have them all use their own Presenter which will just execute the way it is intended for that owner without any IF statements needed.
I feel conflicted because A seems the right thing to do but could get messy with all of the IF statements everywhere and people could delete or modify them without realizing the repercussions. Although B seems like far too much duplication of code and just looks ugly. Is there a point at which you say these classes are too different to be shared and there's too many IF statements for exceptions?
I would agree with the comment above. Put the common code for your presenter in a base class and have the three specific child presenters inherit from it. This follows one of the basic principals when designing a system: "Identify the aspects of your application that vary and separate them from what stays the same."
Good luck.
Don't A.
Do what Serguei suggested or make a common logic class with the duplicated code you are talking about.
精彩评论