oop - Which SOLID Principles are violated? -


introduction


i work on master thesis inheritance problems , work out indicators show inheritance problem exist.

like following example:

example


public static string getanimalnoise(animal animal) {   if (animal instanceof dog)     return "woof";   if (animal instanceof cat)     return "miau";   return ""; } 

the method returns string "woof" if given animal instance dog , "miau" if cat. empty string because animals make no noises @ all.

so correct solution should use polymorphism getnoise method in animal class.

i have analysed different indicators of inheritance problems , want if of them violates solid principle.

i thought example above violates the:

  1. single responsibility principle (srp)
  2. open/closed principle (ocp)
  3. liskov substitution principle (lsp)
  4. dependency inversion principle (dip)

but i'm not sure whether true all.

i thought:

principle violations


srp violation

because conditional statements @ violates srp, because switch case statement or more 1 if-else statement consider more 1 responsibility.

it exists 2 cases there more 1 reason change method.

ocp violation

because if new animal added new case must added method method not close modifications.

lsp violation

each branch executes different actions dependent of animal sub type. think violates lsp ?! know example of rectangle , square , getarea these example thought fits violation.

dip violation

the conditional statements take dependency means statements dependent on details , not on abstractions violates dip.

question:


so question is, given example, given principles violated , reasoning correct?

srp because conditional statements @ violates srp, because switch case statement or more 1 if-else statement consider more 1 responsibility. exists 2 cases there more 1 reason change method.

i disagree. srp meant interpreted pinch of salt. read uncle bob's article on here - coined principle.

i'll quote important bits:

what defines reason change?

this principle people.

when write software module, want make sure when changes requested, changes can originate single person, or rather, single tightly coupled group of people representing single narrowly defined business function. want isolate modules complexities of organization whole, , design systems such each module responsible (responds to) needs of 1 business function.

[...] think principle, remember reasons change people. people request changes. , don't want confuse people, or yourself, mixing code many different people care different reasons.


ocp because if new animal added new case must added method method not close modifications.

correct. method assumes specific set of implementations, , not able handle new ones without being modified.


lsp each branch executes different actions dependent of animal sub type. think violates lsp ?!

it violates lsp, different reason. if pass in giraffe, unexpected result, empty string. means method not correct subtype of animal.


dip conditional statements take dependency means statements dependent on details , not on abstractions violates dip.

technically true, side-effect of violating other 2 principles above. it's not core of problem.


remember principles not rules, don't strict/literal when interpreting them. pragmatism , understanding why principle needed key.


Comments