티스토리 뷰

Inheritance


- Inheritance is a core principle of OO programming.

- But we tend to overuse it.

- Often results in design and code that is inflexible.



Problems with Our Design


- We have code duplicated across classes.

- Hard to gain knowledge of all the ducks.

- Changes can affect other ducks.

- Runtime behavior changes are difficult.


What About Using Interfaces?


- Allow different classes to share similarities.

- Not all classes need to have the same behavior.

- Let us try moving duck behaviors into interfaces.


Implementing Ducks with Interfaces




Also Problematic


- Solves part of the problem, but...

- Absolutely destroys code reuse.

- Becomes a maintenance nightmare.

- Does not allow for runtime changes in behaviors other than flying or quacking.


Design Principle #1


- Identify the aspects of your code that vary and separate them from what stays the same.


"Encapsulate What Varies"


- If some aspect of code is changing,

That's a sign you should pull it out and separate it.


- By separating out the parts of your code that vary

You can extend or alter them without affecting the rest of your code.


- This principle is fundamental to almost every design pattern.



Design Principle #2


- Program to an interface, not an implementation.








The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.





댓글
댓글쓰기 폼
공지사항
Total
395,134
Today
2
Yesterday
41
«   2018/11   »
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  
글 보관함