• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
PRDC12 advanced design patterns
 

PRDC12 advanced design patterns

on

  • 1,444 views

Talk about common used design patterns and anti-patterns

Talk about common used design patterns and anti-patterns

Statistics

Views

Total Views
1,444
Views on SlideShare
1,444
Embed Views
0

Actions

Likes
0
Downloads
11
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    PRDC12 advanced design patterns PRDC12 advanced design patterns Presentation Transcript

    • Advanced Design Patterns & Anti-Patterns with Amir BarylkoAmir Barylko Advanced Design Patterns
    • WHO AM I? • Software quality expert • Architect • Developer • Mentor • Great cook • The one who’s entertaining you for the next hour!Amir Barylko Advanced Design Patterns
    • RESOURCES • Email: amir@barylko.com • Twitter: @abarylko • Blog: http://www.orthocoders.com • Materials: http://www.orthocoders.com/presentationsAmir Barylko Advanced Design Patterns
    • PATTERNS What are they? What are anti-patterns? Which patterns do you use?Amir Barylko Advanced Design Patterns
    • WHAT ARE PATTERNS? •Software design Recipe •or Solution •Should be reusable •Should be general •No particular languageAmir Barylko Advanced Design Patterns
    • ANTI-PATTERNS • More patterns != better design • No cookie cutter • Anti Patterns : Patterns to identify failure • God Classes • High Coupling • Breaking SOLID principles.... • (name some)Amir Barylko Advanced Design Patterns
    • WHICH PATTERNS DO YOU USE? • Fill here with your patterns:Amir Barylko Advanced Design Patterns
    • ADVANCED PATTERNS Let’s vote!Amir Barylko Advanced Design Patterns
    • SOME PATTERNS... • MVVM • Event Aggregator • Factory • Chain of resp. • Event Sourcing • Strategy • Proxy • Test Factory • DTO • ActiveRecord • Visitor • Page Object • Repository • Null ObjectAmir Barylko Advanced Design Patterns
    • MODEL VIEW VIEW MODEL • Separate presentation from business logic (view from model) • Around the family of MVC, MVP, etc... • Most useful when binding is available • Many JS libraries uses it (Knockout.js, Ember.js, etc...) • WPF it is based on MVVMAmir Barylko Advanced Design Patterns
    • THE MODEL • Represents data • Could be a one or multiple objects • Feeds the view model • However, does not know the view or the view modelAmir Barylko Advanced Design Patterns
    • THE VIEW • Represents what is shown to the user • Desktop window • HTML Page • Does not know the model • Loosely coupled to the the view model, binding does all the jobAmir Barylko Advanced Design Patterns
    • THE VIEW MODEL • Represents all the data that the view needs • It could be constructed based on on multiple models • Communicate events to update the view using binding • Complex UI may require composition of MVVMAmir Barylko Advanced Design Patterns
    • WHAT IS BINDING? • Binding connects a model and a UI that represents that model • In order to update the UI and the model to match • Native to the framework or library • Very related to Subject and Observer • Also could be an MVC implementationAmir Barylko Advanced Design Patterns
    • FOR EACH BINDING function AppViewModel() { var self = this; self.people { name: = ko.observableArray([ Bert }, ViewModel { name: Charles }, { name: Denise } ]); self.addPerson = function() { self.people.push({ name: "New at " + new Date() }); }; self.removePerson = function() { self.people.remove(this); } } http://knockoutjs.com/documentation/foreach-binding.htmlAmir Barylko Advanced Design Patterns
    • FOR EACH BINDING List binding <h4>People</h4> <ul data-bind="foreach: people"> <li> Name at position <span data-bind="text: $index"> </span>: <span data-bind="text: name"> </span> <a href="#" data-bind="click: $parent.removePerson">Remove</a> </li> </ul> Data binding <button data-bind="click: addPerson">Add</button> Command bindingAmir Barylko Advanced Design Patterns
    • THE MAGIC ko.applyBindings(new AppViewModel()); ViewModel View Adding element Adds <li> Remove element Removes <li> Execute function Click <button>Amir Barylko Advanced Design Patterns
    • CHAIN OF RESPONSIBILITY • More than one object may handle a request, and the handler isnt known a priori. • The handler should be ascertained automatically. • You want to issue request to one of several objects without specifying The receiver explicitly. • The set of objects that can handle a request should be specified dynamicallyAmir Barylko Advanced Design Patterns
    • Amir Barylko Advanced Design Patterns
    • SnapToX Next SnapToY SnapToPrevious NextAmir Barylko Advanced Design Patterns
    • Amir Barylko Advanced Design Patterns
    • PROXY • Avoid creating the object until needed • Provides a placeholder for additional functionality • Very useful for mocking • Many implementations exist (IoC, Dynamic proxies, etc)Amir Barylko Advanced Design Patterns
    • GOFAmir Barylko Advanced Design Patterns
    • PROXY SEQUENCEAmir Barylko Advanced Design Patterns
    • ACTIVERECORD • Isa Domain Model where classes match very closely the database structure • Each table is mapped to class with methods for finding, update, delete, etc. • Each attribute is mapped to a column • Associations are deduced from the classesAmir Barylko Advanced Design Patterns
    • create_table "movies", :force => true do |t| t.string "title" t.string "description" t.datetime "created_at" t.datetime "updated_at" end create_table "reviews", :force => true do |t| t.string "name" t.integer "stars" t.text "comment" t.integer "movie_id" t.datetime "created_at" t.datetime "updated_at" end class Movie < ActiveRecord::Base validates_presence_of :title, :description has_many :reviews end class Review < ActiveRecord::Base belongs_to :movie endAmir Barylko Advanced Design Patterns
    • movie = Movie.new movie.all # all records # filter by title movie.where(title: Spaceballs) # finds by attribute movie.find_by_title(Blazing Saddles) # order, skip some and limit the result movie.order(title DESC).skip(2).limit(5) # associations CRUD movie.reviews.create(name: Jay Sherman, stars: 1, comment: It stinks!)Amir Barylko Advanced Design Patterns
    • REPOSITORY • Mediator between domain and storage • Acts like a collection of items • Supports queries • Abstraction of the storageAmir Barylko Advanced Design Patterns
    • Amir Barylko Advanced Design Patterns
    • http://martinfowler.com/eaaCatalog/repository.htmlAmir Barylko Advanced Design Patterns
    • ANTIPATTERN CHEAPER BY THE DOZENAmir Barylko Advanced Design Patterns
    • WHAT TO DO? • Use a criteria or better a queryable result (LINQ) • Use a factory to return repositories • Use a UnitOfWork with a factoryAmir Barylko Advanced Design Patterns
    • EVENT AGGREGATOR • Manage events using a subscribe / publish mechanism • Isolates subscribers from publishers • Decouple events from actual models • Events can be distributed • Centralize event registration logic • No need to track multiple objectsAmir Barylko Advanced Design Patterns
    • Channel events from multiple objects into a single object to s i m p l i f y registration for clientsAmir Barylko Advanced Design Patterns
    • MT COMMONSAmir Barylko Advanced Design Patterns
    • COUPLEDAmir Barylko Advanced Design Patterns
    • DECOUPLEDAmir Barylko Advanced Design Patterns
    • EVENT SOURCING • Register all changes in the application using events • Event should be persisted • Complete Rebuild • Temporal Query • Event ReplayAmir Barylko Advanced Design Patterns
    • http://martinfowler.com/eaaDev/EventSourcing.htmlAmir Barylko Advanced Design Patterns
    • Amir Barylko Advanced Design Patterns
    • LIST COMPREHENSION • Syntax Construct in languages • Describe properties for the list (sequence) • Filter • Mapping • Same idea for Set or Dictionary comprehensionAmir Barylko Advanced Design Patterns
    • LANGUAGE COMPARISON • Scala for (x <- Stream.from(0); if x*x > 3) yield 2*x • LINQ var range = Enumerable.Range(0..20); from num in range where num * num > 3 select num * 2; • Clojure (take 20 (for [x (iterate inc 0) :when (> (* x x) 3)] (* 2 x))) • Ruby (1..20).select { |x| x * x > 3 }.map { |x| x * 2 }Amir Barylko Advanced Design Patterns
    • TEST FACTORY / BUILDER • Creates an object for testing (or other) purposes • Assumes defaults • Easy to configure • Fluent interface • Usually has methods to to easily manipulate the domainAmir Barylko Advanced Design Patterns
    • public class When_adding_a_an_invalid_extra_frame { [Test] public void Should_throw_an_exception() { // arrange 10.Times(() => this.GameBuilder.AddFrame(5, 4)); var game = this.GameBuilder.Build(); // act & assert new Action(() => game.Roll(8)).Should().Throw(); } } http://orthocoders.com/2011/09/05/the-bowling-game-kata-first-attempt/Amir Barylko Advanced Design Patterns
    • Amir Barylko Advanced Design Patterns
    • VISITOR • Ability to traverse (visit) a object structure • Different visitors may produce different results • Avoid littering the classes with particular operationsAmir Barylko Advanced Design Patterns
    • LINQ EXPRESSION TREEAmir Barylko Advanced Design Patterns
    • EXPRESSION VISITORAmir Barylko Advanced Design Patterns
    • AND EXPR EVALUATIONAmir Barylko Advanced Design Patterns
    • NULL OBJECT • Represent “null” with an actual instance • Provides default functionality • Clear semantics of “null” for that domainAmir Barylko Advanced Design Patterns
    • class animal { public: virtual void make_sound() = 0; }; class dog : public animal { void make_sound() { cout << "woof!" << endl; } }; class null_animal : public animal { void make_sound() { } }; http://en.wikipedia.org/wiki/Null_Object_patternAmir Barylko Advanced Design Patterns
    • FACTORY • Creates instances by request • More flexible than Singleton • Can be configured to create different families of objects • IoC containers are closely related • Can be implemented dynamic based on interfaces • Can be used also to release “resource” when not neededAmir Barylko Advanced Design Patterns
    • interface GUIFactory { public Button createButton(); } class WinFactory implements GUIFactory { public Button createButton() { return new WinButton(); } } class OSXFactory implements GUIFactory { public Button createButton() { return new OSXButton(); } } interface Button { public void paint(); } http://en.wikipedia.org/wiki/Abstract_factory_patternAmir Barylko Advanced Design Patterns
    • STRATEGY • Abstracts the algorithm to solve a particular problem • Can be configured dynamically • Are interchangeableAmir Barylko Advanced Design Patterns
    • http://orthocoders.com/2010/04/Amir Barylko Advanced Design Patterns
    • DATA TRANSFER OBJECT • Simplifies information transfer across services • Can be optimized • Easy to understandAmir Barylko Advanced Design Patterns
    • http://martinfowler.com/eaaCatalog/dataTransferObject.htmlAmir Barylko Advanced Design Patterns
    • PAGE OBJECT • Abstract web pages functionality to be used usually in testing • Each page can be reused • Changes in the page impact only the implementation, not the clientsAmir Barylko Advanced Design Patterns
    • class ProjectListPage include PageObject def navigate visit(/projects) self end def edit(project) row = find(:xpath, "//td[.=#{project.name}]") row.parent.click_link(Edit) ProjectEditPage.new end def projects all(:css, "#projects tr")...... end endAmir Barylko Advanced Design Patterns
    • When /^I go to the projects page$/ do project_list_page.navigate end When /^I activate the project$/ do project_list_page. navigate. edit(current_project). activate. save endAmir Barylko Advanced Design Patterns
    • QUESTIONS?Amir Barylko Advanced Design Patterns
    • RESOURCES • Email: amir@barylko.com, @abarylko • Slides: http://www.orthocoders.com/presentations • Patterns: Each pattern example has a linkAmir Barylko Advanced Design Patterns
    • RESOURCES IIAmir Barylko Advanced Design Patterns
    • RESOURCES IIIAmir Barylko Advanced Design Patterns