• Stars
    star
    2,419
  • Rank 19,018 (Top 0.4 %)
  • Language
    C#
  • Created almost 4 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GoF Design Patterns with "Real-World" examples involving Food-Related Businesses and mock dependencies

Design Patterns Explained with Food πŸ₯•

GOF Design Patterns in C# with working examples inspired by food

This repository was made as a way to demonstrate and learn using moderately realistic examples of the Gang of Four Design Patterns (Design Patterns - Elements of Reusable Object-Oriented Software) by Gamma, Helm, Johnson, and Vlissides.

A YouTube series is in progress covering the details of each project. It starts here: https://www.youtube.com/watch?v=C_6uQwBjsmk

Motivation

Many examples of these patterns as taught online typically avoid incorporating external dependencies like Databases, AMQP queues, external services for things like email and HTTP APIs. While avoiding adding these dependencies to example code makes it easier to demonstrate the core motivation of the design patterns, it also makes it more difficult to imagine real-world scenarios where these patterns might be used. The projects in this repo contain mock versions of these types of dependencies incorporated into various plausible business scenarios. The examples are intended to be lean enough so that the principal benefit of each pattern is evident, but complex enough to avoid oversimplified scenarios.

Example Programs

Every design pattern has a corresponding .NET 5.0 console application.

