Slide 1

Slide 1 text

Design Patterns Krishantha Dinesh Msc, MIEEE, MBCS Software Architect www.krishantha.com www.youtube.com/krish @krishantha

Slide 2

Slide 2 text

* 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

Slide 3

Slide 3 text

* 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

Slide 4

Slide 4 text

* 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

Slide 5

Slide 5 text

* 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

Slide 6

Slide 6 text

* 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

Slide 7

Slide 7 text

* 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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

* 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

Slide 10

Slide 10 text

* 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

Slide 11

Slide 11 text

* 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

Slide 12

Slide 12 text

* 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

Slide 13

Slide 13 text

* 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

Slide 14

Slide 14 text

* 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

Slide 15

Slide 15 text

* 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

Slide 16

Slide 16 text

* 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

Slide 17

Slide 17 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Break story Design patterns by Krishantha Dinesh - www.krishantha.com

Slide 18

Slide 18 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Design patterns by Krishantha Dinesh - www.krishantha.com

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

* 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

Slide 21

Slide 21 text

* 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

Slide 22

Slide 22 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ News casting service Design patterns by Krishantha Dinesh - www.krishantha.com

Slide 23

Slide 23 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Design patterns by Krishantha Dinesh - www.krishantha.com

Slide 24

Slide 24 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Design patterns by Krishantha Dinesh - www.krishantha.com

Slide 25

Slide 25 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Observer patter with java interface/class Design patterns by Krishantha Dinesh - www.krishantha.com

Slide 26

Slide 26 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Design patterns by Krishantha Dinesh - www.krishantha.com

Slide 27

Slide 27 text

* 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

Slide 28

Slide 28 text

* 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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

* 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

Slide 31

Slide 31 text

* 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

Slide 32

Slide 32 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Real example Design patterns by Krishantha Dinesh - www.krishantha.com

Slide 33

Slide 33 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Classic implementation Design patterns by Krishantha Dinesh - www.krishantha.com

Slide 34

Slide 34 text

* 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

Slide 35

Slide 35 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Decorate it Design patterns by Krishantha Dinesh - www.krishantha.com

Slide 36

Slide 36 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Design patterns by Krishantha Dinesh - www.krishantha.com

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

* 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

Slide 39

Slide 39 text

* 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

Slide 40

Slide 40 text

* 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

Slide 41

Slide 41 text

* 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

Slide 42

Slide 42 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Design patterns by Krishantha Dinesh - www.krishantha.com

Slide 43

Slide 43 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Eager loading Design patterns by Krishantha Dinesh - www.krishantha.com

Slide 44

Slide 44 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Solved in expensive way Design patterns by Krishantha Dinesh - www.krishantha.com

Slide 45

Slide 45 text

* http://www.krishantha.com * https://www.youtube.com/krish * https://www.linkedin.com/in/krish-din/ Design patterns by Krishantha Dinesh - www.krishantha.com