SlideShare una empresa de Scribd logo
1 de 42
Design Principles


                    By
                    Kartheek Nagasuri & Amit Grewal
Design

• What is the meaning of the Design?


                      Design is
                    about “How”

• What is the Difference if compared to
  Analysis?
                       Analysis is
                     about “What”
Design

  Why do we need (good) design?


            To Deliver Faster


                                To Deal with
                                 Complexity
         To Manage Change
Design
• How do we know a design is bad?
                              “W*F? “

      I can’t Fix this code
          “W*F? “                         “W*F? “

                                                    I fixed the problem
                       “W*F? “                          but it broke at
                                             “W*F? “ other places..
         “W*F? “
                         “W*F? “

                                        “W*F? “
Design

• Lets talk about criteria of bad design 
• Are there any Symptoms of bad design?
                           Immobility
         Rigidity




               Fragility    Viscosity
Rigidity

• The Impact of the change is Unpredictable
  – Every change causes a cascade of changes in
    dependant modules.
  – A nice 2 days work become endless “Marathon”
  – Costs become unpredictable.
Immobility

• Its almost impossible to reuse interesting
  parts of the software
  – The useful modules have somany depandancies
  – The cost of rewriting is less compared to the risk
    faced to separate those parts
Fragility

• The Software tends to break in many places
  on every change.
  – The breakage occurs in areas with no conceptual
    relationship.
  – On every fix the software breaks in unexpected
    ways.
Viscosity

• A hack is cheaper to implement than the
  solution with in the design
  – preserving design moves are difficult to think and
    to implement.
  – Its much easier to do wrong thing than the right
    one.
Design

• What is the reason why design becomes rigid,
  fragile, immobile and viscous?

                  Improper
                dependencies
             between the modules
Good Design

• What are the characteristics of a good design


          High Cohesion




                              Low coupling
How can we achieve good
design?
Let’s Go SOLID…

•   Single Responsibility Principle
•   Open Close Principle
•   Liskov Substitution Principle
•   Interface Segregation Principle
•   Dependency Inversion Principle
Single Responsibility Principle
Single Responsibility Principle

• A software module should have one and only
  responsibility
• A software module should have one reason
  only to change
• It translates directly in high cohesion
Single Responsibility Principle

• Is SRP violate here?
           Interface modem
           {
                Public   Void   Dial (string pno);
                Public   void   hangup();
                Public   void   send (char c);
                Public   char   receive();
           }
Single Responsibility Principle

• Is SRP violate here?
           Interface Employee
           {
                 Public   pay calculate();
                 Public   void report(Writer w);
                 Public   void save();
                 Public   void reload();
           }
How to Solve?
  public abstract class BankAccount
   {
   double Balance { get; }
   void Deposit(double amount) {}
   void Withdraw(double amount) {}
   void AddInterest(double amount) {}
   void Transfer(double amount, IBankAccount
    toAccount) {}
   }
public abstract class BankAccount
 {
        double Balance { get; }
        void Deposit(double amount);
        void Withdraw(double amount);
        void Transfer(double amount, IBankAccount toAccount);
}

public class CheckingAccount : BankAccount { }



public class SavingsAccount : BankAccount
{
   public void AddInterest(double amount);
 }
Open Closed Principle
Open Closed Principle

• Modules should be open for extension but
  closed for modification.

• “You should be able to extend the behavior of
  a module without changing it”
public class ProductFilter
 {
    public IEnumerable<Product> ByColor(IList<Product>
   products, ProductColor productColor)
 {
foreach (var product in products)
{
    if (product.Color == productColor) yield return
   product;
 }
 } }
public class ProductFilter
 {
    public IEnumerable<Product> BySize(IList<Product>
   products, ProductSize productSize)
 {
foreach (var product in products)
{
    if (product.Size== productSize) yield return product;
 }
 } }
public class ProductFilter
 {
    public IEnumerable<Product> BySize(IList<Product>
   products, ProductColor productColor, ProductSize
   productSize)
 {
foreach (var product in products)
{
    if (product.Size== productSize &&
   product.color==productColor)
       yield return product;
 }
 } }
