Coding Bootcamp: Introduction to Design Patterns

Who Am I?

Quick Recap - Principles of Object-orientation

Principles of Object-orientation

Principles of Object-orientation

Principles of Object-orientation

Principles of Object-orientation

Design Patterns - A Motivating Example

Design Patterns - A Motivating Example

Implementation

public class Course {

    private Student student;
    private Instructor instructor;

    public Course() {
        student = new Student();
        instructor = new Instructor();
    }
    private void updateAll() {
        student.update();
        instructor.update();    
    }
    public void changeTime() {
        //change time
        updateAll();
    }
}
public class Program {

    public static void main(String[] args) {
        Course course = new Course();
        course.changeTime();
    }
}

Implementation - Cont.

Implementation - Take 2

public class Program {

    public static void main(String[] args) {
        Student student = new Student();
        Instructor instructor = new Instructor();
        Organizer organizer = new Organizer();
        
        Course course = new Course();
        course.addObserver(student);
        course.addObserver(organizer);
        course.addObserver(instructor);
        
        course.changeTime();
    }
}

Implementation - Take 2

import java.util.Observable;

public class Course extends Observable {

    public void changeTime() {
        //change time
        setChanged(); // obligatory before notifying observers
        notifyObservers();
    }
}
import java.util.Observable;

public class Student implements Observer {

    @Override
    public void update(Observable o, Object arg) {

    }
}

A Motivating Example

A Motivating Example

The Implemented Solution

Observer Design Pattern

Observable and Observer (in Java)

What About .NET

Let Us Define Design Patterns

Essential Elements of Patterns

Why to Use Patterns

Categories of Patterns

Exercise 1

Implement a program that notifies the stakeholders whenever the time changes for the course.

Exercise 2

Exercise 3

Use regular expressions to achieve that.

Factory Pattern

Factory Pattern

Factory Pattern Example

Factory Pattern

Let us extend the Factory Pattern


Abstract Factory Pattern

Abstract Factory Pattern

Strategy Pattern


Strategy Pattern

Strategy Pattern

Strategy Pattern

Composite Pattern

Composite Pattern

Motivation

Composite Pattern

Composite Pattern

Composite Pattern

Façade Pattern

Façade Pattern

Façade Pattern

Façade Pattern

References

Exercises

  1. Complete exercise 1, 2, and 3
  2. Provide another way to implement the same functionality but by using string comparison. Pay attention to your design (hint: implement strategy and factory paterns)
  3. 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)

Creative Commons Licence
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.