Reaves.dev

v0.1.0

built using

Phoenix v1.7.12

Design Patterns

Stephen M. Reaves

::

2023-10-10

Notes about Lesson 9 of CS-6300

Summary

History of Patterns

Patterns Catalog

Factory Method Pattern

Intent

Allows for creating objects without specifying their class, by invoking a factory method (i.e., a method whose main goal is to create class instances)

Applicability

Structure

Gc Creator + factoryMethod() : Productcc Concrete Creator + factoryMethod() : Productcc->cp Product cc->p

Participants:

Example

public class ImageReaderFactory {
  public static ImageReader createImageReader(InputStream is) {
    int imageType = getImageType(is);
    
    switch (imageType) {
      case ImageReaderFactory.GIF
        return new GifReader(is);
      case ImageReaderFactory.JPEG
        return new JpegReader(is);
      // ...
    }
  }
}

Strategy Pattern

Intent

Allows for switching between different algorithms for accomplishing a task

Applicability

Structure

Ga Algorithm + algorithmInterface()c Context + contextInterface()a->ccsa Concreate Strategy A + algorithmInterface()csa->acsb Concreate Strategy B + algorithmInterface()csb->a

Participants:

Other Common Patterns

Choosing a Pattern

Approach