Any Problem?

• Every time a user asks for new criteria to filter a product, do
  we have to modify the ProductFilter class?
   – Yes! This means it is not CLOSED for modification.
• Every time a user asks for new criteria to filter a product, can
  we extend the behavior of the ProductFilter class to support
  the new criteria, without opening up the class file again and
  modifying it?
   – No! This means it is not OPEN for extension.
Solution

    • Template
public abstract class roductFilterSpecification
{
public IEnumerable<Product> Filter(IList<Product> products)
{
        return ApplyFilter(products);
 }
protected abstract IEnumerable<Product> ApplyFilter(IList<Product>
products);

}
public class ColorFilterSpecification
   :ProductFilterSpecification
{
 private readonly ProductColor productColor;

    public ColorFilterSpecification(ProductColor productColor)
{
        this.productColor = productColor;
 }
protected override IEnumerable<Product>
   ApplyFilter(IList<Product> products)
{ foreach (var product in products)
{ if (product.Color == productColor) yield return product; } }
   }
public IEnumerable<Product> By(IList<Product> products,
   ProductFilterSpecification filterSpecification)
 {
       return filterSpecification.Filter(products);

  }
Are we Fine now?
Liskov Substitution Principle
Liskov Substitution Principle

• Functions that use ... references to base classes
  must be able to use objects of derived classes
  without knowing it.




• A user of a base class should continue to function
  properly if a derivative of that base class is passed
  to it.
Interface Segregation Principle
Interface Segregation Principle

• ISP States that clients should not know about
  fat classes.
• Instead they should rely on clean cohesive
  interfaces
• Many client specific interfaces are better than
  one general purpose interface
Interface Segregation Principle

• A class with many clients, and one large
  interface to serve them all
Interface Segregation Principle
Dependency Inversion Principle
Dependency Inversion Principle

• High level modules should not depend on low
  level modules, both should depend

• Abstractions should not depend upon details,
  details should depend upon abstractions

• Every dependency in the design should target an
  interface, or an abstract class. No dependency
  should target a concrete class.
Dependency Inversion Principle
Dependency Inversion Principle
Dependency Inversion Principle

• Don’t depend on anything concrete, depend
  only upon abstraction

• High level modules should not be forced to
  change because of a change in low
  level/technology layers

• Drives you towards low coupling
Conclusion

• The main forces driving your design should be
  “High Cohesion” and “Low coupling”

• SOLID principles put you on right path.

Más contenido relacionado

Similar a Design Principles

Devconf 2011 - PHP - How Yii framework is developed
Devconf 2011 - PHP - How Yii framework is developedDevconf 2011 - PHP - How Yii framework is developed
Devconf 2011 - PHP - How Yii framework is developedAlexander Makarov
 
Common Project Mistakes: Visualization, Alarms, and Security
Common Project Mistakes: Visualization, Alarms, and SecurityCommon Project Mistakes: Visualization, Alarms, and Security
Common Project Mistakes: Visualization, Alarms, and SecurityInductive Automation
 
Things Every Professional Programmer Should Know
Things Every Professional Programmer Should KnowThings Every Professional Programmer Should Know
Things Every Professional Programmer Should KnowDaniel Sawano
 
Cleaning Code - Tools and Techniques for Large Legacy Projects
Cleaning Code - Tools and Techniques for Large Legacy ProjectsCleaning Code - Tools and Techniques for Large Legacy Projects
Cleaning Code - Tools and Techniques for Large Legacy ProjectsMike Long
 
Building Large Sustainable Apps
Building Large Sustainable AppsBuilding Large Sustainable Apps
Building Large Sustainable AppsBuğra Oral
 
Use Design Principle to Improve code quality
Use Design Principle to Improve code qualityUse Design Principle to Improve code quality
Use Design Principle to Improve code qualityHebin Wei
 
Refactoring to SOLID Code
Refactoring to SOLID CodeRefactoring to SOLID Code
Refactoring to SOLID CodeAdil Mughal
 
