DEV Community

CodingBlocks

Command, Repository and Mediator Design Patterns

In this episode we go back to the design pattern well that we've been away from for so long.  We cover the Command, Repository and Mediator design patterns.  It was hard for us to believe, but it's been almost a year since our last design patterns episode!!!  Come on in for fun, learning, and of course, our tips of the week.

See the original show notes at:
http://www.codingblocks.net/epsiode42

Leave us a review here:
http://www.codingblocks.net/review

News

Stitcher Reviews:

Spectre013, Christoffer, Genius, HAM3rtag, joe_recursion_joe, Gearhead2k, Manriquey2k, Mike North, AndrewM, MildManneredCalvin, Freeleeks

iTunes Reviews:

Sid Savara, J. Mair, tonicorb, Nmkel999, Eschwartz20, mochadwi, Banjammin, wisco_cmo, NewZeroRiot, Nate_the_DBA, Pauloispaulo

Joe’s chess game!

Joe on Hello Tech Pros

Software Engineering Radio #256 on Unit Testing

!important is a CSS code smell.

Do you name your IIFEs? You should.

This is our 5th, Design Patterns Episode. Last one was in July 2015.

30: Adapter, Facade, and Memento
19: Iterators, Observers, and Chains
16: Strategy, Template, Null Object
11: Factories, Factory Methods, Builder, Prototype

What type of development do you prefer?

Front-End
Back-End
Full Stack

Design Patterns - Command, Repository, and Mediator

Command Pattern

  • Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undo.
  • It's not "method()" it's "new Object(), and later...object.invoke()
  • Why? Nice, clean way of organizing your code. Especially if….
  • OO replacement for callbacks - meh
    • Specify, queue, execute
    • Undo
    • Transactions/Logging
    • Not mentioned:
      • Macros
      • Async
  • Real World Applications
    • GUI/Menus - copy/paste/undo/photoshop
    • Video Games
      • Age of Empires sent commands rather than the game state!
      • It’s not if("B") { jump(); }, if(input[k]) { input[k].invoke() }
        • Great for different contexts, like menus
    • Parallel / Tasks
      • Async/Await
    • Queues / Multi-Step Wizards
      • Queue up the commands, execute all at once
    • Restaurants????
  • How it’s done:
    • Client: Customer
    • Receiver: Waiter
    • I/Command: Order
    • Invoker: Cook
  • Great example on sourcemaking.com
  • What about callbacks?
  • Why not the observer pattern?
  • Challenge for the listeners, program the command pattern - do it “by the book”

Repository Pattern

Why?

  • Testable with isolated data layer
  • Centrally managed access rules and logic
  • Centralized caching strategy
  • Allows you to separate business logic from data access logic
  • Strongly typed entities
  • Business entity associations
  • Can apply a domain model to simplify business logic
  • Decouple business entity from data storage technology - the repository doesn’t expose where it gets its data

What?

  • Sits between the data source and the business layer
    • Maps data from the data source to an entity
    • Persists changes from the entity back to the data source
      • Can use the Unit of Work pattern for complex, multi-step operations
    • Typically utilizes a Data Mapper Pattern, or an ORM such as Entity Framework in .NET

Mediator Pattern

What is it?

  • The mediator pattern defines an object that encapsulates how a set of objects interact
  • Promotes loose coupling by keeping objects from referring to each other explicitly
  • Promotes the Single Responsibility Principle by allowing communication to be offloaded to a class that handles just that.
  • Similar to the Observer pattern
    • Mediator pattern can be implemented during the observer pattern,
    • The Observer pattern distributes communication by introducing “observer” and “subject” objects.
  • Also similar to the Facade pattern in that it abstracts functionality of the classes.

Examples

  • Chat room
  • Air Traffic Control
  • Button events?

Resources We Like

http://gameprogrammingpatterns.com/
https://msdn.microsoft.com/en-us/library/ff649690.aspx?f=255&MSPPError=-2147217396
https://genericunitofworkandrepositories.codeplex.com/
http://blog.falafel.com/implement-step-step-generic-repository-pattern-c/
http://www.gamasutra.com/view/feature/131503/1500_archers_on_a_288_network_.php
https://sourcemaking.com/design_patterns/command
https://sourcemaking.com/design-patterns-book
http://www.codeproject.com/Articles/526874/Repository-pattern-done-right
https://en.wikipedia.org/wiki/Mediator_pattern
https://sourcemaking.com/design_patterns/mediator
http://programmers.stackexchange.com/questions/134432/mediator-vs-observer

Tips of this Episode

Allen:

Want a JavaScript tip per day?!
http://www.jstips.co/

Bonus!!!  Want tons of cheap stuff?!
App called "Geek" - download it on iOS or Android

Michael:

Use Nunit's TestCaseSource to test objects in your test cases

How to delete/forget about a wireless network in Win8.1

  • netsh wlan show profiles
  • netsh wlan delete profile name="gogoinflight"

SOURCE: http://www.digitalcitizen.life/how-delete-forget-wireless-network-profiles-windows-81

Joe:

Learn a new language! Stretch the brain, learn new concepts and see old concepts in a new light! Scripting, Compiled, Functional - Conway’s Game of Life!

Episode source