What's the most used philosophy of keeping independent concepts separate in OOP? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this questionOr according to your own experience,
what's your favorite trick ?
The #1 principle is encapsulation - at least the colleges have that part right.
Inheritance, polymorphism, cohesion, concision, coupling, dependency, etc... all these things really fall under that one umbrella.
"Encapsulate by practice and expose by need."
The rest pretty much proceeds from there: Limit the number of places that a change forces you to revisit, ideally, every change to behavior would either be the beginning or the end of a chain-reaction (change the test then change the thing it tests) and every change to design would trigger absolutely no consequent changes. This is a reality that is seldom achieved.
"Find what varies and encapsulate it."
Don't use inheritance to give one class the behaviors of another; use delegation in its place. Use inheritance to create abstractions behind which numerous variations live. If you think you have a problem that requires you to degrade design, your problem is probably that, instead, your design needs to improve.
There's probably a good list of OOP code qualities somewhere with good descriptions, so I won't bother to retype it here. Follow those principles, adapt your practices as your situation requires, and you should be pretty good to go.
In my CSC 110 class I was taught Homer Simpson likes PIE (PIE being the big 3 concepts of object oriented programming).
P = polymorphism I = inheritance E = encapsulation
精彩评论