Team 302 Software Training
Design Principles
Design Principles
Prerequisite Training:
Design Principles
DRY vs. WET Code
Interface vs. Implementation
Inheritance
Inheritance
Dog
Poodle
Great Dane
Irish Setter
Golden Retriever
Composition
Composition
Original Robot
Passive Grabber
Elevator
Omni Chassis
Final Robot
ActiveGrabber
4Bar
Composition - Changing behavior at runtime
Favor Composition over Inheritance
Example - Inheritance
Example - Composition
Delegation
SOLID Design Techniques
Single Responsibility Principle
A class should do only one thing (AND DO IT WELL!!)
Single Responsibility Principle
Separate classes to:
As opposed to one class that reads the Joystick inputs and sets the Chassis Motor Speeds
Open/Closed Principle
Software should be open for extensions but closed for modifications
Image courtesy of Derick Bailey.
Open/Closed Principle
Example of how we could have done it ….
Liskov Principle
Design by contract, so that a subclass can replace a base class without causing a problem.
Image courtesy of Derick Bailey.
Liskov Principle
Liskov Principle
Interface Segregation
No client should be force to depend on a method it doesn’t use
Image courtesy of Derick Bailey.
Interface Segregation
Imagine an application that calculates the area and perimeter of polygon shapes. The interface IShape has methods CalcArea() and CalcPerimeter(). Later, it is decided that the application draw the shapes as well as calculate these values. Should you just add a Draw() method on the IShape() interface? Will all clients care about it? Will there be other methods for drawing later?
Dependency Inversion
High level modules shouldn’t depend on low level modules. Both should depend on abstractions.
Similar to electrical create plugs and sockets (interfaces) to hook things together.
Image courtesy of Derick Bailey.
Dependency Inversion
Dependency Inversion