Design Patterns
and Principles

What we’ll cover

What is a Design Pattern?

Purpose of a Design Pattern

Four Essential Elements of a Pattern

  1. Pattern Name
  2. Problem
  3. Solution
  4. Consequences

1. Pattern name

2. Problem

3. Solution

4. Consequences

Classifying Design Patterns





Creational Patterns

Two Recurring Themes of Creational Patterns

  1. They encapsulate knowledge about which concrete classes the system uses
    • Abstracts the instantiation process
  2. Hide how instances of classes are created and put together
    • Helps make a system independent of how its objects are created, composed, or represented


Creational Patterns: Builder

Brief Example

Creational Patterns: Factory

Brief Example

public class LicenseFactory {
  private LicenseBuilder builder;

  public LicenseFactory(LicenseBuilder builder) {
    this.builder = builder;
  }

  public License createLicense(String addressLine1, String addressLine2, String city, String state, String zipcode) {
      return builder
              .setAddressLine1(addressLine1)
              .setAddressLine2(addressLine2)
              .setCity(city)
              .setState(state)
              .setZipcode(zipcode)
              .createLicense();
  }
  public License createLicense(String firstName, String lastName, Date birthdate, Long licenseNumber) {
      return builder
              .setFirstName(firstName)
              .setLastName(lastName)
              .setBirthDate(birthdate)
              .setLicenseNumber(licenseNumber)
              .createLicense();
  }
}


Prototype Design Pattern

Description

Brief Example

public class LicenseBuilder {
    private Date birthDate;
    private String firstName;
    private String lastName;
    private Long licenseNumber;
    private Character driverClass;
    private String addressLine1;
    private String addressLine2;
    private String city;
    private String state;
    private String zipcode;

    public LicenseBuilder(LicenseBuilder licenseBuilderToBeCloned) {
        this.firstName = licenseBuilderToBeCloned.firstName;
        this.lastName = licenseBuilderToBeCloned.lastName;
        this.birthDate = licenseBuilderToBeCloned.birthDate;
        this.licenseNumber = licenseBuilderToBeCloned.licenseNumber;
        this.driverClass = licenseBuilderToBeCloned.driverClass;
        this.addressLine1 = licenseBuilderToBeCloned.addressLine1;
        this.addressLine2 = licenseBuilderToBeCloned.addressLine2;
        this.city = licenseBuilderToBeCloned.city;
        this.state = licenseBuilderToBeCloned.state;
        this.zipcode = licenseBuilderToBeCloned.zipcode;
    }
}




Structural Patterns

Brief Example

Behavioral Patterns

Template Pattern

Example

// degree of 1
public static Integer[] getRange(int start) {
    return getRange(0, start, 1);
}
// degree of 2
public static Integer[] getRange(int start, int stop) {
    return getRange(start, stop, 1);
}
// template method; degree of 3
public static Integer[] getRange(int start, int stop, int step) {
    List<Integer> list = new ArrayList<>();
    for(int i = start; i<stop; i+=step) {
        list.add(i);
    }
    return list.toArray(new Integer[list.size()]);
}