NET
NET
Huan
AGENDA
❖ The Single Responsibility Principle
❖ Example
SRP PRICIPLE
SRP Defined
Cohesion
How strongly-related and focused are the various responsibilities of a
module
Coupling
The degree to which each program module relies on each one of the
other modules
SRP PRICIPLE
Cohesion & Coupling
SRP PRICIPLE
Cohesion & Coupling
SRP PRICIPLE
Responsibility are axes of change
1. Credit Order
Retail
• Checkout
• Payment- Credit Card
2. Cash Order
• Checkout Operations
Order 1. Checkout
2. Payment
3. Notify Customer
4. Reserve
Process
Inventory
Online 1. Online Order
• Checkout
• Payment - Credit Card
• Notify Customer
• Reserve Inventory
SRP PRICIPLE
Example
SRP PRICIPLE
Example
SRP PRICIPLE
Example - Problems
Service 1 Service 2
Object
Applications UI
The OCP Principle
❖ OCP Defined
❖ Demo
Open to extension
New behavior can be added in the future.
Closed to Modification
Changes to source or binary code are not required.
OCP PRICIPLE
OCP Defined
⮚ A module will be said to be open if it is still available for extension.
For example, it should be possible to add fields to the data structures it
contains, or new elements to the set of functions it performs.
Cart
Operations
Price Rule
Operations
1. Is Match – By Order Item (_Type)
2. Calculate Price – By Order Item
OCP PRICIPLE
Solutions- Composition / Strategy Pattern Demo
OCP PRICIPLE
Solutions- Composition / Strategy Pattern Demo
OCP PRICIPLE
Signal
Object M1
Attributes
Object M Operations
1. RunType
Attributes Type A Object M3
Operations
1. RunType Attributes
+ Type A Operations
+ Type B 1. RunType
…….. Object M2 Type N
+ Type N Attributes
Operations
1. RunType
Type B
OCP PRICIPLE
When do we apply OCP?
Experience Tells You
If you know from your own experience in the problem domain that a
particular class of change is likely to recur, you can apply OCP up front in
your design
❖ LSP Defined
❖ Demo
⮚ Problems
⮚ Solution
❖ LSP Smells
LSP PRICIPLE
LSP Defined
Rectangle Square
Attributes Attributes
1. Width 1. Height
2. Height Operations
Operations 1. Areas
1. Areas (Height*Height)
(Width*Height)
LSP PRICIPLE
LSP Violation Smells
The ISP Principle
❖ Demo
⮚ Problems
⮚ Solutions
❖ ISP Smells
❖ ISP Tips
ISP Principle
ISP Defined
ISP Defined
ISP Principle
Demo
ISP Principle
Demo
ISP Principle
Demo
ISP Principle
Problems Demo
Solutions Demo
ISP Principle
Solutions
ISP Principle
IPS Smells
Unimplemented interface methods
If you find “fat” interfaces are problematic but you do not own
them
• Create a smaller interface with just what you need
• Implement this interface using an Adapter that implements the full
interface
ISP Principle
ISP Tips
Keep interfaces small, cohesive, and focused
• Don’t force client code to depend on things it doesn’t need
❖ Demo
⮚ Problems
⮚ Solutions
❖ Dependency Injection
❖ DIP Smells
❖ ISP Tips
DI Principle
DI Defined
Result
• Tight coupling
• No way to change implementation details (OCP violation)
• Difficult to test
DI Principle
Dependency Injection
Dependency Injection is a technique that is used to allow
calling code to inject the dependencies a class needs
when it is instantiated.
Pros
• Classes self-document what they need to perform their work
• Works well with or without a container
• Classes are always in a valid state once constructed
Cons
• Constructors can have many parameters/dependencies (design
smell)
• Some features (e.g. Serialization) may require a default constructor
• Some methods in the class may not require things other methods
require (design smell)
DI Principle
Property Injection
Dependencies are passed in via a property
• Also known as “setter injection”
Pros
• Dependency can be changed at any time during object lifetime
• Very flexible
Cons
• Objects may be in an invalid state between construction and setting of
dependencies via setters
• Less intuitive
DI Principle
Parameter Injection
Dependencies are passed in via a method parameter
Pros
• Most granular
• Very flexible
• Requires no change to rest of class
Cons
• Breaks method signature
• Can result in many parameters (design smell)
Q&A