Software Development Life Cycle
Software Development Life CycleSoftware Development Life Cycle
Software Development Life Cyclenayanbanik
 
50500113 spiral-model
50500113 spiral-model50500113 spiral-model
50500113 spiral-modelasidharath
 
The OO Design Principles
The OO Design PrinciplesThe OO Design Principles
The OO Design PrinciplesSteve Zhang
 
Staging and Deployment
Staging and DeploymentStaging and Deployment
Staging and Deploymentheyrocker
 
It is a sunny day
It is a sunny dayIt is a sunny day
It is a sunny daybcoder
 
Friday final test
Friday final testFriday final test
Friday final testbcoder
 
Pointcut Rejuvenation: Recovering Pointcut Expressions in Evolving Aspect-Ori...
Pointcut Rejuvenation: Recovering Pointcut Expressions in Evolving Aspect-Ori...Pointcut Rejuvenation: Recovering Pointcut Expressions in Evolving Aspect-Ori...
Pointcut Rejuvenation: Recovering Pointcut Expressions in Evolving Aspect-Ori...Raffi Khatchadourian
 
The business case for contributing code
The business case for contributing codeThe business case for contributing code
The business case for contributing codeZivtech, LLC
 
Arch factory - Agile Design: Best Practices
Arch factory - Agile Design: Best PracticesArch factory - Agile Design: Best Practices
Arch factory - Agile Design: Best PracticesIgor Moochnick
 
Rediscovering Modularity - .NET Edition
Rediscovering Modularity - .NET EditionRediscovering Modularity - .NET Edition
Rediscovering Modularity - .NET EditionChris Chedgey
 
Kevin Whinnery: Best Practices for Cross-Platform Mobile Development
Kevin Whinnery: Best Practices for Cross-Platform Mobile DevelopmentKevin Whinnery: Best Practices for Cross-Platform Mobile Development
Kevin Whinnery: Best Practices for Cross-Platform Mobile DevelopmentAxway Appcelerator
 
Resilient Enterprise Design (Craig Villamor at Enterprise UX 2017)
Resilient Enterprise Design (Craig Villamor at Enterprise UX 2017)Resilient Enterprise Design (Craig Villamor at Enterprise UX 2017)
Resilient Enterprise Design (Craig Villamor at Enterprise UX 2017)Rosenfeld Media
 
Deep Dive into the Idea of Software Architecture
Deep Dive into the Idea of Software ArchitectureDeep Dive into the Idea of Software Architecture
Deep Dive into the Idea of Software ArchitectureMatthew Clarke
 

Similar a Design Principles (20)

Devconf 2011 - PHP - How Yii framework is developed
Devconf 2011 - PHP - How Yii framework is developedDevconf 2011 - PHP - How Yii framework is developed
Devconf 2011 - PHP - How Yii framework is developed
 
Common Project Mistakes: Visualization, Alarms, and Security
Common Project Mistakes: Visualization, Alarms, and SecurityCommon Project Mistakes: Visualization, Alarms, and Security
Common Project Mistakes: Visualization, Alarms, and Security
 
Things Every Professional Programmer Should Know
Things Every Professional Programmer Should KnowThings Every Professional Programmer Should Know
Things Every Professional Programmer Should Know
 
Cleaning Code - Tools and Techniques for Large Legacy Projects
Cleaning Code - Tools and Techniques for Large Legacy ProjectsCleaning Code - Tools and Techniques for Large Legacy Projects
Cleaning Code - Tools and Techniques for Large Legacy Projects
 
Building Large Sustainable Apps
Building Large Sustainable AppsBuilding Large Sustainable Apps
Building Large Sustainable Apps
 
Use Design Principle to Improve code quality
Use Design Principle to Improve code qualityUse Design Principle to Improve code quality
Use Design Principle to Improve code quality
 
Refactoring to SOLID Code
Refactoring to SOLID CodeRefactoring to SOLID Code
Refactoring to SOLID Code
 
