Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Software Design Patterns

Software Design Patterns

In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code. It is a description or template for how to solve a problem that can be used in many different situations. Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system.

Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. Patterns that imply mutable state may be unsuited for functional programming languages, some patterns can be rendered unnecessary in languages that have built-in support for solving the problem they are trying to solve, and object-oriented patterns are not necessarily suitable for non-object-oriented languages.

Design patterns may be viewed as a structured approach to computer programming intermediate between the levels of a programming paradigm and a concrete algorithm.
-wiki

Krishantha Dinesh

October 21, 2019
Tweet

More Decks by Krishantha Dinesh

Other Decks in Programming

Transcript

  1. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Objectives • Not to

    teach all available design patterns • Give the basic understanding about patterns Java walkthrough by Krishantha Dinesh - www.krishantha.com
  2. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Prerequisites • Basic java

    understanding • Mobilephone.soundmode=soundmode.completesilent && soundmode.completesilent != soundmode.vibrate Java walkthrough by Krishantha Dinesh - www.krishantha.com
  3. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Prerequisites -2 • OOP

    Programming experience • OOOP Concepts • Basic UML understanding • Basic understanding about Elipse Design patterns by Krishantha Dinesh - www.krishantha.com
  4. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ What and why are

    the design patterns • Design pattern mean well matured tested solution for common problems • Design patterns are not bounded with specific solution / implementation • Design pattern is not specific for domain. i.e pattern A for inventory domain and pattern B for manufacturing domain • Design pattern will be general solution for common problems • It is not a library and you cannot download and install • Its not lead to reuse code.. But experionse Design patterns by Krishantha Dinesh - www.krishantha.com
  5. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Father ?? • Its

    initially introduced by “gang of four” • Erich Gamma, • Richard Helm, • Ralph Johnson, • John Vlissides • Originally there were 23 Design patterns by Krishantha Dinesh - www.krishantha.com
  6. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Is there special name?

    • Register your object with mine.. Implement update method for it.. When it get called call getValue method of mine • Observer pattern Design patterns by Krishantha Dinesh - www.krishantha.com
  7. Strategy pattern In computer programming, the strategy pattern a.k.a policy

    pattern is a software design pattern that enables an algorithm's behavior to be selected at runtime. • defines a family of algorithms, • encapsulates each algorithm, and • makes the algorithms interchangeable within that family. Design patterns by Krishantha Dinesh - www.krishantha.com
  8. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Inheritance • You should

    go with inheritance only and only if its inline with “is a relationship” • Having inheritance is good. But having too much is bad • When have over inheritance its lead to inflexible code. • Also difficult to debug Design patterns by Krishantha Dinesh - www.krishantha.com
  9. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ • As example take

    Human as super class and Employee as child class • Human has method as walk() and hear() • When Employee, Student extends from Human it fine. • But when Infant implements from Human we need to override the walk() method. • When deaf patient implements from Human we need to override hear() • Too many override means its wrong use of inheritance. • We are not getting reuse benefits • Change on parent class can cause ripple down effect • Changing behaviors in runtime is difficult Design patterns by Krishantha Dinesh - www.krishantha.com
  10. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ How can solve this

    problem • We can use interface as Hearable and Walkable • We can implements those if over object deserve it • And we can use Human as it is and remove those method from it • Problem solved ?? Design patterns by Krishantha Dinesh - www.krishantha.com
  11. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Solution lead other problem

    • When we have to extends 20 classes from Human we need to implement walk() and hear() method every where • It remove the code reusability • Maintenance is really hard Design patterns by Krishantha Dinesh - www.krishantha.com
  12. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Rule #1 • Identify

    the aspect of your code that different and how separate them from what they say as same • If you think with encapsulation principle it says “Encapsulate what varies” • When you need to keep alter hear() and walk() behaviors that is very good sign to pull them out • When you separate thing which frequently changing you can manage your code without effecting others Design patterns by Krishantha Dinesh - www.krishantha.com
  13. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Rule number 2 •

    Code to interface. Not to the implementation • This lead lot of flexibility and maintainability • You can switch between any implementation which share same interface Design patterns by Krishantha Dinesh - www.krishantha.com
  14. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Rule number 3 •

    Favor composition over inheritance • We can say human has a walking behavior. • Instead of inheriting that we can compose that. • This lead runtime behavior change • Also use if need only Design patterns by Krishantha Dinesh - www.krishantha.com
  15. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ • We can move

    behavior our from inheritance and make it as composition. • In a given example we can use walkable() and hearable() out from main inheritance tree. Still you can user inheritance behavior like breath() feed() and s on Design patterns by Krishantha Dinesh - www.krishantha.com
  16. Observer pattern The observer pattern is a software design pattern

    in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems. Design patterns by Krishantha Dinesh - www.krishantha.com
  17. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Publisher and receiver Notification

    service Subscriber Subscriber Subscriber Non Subscriber Design patterns by Krishantha Dinesh - www.krishantha.com
  18. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ • Its create one

    to many relationship between object • Publisher call as “subject” and observer call as dependents Design patterns by Krishantha Dinesh - www.krishantha.com
  19. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Loosely coupled ?? •

    The are interacting. But don’t know much about each other. • So they coupled • Subject only knows observer as interface. But not about the real implementation. • Subject only knows he has list of observers • So loosely coupled Design patterns by Krishantha Dinesh - www.krishantha.com
  20. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Rule number 4 •

    Encourage for loose coupling as much as possible. • Its give you the flexibility to deal with object with out breaking contract Design patterns by Krishantha Dinesh - www.krishantha.com
  21. Decorator pattern In object-oriented programming, the decorator pattern a.k.a Wrapper

    is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. Design patterns by Krishantha Dinesh - www.krishantha.com
  22. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Rule number 5 •

    Open for extension close for modification. • This is known as “open close principle” • In future modification if you need to modify code that is bad. You should be able to extent without modify existing code • This is one of most important design pattern Design patterns by Krishantha Dinesh - www.krishantha.com
  23. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Overview of decorator pattern

    https://creately.com/app?diagid=i339 i1kv2 Design patterns by Krishantha Dinesh - www.krishantha.com
  24. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Car is a vehicle

    and van is a vehicle Design patterns by Krishantha Dinesh - www.krishantha.com
  25. Singleton pattern In software engineering, the singleton pattern is a

    design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. Design patterns by Krishantha Dinesh - www.krishantha.com
  26. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Why we need it

    • Login facility • Access hardware • UI dialog • Thread pools • Anywhere when we need to access by one at a time Design patterns by Krishantha Dinesh - www.krishantha.com
  27. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ new SigletonClass() ?? •

    Wont it do the job? • NO • Because some one else can use same statement to create object. Its multiple Design patterns by Krishantha Dinesh - www.krishantha.com
  28. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ The what we can

    do? • Make constructor private • That make only same class can access it. No exteral classes can call it • Create other static method to give access to other classes. Design patterns by Krishantha Dinesh - www.krishantha.com
  29. * http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Problem solved • Its

    seems like problem solved • But is it really solved? • This code is not thread safe Design patterns by Krishantha Dinesh - www.krishantha.com