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

Design Patterns

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

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.

Tushar Sharma

March 28, 2018

Transcript

47 slides
  1. 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
  2. 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
  3. 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"!
  4. 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 :)
  5. 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.
  6. Essential Elements of Patterns • Name • Problem • Problem

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

    • Changeability • Testability • ... • Common vocabulary
  8. 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
  9. 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.
  10. Exercise 2 • Write a Utility class for reading a

    text file and writing to a text file.
  11. 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.
  12. Let us use time machine to go one week back

    and revisit patterns quickly
  13. 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.
  14. Essential Elements of Patterns • Name • Problem • Problem

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

    • Changeability • Testability • ... • Common vocabulary
  16. 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
  17. Factory Pattern • Motivation • Creating objects without exposing the

    instantiation logic to the clients • Providing a common interface to refer all objects (of a kind)
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. Composite Pattern • Benefits • Transparency (Clients are shielded from

    the object hierarchy) • Extensibility (New leafs are easy to add)
  25. 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
  26. 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
  27. 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

More Decks by Tushar Sharma

Other Decks in Programming