DEV Community

CodingBlocks

Episode 42 – 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 and take the survey here:
http://www.codingblocks.net/epsiode42

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

Come join us Slackers!
http://www.codingblocks.net/slack

#yop-poll-container-17_yp57bd1239bd7b5 { width: 200px; background:#fff; padding:10px; color:#555; overflow:hidden; font-size:12px; } #yop-poll-container-17_yp57bd1239bd7b5 input[type='text'] { margin:0px 0px 5px 0px; padding:2%; width:96%; text-indent:2%; font-size:12px; } .yop-poll-name-17_yp57bd1239bd7b5 { font-size:14px; font-weight:bold; } #yop-poll-questions-container-17_yp57bd1239bd7b5 { font-size:14px; margin:5px 0px; } .yop-poll-question-container-17_yp57bd1239bd7b5 { padding: 2px; } .yop-poll-question-17_yp57bd1239bd7b5 { font-style: italic; text-align: center; margin-bottom: 21px; margin-top: -10px; width: 100%; } .yop-poll-answers-17_yp57bd1239bd7b5 { } .yop-poll-answers-17_yp57bd1239bd7b5 ul { list-style: none outside none; margin: 0; padding: 0; } .yop-poll-li-answer-17_yp57bd1239bd7b5 { font-style:normal; margin:0px 0px 10px 0px; padding:0px; font-size:12px; margin-bottom:20px; margin-bottom:20px; } .yop-poll-li-answer-17_yp57bd1239bd7b5 input { margin:0px; float:none; } .yop-poll-li-answer-17_yp57bd1239bd7b5 label { margin:0px; font-style:normal; font-weight:normal; font-size:12px; float:none; } .yop-poll-results-17_yp57bd1239bd7b5 { font-size: 12px; font-style: italic; font-weight: normal; margin-left: 15px; } .yop-poll-customs-17_yp57bd1239bd7b5 { } .yop-poll-customs-17_yp57bd1239bd7b5 ul { list-style: none outside none; margin: 0; padding: 0; } .yop-poll-li-custom-17_yp57bd1239bd7b5 { padding:0px; margin:0px; font-size:14px; } /* Start CAPTCHA div style*/ #yop-poll-captcha-input-div-17_yp57bd1239bd7b5 { margin-top:5px; } #yop-poll-captcha-helpers-div-17_yp57bd1239bd7b5 { width:30px; float:left; margin-left:5px; height:0px; } #yop-poll-captcha-helpers-div-17_yp57bd1239bd7b5 img { margin-bottom:2px; } #yop-poll-captcha-image-div-17_yp57bd1239bd7b5 { margin-bottom:5px; } #yop_poll_captcha_image_17_yp57bd1239bd7b5 { float:left; } /* End CAPTCHA div style*/ .yop-poll-clear-17_yp57bd1239bd7b5 { clear:both; } #yop-poll-vote-17_yp57bd1239bd7b5 { } /* Start Result bar*/ .yop-poll-results-bar-17_yp57bd1239bd7b5 { background:#f5f5f5; height:10px; } .yop-poll-results-bar-17_yp57bd1239bd7b5 div { background:#555; height:10px; } /* End Result bar*/ /* Start Vote Button*/ #yop-poll-vote-17_yp57bd1239bd7b5 div#yop-poll-vote-17_yp57bd1239bd7b5 button { float:left; } #yop-poll-vote-17_yp57bd1239bd7b5 div#yop-poll-results-17_yp57bd1239bd7b5 { float: right; margin-bottom: 20px; margin-top: -20px; width: auto; } #yop-poll-vote-17_yp57bd1239bd7b5 div#yop-poll-results-17_yp57bd1239bd7b5 a { color:#555; text-decoration:underline; font-size:12px; } #yop-poll-vote-17_yp57bd1239bd7b5 div#yop-poll-back-17_yp57bd1239bd7b5 a { color:#555; text-decoration:underline; font-size:12px; }#yop-poll-vote-17_yp57bd1239bd7b5 div#yop-poll-archive-17_yp57bd1239bd7b5 a { color:#555; text-decoration:underline; font-size:12px; } #yop-poll-vote-17_yp57bd1239bd7b5 div { float:left; width:100%; } /* End Vote Button*/ /* Start Messages*/ #yop-poll-container-error-17_yp57bd1239bd7b5 { font-size:12px; font-style:italic; color:red; text-transform:lowercase; margin-bottom:20px; text-align:center; } #yop-poll-container-success-17_yp57bd1239bd7b5 { font-size:12px; font-style:italic; color:green; margin-bottom:20px; text-align:center; } /* End Messages*/ .yop-poll-clear-17_yp57bd1239bd7b5 { clear: both; }#yop-poll-container-17_yp57bd1239bd7b5 img { max-width: 200px; } .yop-poll-forms-display{}
What type of development do you prefer?
  • Front End / UI
  • Middle Tier / Server
  • Backend / Database
  • Full Stack

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

The "gang of four" book

The “gang of four” book

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