Pattern Type Example Program Why Used
Abstract Factory Creational Custom Meal Planner We want to be able to create families of Diets with their own behavior for generating things like grocery lists and other data in an extensible fashion without specifying concrete classes in our client
Builder Creational Bakery Purchase Order System We want to separate the complex logic used to construct POs from its representation. We can provide a fluent syntax for creating new purchase orders and a seamless way to use the builder as an instance of the class it is responsible for building when construction is complete.
Factory Method Creational Food Delivery Service We want to make it easier to extend delivery type construction. We can introduce new delivery types without modifying client code.
Prototype Creational Dining Room Designer In our graphic design application, we'd like to make it easier to create icons that are expensive or cumbersome to otherwise obtain. We can create deep copies of protypical chair and table objects - without knowing the internal state of the copied object - for our designer that can be used and modified independently after being cloned.
Singleton Creational Ingredients Database Connection Pool We create a singleton connection pool manager that is used by different clients to connect to a database. If more clients try to connect than the pool permits, it prevents further connection. Uses Lazy initialization for thread safety.
Adapter Structural Recipe Search We need to use JSON to populate a recipe document store for our website, but our favorite API returns XML. We can use an adapter to match our required interface and use the existing resource as-is.
Bridge Structural Farmer's Market Simulator A Farmer's Market Simulator provides different class hierarchies for types of Vendors and types of Payment Processors. Since any vendor should be compatible with any Payment Processor, we favor composition using a bridge to develop the Vendor Abstraction from the Payment Processor Implementation independently.
Composite Structural Tea Cartonizer We sell tea by the carton. Cartons can contain more cartons. We'd like to print the number of servings on the outside of any carton, even if it contains lots of other cartons. We can use the composite pattern to treat the aggregation in the same way that we treat individual objects, exposing behavior to get the number of servings regardless of the specific object details, treating aggregations and individual objects uniformly.
Decorator Structural Front-of-House Service We have an automated system for letting diners know when their table is ready. By default, we make an announcement over the restaurant intercom. However, we would like to extend the ability to notify customers by email or text, and need a way to easily add, remove, and combine these options without adding a lot of complex logic or exponentially increasing the number of subclasses.
Facade Structural Grocery Store Report The underlying service providers that help us put our Grocery Store Reports are complex and hard to use. We want to hide that complexity behind an easy-to-use interface for use in our client code
Flyweight Structural Grocery Store City Planning Simulation As part of our city planning efforts, we want to build a grocery store in an optimal location. We are building a simulation that simulates the location and movement of our city's Inhabitants, which are objects containing fields and other objects. Some of these objects represent unchanging, static data that is shared by large portions of other inhabitants, like neighborhood and mode of travel. Some of the data is intrinsic to the inhabitant itself during the simulation, like its latitude and longitude. A flyweight can be used to save memory during the simulation by caching the extrinsic data as a shared resource among the hundreds of thousands of inhabitant objects.
Proxy Structural Food Bank Donation Processor Our Food Bank Processor uses an off-the-shelf system for accepting food donations. However, we want to control access to this resource while maintaining the same interface. We have the need to log use of the resource and prevent certain items from being donated, so we use a Proxy.
Chain of Responsibility Behavioral Kombucha Order Processor Our order pipeline for kombucha customers is pretty straightforward. We cartonize, query a loyalty program, print shipping labels if the order is online, and email or print a receipt. Some of the details of these steps change depending on some state - like whether the order is online, whether or not the customer is a loyalty member. For instance, we want to print ads on receipts for non-loyalty-member customers. We can use the chain of responsibility pattern to compose and execute each stage of the order processing pipeline.
Command Behavioral Composter Sign Up Form We run a company that allows people in our neighborhood to sign up for organic compost collection. Some complex business logic happens behind-the-scenes, including sending messages to AWS SQS queues for vehicle routing services to consume and sending welcome emails. We want a way to parametrize methods with different types of commands for executing this logic at different points in our onboarding process, so we create Command objects to encapsulate the logic and data required to complete these operations, and allow our client to execute arbitrary command types at specific times during its onboarding algorithm.
Interpreter Behavioral Chocolate Shop Barcode Reader We started a low-waste chocolate shop that serves fair-trade chocolate to patrons in bulk. Each container of chocolate needs to be scanned using a custom barcode scanner. The barcode needs to be interpreted by our software to perform downstream operations related to inventory and reporting. We'll build and integrate an interpreter for use by our scanner to make this possible.
Iterator Behavioral Best Restaurant Finder We built an app that lets users travel through their favorite restaurants in order of "best to worst", but different users have different criteria for ranking their restaurants. We generally want to be able to traverse a collection of restaurants without worrying about the underlying details or even revealing its structure. So, we'll use the Iterator pattern without exposing these implementation details.
Mediator Behavioral Food Truck Coordinator We need a way to coordinate our Food Trucks and Food Carts throughout the city. These various vehicles need to communicate with each other, but things are getting messy as we try to add new types of vehicles and reasons for communication. We'll build a Mediator to handle this complex collaboration so we can vary their interactions independently.
Memento Behavioral Doughnut Shop Cart We run a shop that sells the best doughnuts in the city. We run an online e-commerce store that allows users to add doughnuts to their cart. We want a way for customers to undo adding doughnuts to their cart, so we implemented a basic version of this feature using the Memento Pattern.
Observer Behavioral Diet Plan Tracking App We just launched our diet plan tracking app, and our users are constantly refining their profiles with their new weight, exercise goals, and photos. Every time the profiles get updated, we need to kick off other processes to fulfill many different features of our service. We tried polling for changes, but that's getting too expensive, and we're missing updates between intervals. Let's switch from a "pull" to a "push" design using the Observer pattern, so all of our dependent services can subscribe to the profile change event.
State Behavioral Cafe Rewards Program Our family coffee shop offers a tiered rewards for frequent patrons. The more points a customer earns, the higher their tier. We want to check and adjust customer tiers with each purchase, so we'll implement a State Pattern so that the Rewards Evaluator behaves according to the Rewards Tier for a customer.
Strategy Behavioral Restaurant Menu Changer We run a restaurant that displays a menu that changes throughout the day. We have different processes that go into making breakfast, lunch, and dinner menus, but higher-level processes remain the same. We cook and serve food at all hours, but the strategy for getting that done varies depending on the circumstance. We also want to easily be able to generate menus for higher or lower price ranges at different locations without duplicating code or cluttering high-level objects with conditional logic. We can rely on the benefits of composition over inheritance and implement the Strategy Pattern to keep our code a bit cleaner.
Template Method Behavioral Cookbook Printer We run a cookbook printing business for self-published chefs. Much of the process used to print the recipes in our cookbooks is exactly the same, but often differs slightly depending on the recipe. We use a template method with default behavior and virtual behavior overridden (either forcibly or optionally) by concrete recipes using inheritance.
Visitor Behavioral Farmer's Market Research We work for a company that provides sales and marketing reports for regional Farmer's Markets. Different Farmer's Markets and their vendors have their own logic for generating data we can use for our reports, but we need to be able to generate our reports without significantly modifying any of the existing data-producing classes. We instead make them 'visitable' by our own classes, which can in turn use them as needed to generate custom reports for sales and marketing teams.

