Software Design Patterns

Jul 11, 2024

Software Design Patterns

Introduction

  • Junior developers' code often likened to Play-Doh snakes.
  • Senior developers' code compared to the Sistine Chapel.
  • Principal engineers revert to simple patterns for maintainability.
  • Lecture will cover 10 software design patterns, including pros and cons.
  • Patterns categorized:
    • Creational (object creation)
    • Structural (object relationships)
    • Behavioral (object communication)

Importance of Design Patterns

  • Influential text: Design Patterns by the Gang of Four.
  • Design patterns help solve recurring problems in programming.
  • Key to being a proficient software engineer: problem-solving, not syntax memorization.

Singleton Pattern

  • Only one instance of an object can be created.
  • JavaScript example: global object vs singleton class.
  • Use language features before patterns to avoid unnecessary boilerplate.

Prototype Pattern

  • Alternative to inheritance: clones objects instead of creating class hierarchies.
  • JavaScript supports prototypal inheritance natively.
  • Use Object.create to set up inheritance from a prototype object.

Builder Pattern

  • Construct objects step-by-step using methods instead of constructors.
  • Useful for managing complex object creation processes.
  • Method chaining allows for the accumulation of settings/options.

Factory Pattern

  • Uses methods to instantiate objects, avoiding the new keyword.
  • Ideal for cross-platform apps with different implementations for iOS and Android.

Facade Pattern

  • Simplifies complex systems by providing a simple API.
  • Example: Simplifying electrical and plumbing operations in a house with a single interface.
  • Common in many JavaScript packages (e.g., jQuery).

Proxy Pattern

  • Acts as a substitute for another object.
  • Example: Vue.js reactivity system uses proxies to intercept data changes and update the UI.
  • Useful for large objects to simulate the original without duplicating.

Iterator Pattern

  • Traverses collections of objects.
  • Custom iterator in JavaScript can implement a range function with next and done properties.
  • Supports loops like for...of using symbol iterator.

Observer Pattern

  • One-to-many relationship: multiple objects subscribe to events from a single object.
  • Used in reactive systems like Firebase and RxJS for real-time updates.
  • Subject class in RxJS allows multiple subscriptions and broadcast changes.

Mediator Pattern

  • Manages complex communication between many objects.
  • Example: Air traffic controller managing airplane and runway interactions.
  • Middleware in Express.js as mediator for HTTP requests and responses.

State Pattern

  • Represents an object's behavior based on its state.
  • Avoids complex conditional logic by using state-specific classes.
  • Example: Different behaviors based on an object's mood/state using finite state machines.

Conclusion

  • Design patterns can significantly simplify complex code and make it more maintainable.
  • Practical real-world implementations illustrate the utility of each pattern.
  • Further learning recommended on additional design patterns.