Software Development Life Cycle
Software Development Life CycleSoftware Development Life Cycle
Software Development Life Cycle
 
50500113 spiral-model
50500113 spiral-model50500113 spiral-model
50500113 spiral-model
 
The OO Design Principles
The OO Design PrinciplesThe OO Design Principles
The OO Design Principles
 
Staging and Deployment
Staging and DeploymentStaging and Deployment
Staging and Deployment
 
It is a sunny day
It is a sunny dayIt is a sunny day
It is a sunny day
 
Friday final test
Friday final testFriday final test
Friday final test
 
Pointcut Rejuvenation: Recovering Pointcut Expressions in Evolving Aspect-Ori...
Pointcut Rejuvenation: Recovering Pointcut Expressions in Evolving Aspect-Ori...Pointcut Rejuvenation: Recovering Pointcut Expressions in Evolving Aspect-Ori...
Pointcut Rejuvenation: Recovering Pointcut Expressions in Evolving Aspect-Ori...
 
The business case for contributing code
The business case for contributing codeThe business case for contributing code
The business case for contributing code
 
Arch factory - Agile Design: Best Practices
Arch factory - Agile Design: Best PracticesArch factory - Agile Design: Best Practices
Arch factory - Agile Design: Best Practices
 
Rediscovering Modularity - .NET Edition
Rediscovering Modularity - .NET EditionRediscovering Modularity - .NET Edition
Rediscovering Modularity - .NET Edition
 
Kevin Whinnery: Best Practices for Cross-Platform Mobile Development
Kevin Whinnery: Best Practices for Cross-Platform Mobile DevelopmentKevin Whinnery: Best Practices for Cross-Platform Mobile Development
Kevin Whinnery: Best Practices for Cross-Platform Mobile Development
 
Resilient Enterprise Design (Craig Villamor at Enterprise UX 2017)
Resilient Enterprise Design (Craig Villamor at Enterprise UX 2017)Resilient Enterprise Design (Craig Villamor at Enterprise UX 2017)
Resilient Enterprise Design (Craig Villamor at Enterprise UX 2017)
 
Deep Dive into the Idea of Software Architecture
Deep Dive into the Idea of Software ArchitectureDeep Dive into the Idea of Software Architecture
Deep Dive into the Idea of Software Architecture
 