More Repositories

1

lightlib-lms

A lightweight library management system built in .NET 5.0 MVC with Postgres and EF Core
C#
151
star
2

react-firebase-notes-app

A simple app to explore React with Firebase
JavaScript
74
star
3

base-chat

A simple chat application built using Angular and Firebase
TypeScript
52
star
4

dotfiles

My dotfiles
Vim Script
38
star
5

wake_engine

A chess engine in Python using numPy (WIP). Livestreamed here: www.youtube.com/watch?v=1QotIA4_jb4
Python
33
star
6

react-flashcards

A simple React + Firebase flashcard application
JavaScript
32
star
7

dotnet-forum-starter

ProductiveDev Forum Demo App
C#
30
star
8

ng-sight

Business Intelligence Dashboard built with Angular 4
TypeScript
30
star
9

ng-pixabay-api-search

A sample Angular 4 application built to query the Pixabay API for images. Implements Material Design and Masonry.
TypeScript
26
star
10

ng-run-journal

An angular application to journal GPS routes and display them using Leaflet and Mapbox
TypeScript
24
star
11

ng-gallery

Angular 4 simple gallery project
TypeScript
22
star
12

flask-ner

Flask + spaCy simple API for Named Entity Recognition
Python
20
star
13

storytelling

Links to compelling stories about programming and how storytelling itself relates to computing
19
star
14

python_epidemic_simulation

A crude simulation of an epidemic in Python with pygame visualization
Python
18
star
15

5-minute-fridays

Code from my Five Minute Fridays YouTube series
C#
15
star
16

Javelin

Search / Information Retrieval patterns using .NET
C#
11
star
17

house-prices

EDA, linear regression, random forest house price prediction
Jupyter Notebook
10
star
18

limbus

A basic NLP pipeline in Python with time-series sentiment analysis
Python
8
star
19

LangLearn

Language Learning example app - Blazor + .NET Core Web API
HTML
7
star
20

advantage-api

A basic web API with PostgreSQL back end, sample data seed for developing front-end apps.
C#
6
star
21

data-science-notebooks

A collection of personal jupyter notebooks
Jupyter Notebook
6
star
22

demo-scripts

Repository containing scripts used for my YouTube tutorials
5
star
23

python-twisted-chat-server

working through examples from twisted network programming essentials
Python
4
star
24

sicp-meetup

Structure and Interpretation of Computer Programs Meetup
Scheme
4
star
25

csharp-gof

Gang Of Four Design Pattern implementations in C#
C#
4
star
26

ninety-nine-c-sharp

Completing 99 Problems using C-Sharp (http://aperiodic.net/phil/scala/s-99/)
C#
3
star
27

hanzil

Angular2 application to help users learn Hanzi (in progress)
TypeScript
3
star
28

deep-learning-book-notes

My notes from the Deep Learning book by Goodfellow, Bengio, and Courville. (WIP)
Jupyter Notebook
3
star
29

group_sketch

Jupyter Notebook
2
star
30

wesdoyle

2
star
31

c-algos

Implementations of useful algorithms in C
C
2
star
32

abbreviator

Abbreviates words in a string.
Python
2
star
33

librarian-of-babel

Chrome Plugin for investigating Borges "Library of Babel" emulation at https://libraryofbabel.info/
JavaScript
1
star
34

facedetect

Face detection using OpenCV + Raspberry Pi
Python
1
star
35

when-is-chess

fetches chess events from the library calendar
Python
1
star
36

jira-actions-test

JIRA actions test
1
star
37

mnist-paint

Vanilla JS + TensorFlow paint with MNIST hand-written digit classification
Jupyter Notebook
1
star
38

wesdoyle.net

HTML
1
star
39

my-blog

https://wesdoyle.net | My blog. Current iteration is built with Nuxt.js + Tailwind
Vue
1
star
40

recursive.link

Vue + d3 chaos games
Vue
1
star
41

blog

Source code for my old blog. New source code is here: https://github.com/wesdoyle/my-blog
Vue
1
star
42

ctci-jupyter

Working through Cracking the Coding Interview, 6th Edition with Python with Jupyter Notebook
Jupyter Notebook
1
star
43

clrs-jupyter

Working through CLRS (Introduction to Algorithms) in Python with Jupyter Notebook
Jupyter Notebook
1
star