Giter VIP home page Giter VIP logo

design-patterns's Introduction

Design Patterns

Exploring design patterns and their uses.

Observer Pattern

This is an event model, also referred to as "PubSub".

Observers are used so functionality that's not directly related to an action can still listen in and react to that action. For example, the action of creating a new user might entail:

  1. Saving the user to a data store
  2. Sending a welcome email to the new user

Saving the user to the data store is a direct function of creating a new user. However, the email is an ancillary action. Using the Observer pattern, an application's email service could listen for a "user created" event in order to know when to send a welcome email.

In this way, the email service is not directly coupled to the code for creating of a new user.

Note: For this pattern, we could use PHP's built-in SPL subject and observer classes, but what fun is that? Learning about SplObjectStorage is more fun.

Decorator Pattern

The decorator pattern allows you to add behaviors or information to an object at run-time (vs "compile time"). This is used instead of inheritance, which can cause maintainability issues when new functionality is added.

Instead of modifying extending a class to add functionality, we can create classes which each encapsulate a new bit of functionality or data, and add it to our base object as needed.

An example is decorating a data-retrieval class with a cache implementation. You can wrap your data repository class in a decorator who's job is to attempt to retrieve requested data from cache. If the data does not exist in cache, it can pass the responsibility back the base data store class.

This can also be used with actions such as logging and profiling.

design-patterns's People

Contributors

fideloper avatar

Watchers

pieter lelaona avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.