Design Principles

  • 1. Design Principles By Kartheek Nagasuri & Amit Grewal
  • 2. Design • What is the meaning of the Design? Design is about “How” • What is the Difference if compared to Analysis? Analysis is about “What”
  • 3. Design Why do we need (good) design? To Deliver Faster To Deal with Complexity To Manage Change
  • 4. Design • How do we know a design is bad? “W*F? “ I can’t Fix this code “W*F? “ “W*F? “ I fixed the problem “W*F? “ but it broke at “W*F? “ other places.. “W*F? “ “W*F? “ “W*F? “
  • 5. Design • Lets talk about criteria of bad design  • Are there any Symptoms of bad design? Immobility Rigidity Fragility Viscosity
  • 6. Rigidity • The Impact of the change is Unpredictable – Every change causes a cascade of changes in dependant modules. – A nice 2 days work become endless “Marathon” – Costs become unpredictable.
  • 7.
  • 8. Immobility • Its almost impossible to reuse interesting parts of the software – The useful modules have somany depandancies – The cost of rewriting is less compared to the risk faced to separate those parts
  • 9. Fragility • The Software tends to break in many places on every change. – The breakage occurs in areas with no conceptual relationship. – On every fix the software breaks in unexpected ways.
  • 10. Viscosity • A hack is cheaper to implement than the solution with in the design – preserving design moves are difficult to think and to implement. – Its much easier to do wrong thing than the right one.
  • 11. Design • What is the reason why design becomes rigid, fragile, immobile and viscous? Improper dependencies between the modules
  • 12. Good Design • What are the characteristics of a good design High Cohesion Low coupling
  • 13. How can we achieve good design?
  • 14. Let’s Go SOLID… • Single Responsibility Principle • Open Close Principle • Liskov Substitution Principle • Interface Segregation Principle • Dependency Inversion Principle
  • 16. Single Responsibility Principle • A software module should have one and only responsibility • A software module should have one reason only to change • It translates directly in high cohesion
  • 17. Single Responsibility Principle • Is SRP violate here? Interface modem { Public Void Dial (string pno); Public void hangup(); Public void send (char c); Public char receive(); }
  • 18. Single Responsibility Principle • Is SRP violate here? Interface Employee { Public pay calculate(); Public void report(Writer w); Public void save(); Public void reload(); }
  • 19. How to Solve? public abstract class BankAccount { double Balance { get; } void Deposit(double amount) {} void Withdraw(double amount) {} void AddInterest(double amount) {} void Transfer(double amount, IBankAccount toAccount) {} }
  • 20. public abstract class BankAccount { double Balance { get; } void Deposit(double amount); void Withdraw(double amount); void Transfer(double amount, IBankAccount toAccount); } public class CheckingAccount : BankAccount { } public class SavingsAccount : BankAccount { public void AddInterest(double amount); }
  • 22. Open Closed Principle • Modules should be open for extension but closed for modification. • “You should be able to extend the behavior of a module without changing it”
  • 23. public class ProductFilter { public IEnumerable<Product> ByColor(IList<Product> products, ProductColor productColor) { foreach (var product in products) { if (product.Color == productColor) yield return product; } } }
  • 24. public class ProductFilter { public IEnumerable<Product> BySize(IList<Product> products, ProductSize productSize) { foreach (var product in products) { if (product.Size== productSize) yield return product; } } }
  • 25. public class ProductFilter { public IEnumerable<Product> BySize(IList<Product> products, ProductColor productColor, ProductSize productSize) { foreach (var product in products) { if (product.Size== productSize && product.color==productColor) yield return product; } } }
  • 26. Any Problem? • Every time a user asks for new criteria to filter a product, do we have to modify the ProductFilter class? – Yes! This means it is not CLOSED for modification. • Every time a user asks for new criteria to filter a product, can we extend the behavior of the ProductFilter class to support the new criteria, without opening up the class file again and modifying it? – No! This means it is not OPEN for extension.
  • 27. Solution • Template public abstract class roductFilterSpecification { public IEnumerable<Product> Filter(IList<Product> products) { return ApplyFilter(products); } protected abstract IEnumerable<Product> ApplyFilter(IList<Product> products); }
  • 28. public class ColorFilterSpecification :ProductFilterSpecification { private readonly ProductColor productColor; public ColorFilterSpecification(ProductColor productColor) { this.productColor = productColor; } protected override IEnumerable<Product> ApplyFilter(IList<Product> products) { foreach (var product in products) { if (product.Color == productColor) yield return product; } } }
  • 29. public IEnumerable<Product> By(IList<Product> products, ProductFilterSpecification filterSpecification) { return filterSpecification.Filter(products); }
  • 30. Are we Fine now?
  • 32. Liskov Substitution Principle • Functions that use ... references to base classes must be able to use objects of derived classes without knowing it. • A user of a base class should continue to function properly if a derivative of that base class is passed to it.
  • 34. Interface Segregation Principle • ISP States that clients should not know about fat classes. • Instead they should rely on clean cohesive interfaces • Many client specific interfaces are better than one general purpose interface
  • 35. Interface Segregation Principle • A class with many clients, and one large interface to serve them all
  • 38. Dependency Inversion Principle • High level modules should not depend on low level modules, both should depend • Abstractions should not depend upon details, details should depend upon abstractions • Every dependency in the design should target an interface, or an abstract class. No dependency should target a concrete class.
  • 41. Dependency Inversion Principle • Don’t depend on anything concrete, depend only upon abstraction • High level modules should not be forced to change because of a change in low level/technology layers • Drives you towards low coupling
  • 42. Conclusion • The main forces driving your design should be “High Cohesion” and “Low coupling” • SOLID principles put you on right path.