solid principles csharp real world examples
SOLID principles c# real world examples
1. Rigidity
- Changes are difficult and painful.
- Every change requires cascade of changes in dependent modules Scope of change is unpredictable.
- Your manager has a favorite scope multiplier, usually more than 2.
2. Fragility
- Closely related to Rigidity.
- You can never predict the impact of change.
- You never know what will break.
3. Immobility
- The design is hard to reuse the code is so tangled that it is the code is so tangled that it is impossible to reuse anything
- A module could be Telltale sign: a module could be reused but the effort and risk of reused but the effort and risk of separating it from the original separating it from the original environment is too high
4. Viscosity : Viscosity of the
software
- changes or additions are easier to implement by doing the wrong thing
Viscosity of the
environment:
- the development environment is slow and inefficient ,
- high
compile times, long feedback time in testing, laborious integration in a
multi-team project
Basic Principles of
Object Oriented Design
1. Single
Responsibility Principles
2. Open-Closed
Principle
3. Liskov
Substitution Principle
4. Interface
Segregation Principle
5. Dependency
Inversion Principle
1). Single Responsibility Principle
Example of Single Responsibility principle as below
- A class should have one and only one reason to change.
- There should never be more than one reason for a class to change
Example of Single Responsibility principle as below
Problem : Below is the of two responsibility
{
public void
dial(String pno);
public void hangup();
public void
send(char c);
public char
recv();
}
Solution: Separate into two interfaces
interface DataChannel{
public void send(char c);
public char recv();
}
interface Connection
{
public void dial(String phn);
public char hangup();
}
2). Open-Closed Principle
- Modules should be open for extension but closed for modification.
- Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification
- Change a class' behavior using inheritance and composition
3). Liskov Substitution Principle
- Derived classes should be substitutable for their base classes (or interfaces).
- Methods that use references to base classes (or interfaces) have to be able to use methods of the derived classes without knowing about it or knowing the details.
- it means that we must make sure that new derived classes are extending the base classes without changing their behavior
4). Interface Segregation Principle
- High-level modules should not depend on low-level modules. Both should depend on abstractions.
- Abstractions should not depend upon details. Details should depend upon abstractions.
- Clients should not be forced to depend upon interfaces that they do not use
//Bad solutions Example
interface Worker {
void work(); void
eat();
}
//Right Solution
- split into two
interfaces
interface Workable {
public void work();
}
interface Feedable{
public void eat();
}
5). Dependency Inversion Principle
- High level modules should not depend upon low level modules. Both should depend upon abstractions.
- Abstractions should not depend upon details. Details should depend upon abstractions.
// Dependency Inversion Principle - Bad
example
class Worker {
public void work() {
// ....working
}}
class Manager {
Worker worker;
public void setWorker(Worker w)
{
worker = w;
}
public void manage() {
worker.work();
}}
class SuperWorker {
public void work() {
//.... working
much more
}
}
// Dependency Inversion Principle - Good
example
interface IWorker {
public void work();
}
class Worker
implements IWorker{
public void work() {
// ....working
}
}
class
SuperWorker implements IWorker{
public void work() {
//.... working
much more
}
}
class Manager {
IWorker worker;
public void setWorker(IWorker w)
{
worker = w;
}
public void manage() {
worker.work();
}
}
For more details about solid principles you can use below site.
Tag | solid principles java example,solid principles c# interview questions,solid principles c# codeproject,solid principles c# msdn,solid principles examples,solid principles c# examples,open closed principle real world example
Go for Step by Step GitHub code manage post :
Purpose
If you take the SOLID principles to their extremes, you arrive at something that makes Functional Programming look quite attractive.
solid principles csharp real world examples
Reviewed by Vikas Kumar Singh
on
May 02, 2018
Rating:
No comments: