PRDCW-advanced-design-patterns

Post on 31-Oct-2014

582 views 0 download

Tags:

description

Presentation done at PRDC west about design patterns and anti patterns

transcript

Amir Barylko Advanced Design Patterns

AMIR BARYLKO

ADVANCEDDESIGN PATTERNS

Amir 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/presentations

Amir Barylko Advanced Design Patterns

PATTERNSWhat 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 language

Amir 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 PATTERNSLet’s vote!

Amir Barylko Advanced Design Patterns

SOME PATTERNS...

• Chain of resp. 18

• Proxy 1

• ActiveRecord 12

• Repository 5

• Event Aggregator 26

• Event Sourcing 22

• List Comprehension 1

•Object Mother10

• Visitor 8

•Null Object

• Factory 4

• Strategy 11

•DTO 4

• Page Object 16

Amir Barylko Advanced Design Patterns

CHAIN OF RESPONSIBILITY

•More than one object may handle a request, and the handler isn't 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 dynamically

Amir Barylko Advanced Design Patterns

Amir Barylko Advanced Design Patterns

SnapToX

SnapToY

SnapToPrevious

Next

Next

Amir 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

GOF

Amir Barylko Advanced Design Patterns

PROXY SEQUENCE

Amir Barylko Advanced Design Patterns

ACTIVERECORD

• Is a 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 classes

Amir 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 :reviewsend

class Review < ActiveRecord::Base belongs_to :movieend

Amir Barylko Advanced Design Patterns

movie = Movie.new

movie.all # all records

# filter by titlemovie.where(title: 'Spaceballs')

# finds by attributemovie.find_by_title('Blazing Saddles')

# order, skip some and limit the resultmovie.order('title DESC').skip(2).limit(5)

# associations CRUDmovie.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 storage

Amir Barylko Advanced Design Patterns

Amir Barylko Advanced Design Patterns

http://martinfowler.com/eaaCatalog/repository.html

Amir Barylko Advanced Design Patterns

ANTIPATTERNCHEAPER BY THE DOZEN

Amir 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 factory

Amir 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 objects

Amir Barylko Advanced Design Patterns

Channel events from multiple o b j e c t s i n t o a single object to s i m p l i f y registration for clients

Amir Barylko Advanced Design Patterns

MT COMMONS

Amir Barylko Advanced Design Patterns

COUPLED

Amir Barylko Advanced Design Patterns

DECOUPLED

Amir Barylko Advanced Design Patterns

EVENT SOURCING

• Register all changes in the application using events

• Event should be persisted

• Complete Rebuild

• Temporal Query

• Event Replay

Amir Barylko Advanced Design Patterns

http://martinfowler.com/eaaDev/EventSourcing.html

Amir 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 comprehension

Amir Barylko Advanced Design Patterns

• Scalafor (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 }

LANGUAGE COMPARISON

Amir Barylko Advanced Design Patterns

OBJECT MOTHER / BUILDER

• Creates an object for testing (or other) purposes

• Assumes defaults

• Easy to configure

• Fluent interface

• Usually has methods to to easily manipulate the domain

Amir 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 operations

Amir Barylko Advanced Design Patterns

LINQ EXPRESSION TREE

Amir Barylko Advanced Design Patterns

EXPRESSION VISITOR

Amir Barylko Advanced Design Patterns

AND EXPR EVALUATION

Amir Barylko Advanced Design Patterns

NULL OBJECT

• Represent “null” with an actual instance

• Provides default functionality

• Clear semantics of “null” for that domain

Amir 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_pattern

Amir 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 needed

Amir 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_pattern

Amir Barylko Advanced Design Patterns

STRATEGY

• Abstracts the algorithm to solve a particular problem

• Can be configured dynamically

• Are interchangeable

Amir 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 understand

Amir Barylko Advanced Design Patterns

http://martinfowler.com/eaaCatalog/dataTransferObject.html

Amir 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 clients

Amir 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")...... endend

Amir Barylko Advanced Design Patterns

When /^I go to the projects page$/ do project_list_page.navigateend

When /^I activate the project$/ do project_list_page. navigate. edit(current_project). activate. saveend

Amir 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 link

Amir Barylko Advanced Design Patterns

RESOURCES II

Amir Barylko Advanced Design Patterns

RESOURCES III

Amir Barylko Advanced Design Patterns

QUALITY SOFTWARETRAINING

• Topics: Kanban, BDD & TDD.

•When: May 4, 10-11 & 16-17

•More info: http://www.maventhought.com

• Goal: Learn techniques, methodologies and tools to improve the quality in your job.