After reading first chapter from Head First Design Patterns book i figured out few good points which should be noted while designing any system in object oriented way which are below:
– identify the aspect of your application that vary and separate the from what stays the same.
- Take what varies and “encapsulate” it so it won’t affect the rest of your code.
The result? Fewer unintended consequences from code changes and more flexibility in your systems!
– Program to an interface, not an implementation
- We will use an interface to represent each behaviour and each implementation of a behaviour will implement one of those interfaces.
– Class relationships: IS-A, HAS-A, IMPLEMENTS
– Favour composition over inheritance (HAS-A)
- Instead of inheriting it’s behaviour the entity get their behaviour by being composed with the Right behaviour object
- You can change behaviour at runtime as long as the object you are composing with implements the correct behaviour of interface.
- Composition is used in many design patterns.
The Strategy Pattern defines a family of algorithms(behaviours), encapsulates each one and make them interchangeable. Strategy let’s the algorithm(behaviour) change independently from the Client that use it.