Design Patterns

Design Patterns

This presentation provides an overview of design patterns for object-oriented programming languages. It covers Observer, Factory, Abstract factory, Composite, and Facade patterns.

16a26342d4ff4724dfe59b0dc947dead?s=128

Tushar Sharma

March 28, 2018
Tweet

Transcript

  1. Design Patterns Advanced Topics in Software Engineering

  2. Principles of Object-orientation • Abstraction - Simplify and generalizes entities

    • Encapsulation - Separation of concerns and information hiding • Modularization - Cohesive and loosely-coupled abstractions • Hierarchy - Hierarchical organization of abstractions
  3. A motivating Example • There is the Software Engineering in

    Practice Course and a few stakeholders (Students and Instructors) • Any change in the course must be notified to all the stakeholders • Now, think about its implementation
  4. A motivating Example (2)

  5. Implementation

  6. Implementation (2) • Is it easy to add a new

    observer (a new Student or a new Instructor)? • You need to make multiple changes • What about adding a new category of observers; for example "Organizer"!
  7. Implementation – Take 2

  8. A motivating example (3) • Now, think again about adding

    new observers. • Is it easier? • In the first implementation, `Course` class is "tightly-coupled" to `Student` and `Instructor` classes. While in the second case, `Course` class is "loosely-coupled" to it's observers. • Congratulations!! You have just learned the first design pattern :)
  9. The Implemented Solution

  10. Observer Design Pattern

  11. Observable and Observer (in Java)

  12. Let Us Define Design Patterns • Design patterns are descriptions

    of communicating objects and classes that are customized to solve a general design problem in a particular context. (By Gang of Four) • Design patterns are solutions to commonly occurring design problems.
  13. Essential Elements of Patterns • Name • Problem • Problem

    and context • Solution • Relationships, roles, and responsibilities • Consequences • Benefits and liabilities • Tradeoffs
  14. Why to Use Patterns • Higher design quality • Flexibility

    • Changeability • Testability • ... • Common vocabulary
  15. Categories of Patterns • Creational - patterns answer how to

    create and when to create • Examples: Factory, Abstract Factory, Builder • Structural - patterns answer how to compose/structure • Examples: Adapter, Composite, Decorator • Behavioral - patterns describe how a group of objects cooperate to carry out a task • Examples: Observer, Strategy, Visitor
  16. Exercise 1 • In the context of the SEiP course,

    there is a course, students, instructors, and organizers. • The course has a start time and end time. • If any of the timing changes, the stakeholders have to be notified. Implement a program that notifies the stakeholders whenever the time changes for the course.
  17. Exercise 2 • Write a Utility class for reading a

    text file and writing to a text file.
  18. Exercise 3 • Write a program to compute following metrics

    for Java code: • LOC (Lines of Code) • Number of classes • Number of methods. • Write the metrics in a file. • Use regular expressions to calculate the aforementioned metrics. • Use the read & write methods from the Utility class created in Exercise 2.
  19. Design Patterns Part-2 Advanced Topics in Software Engineering

  20. Let us use time machine to go one week back

    and revisit patterns quickly
  21. Design Patterns: Definition • Design patterns are descriptions of communicating

    objects and classes that are customized to solve a general design problem in a particular context. (By Gang of Four) • Design patterns are solutions to commonly occurring design problems.
  22. Essential Elements of Patterns • Name • Problem • Problem

    and context • Solution • Relationships, roles, and responsibilities • Consequences • Benefits and liabilities • Tradeoffs
  23. Why to Use Patterns • Higher design quality • Flexibility

    • Changeability • Testability • ... • Common vocabulary
  24. Categories of Patterns • Creational - patterns answer how to

    create and when to create • Examples: Factory, Abstract Factory, Builder • Structural - patterns answer how to compose/structure • Examples: Adapter, Composite, Decorator • Behavioral - patterns describe how a group of objects cooperate to carry out a task • Examples: Observer, Strategy, Visitor
  25. Let us use time machine again and come back to

    the present J
  26. Factory Pattern • Motivation • Creating objects without exposing the

    instantiation logic to the clients • Providing a common interface to refer all objects (of a kind)
  27. Factory Pattern

  28. Factory Pattern - Example

  29. Factory Pattern • Benefits • Separation of concerns - the

    logic to instantiate classes is separated • Flexibility - to extend Product hierarchy without affecting clients • Liabilities • Don't bypass
  30. Let us extend the Factory pattern • Motivation • Let

    us assume you have families of objects to instantiate • Widget library providing UI elements such as Button and TextBox • The library instantiate UI elements based on the platform such as Windows and Linux
  31. Abstract Factory Pattern

  32. Abstract Factory Pattern • Benefits • Flexibility (Entire product families

    can be easily exchanged) • Separation of concerns (Object lifetime management is separated from object use) • Liabilities • GoF Abstract Factory only covers object creation, not object disposal
  33. Strategy Pattern • Motivation • Support for more than one

    algorithm required in many situations: • Clients do not want to be dependent on a supplied default algorithm • Suitability of an algorithm may change based on the context (input, platform, ...) • Integrating new algorithms should be easy (without modifying existing code, if possible) • Examples • Compression algorithm, sort algorithm
  34. Strategy Pattern

  35. Strategy Pattern • Benefits • Freedom to choose algorithms •

    Easier extensibility • Applicability • Many related classes differ only in their behavior • You need different variants of an algorithm • A class defines many behaviors, and these appear as multiple conditional statements in its operations
  36. Composite Pattern

  37. Composite Pattern • Motivation • Need to represent “whole-part” hierarchies

    with following requirements • Preserve hierarchical structure • Same interface for both compound or atomic objects for clients • It should be easy to extend the hierarchy with new element types
  38. Composite Pattern

  39. Composite Pattern

  40. Composite Pattern • Benefits • Transparency (Clients are shielded from

    the object hierarchy) • Extensibility (New leafs are easy to add)
  41. Façade Pattern • Motivation • Let us assume that we

    have a component that provide complex multi-steps services • How to provide a client simplified access to the functionality of such a component
  42. Façade Pattern

  43. Façade Pattern • Benefits • Provides a simple interface to

    a complex system • De-couples the subsystem for clients and other subsystems, thereby promoting subsystem independence and portability • Layers the subsystem • Use façade to define an entry point to each subsystem level • If subsystems are dependent, then the dependencies between them can be simplified by making them communicate with each other solely through their facades
  44. None
  45. Further Reading

  46. Exercises 3. Complete exercise 1, 2, and 3 4. Provide

    another way to implement the same functionality but by using string comparison. Pay attention to your design (hint: implement strategy and factory patterns) 5. Extend the above program and implement a segregated interface to compute all the supported metrics. Write all the computed metrics to a CSV file. (hint: use Facade pattern) Deadline: 25 May, 2017
  47. Courtesy: spikedmath.com Thank you!!