SlideShare a Scribd company logo
+




Design Patterns for Rubyists
Who Said Dynamic Languages Can’t Have Patterns?
Software Design Trilogy – Part II
Andy Maleh / Software Engineer / Groupon
+
    Outline

     Design   Patterns Pop Quiz

     Practical Applications   of Design Patterns in Ruby

     Alternative   Implementations in Ruby

     Deprecated    Design Patterns in Ruby

     Summary
+
    Design Patterns Pop Quiz

     Who   are the Gang of Four?
     1.   Group of communist party leaders in China
     2.   Rock band
     3.   Bunch of computer programmers
+
    Design Patterns Pop Quiz

     What   is a Design Pattern?
        Reusable solution to a common problem encountered at the
         software design level of abstraction.
+
    Design Patterns Pop Quiz

     What   does a Design Pattern consist of?
        Name
        Problem
        Solution
        Consequences
+
    Practical Applications of Design
    Patterns in Ruby
    Strategy

     Problem: Need to customize behavior with multiple
     variations at run time, and possibly allow clients to
     provide their own customization as well

     Solution:
              Encapsulate each behavior variation in a
     Strategy object
+
    Practical Applications of Design
    Patterns in Ruby - Strategy
     Example:
        Problem: A customer can submit a payment in one of multiple
         methods, such as credit card, cashier’s check, and paypal.
         The code is littered with conditionals that alter behavior per
         payment type, making it quite involved and difficult to add a
         new payment type in the future or maintain existing code
        Solution: Separate each payment type into its own
         PaymentStrategy object and let it handle that payment’s
         details
+
    Practical Applications of Design
    Patterns in Ruby - Strategy
     Code:
+
    Practical Applications of Design
    Patterns in Ruby
    State

     Problem: An abstraction transitions through multiple
     states and behaves differently under each

     Solution:
              Encapsulate behavior variations in multiple
     State objects
+
    Practical Applications of Design
    Patterns in Ruby - State
     Example:
       Problem: An order passes through multiple states
        before it is processed:
         new  unverified  shipping  processed
     It can also be made inactive
     Developers are tired of all the conditionals littering their
     codebase everywhere about the different behaviors
     associated with each state
      Solution: Encapsulate behavior associated with each
        state in its own OrderState object, including the
        transitions
+
    Practical Applications of Design
    Patterns in Ruby - State
     Code:
+
    Practical Applications of Design
    Patterns in Ruby
    Composite

     Problem: An  object may be composed of other
     objects, like chapters consisting of sections in a
     book, yet both parent and child objects share
     common behavior

     Solution:
              Define a child object superclass and a
     parent object superclass. The parent object
     superclass extends the child object superclass to
     share common behavior.
+
    Practical Applications of Design
    Patterns in Ruby - Composite
     Example:
      Problem: An educational website needs to handle
       behavior for storing and formatting a hierarchy of
       information consistently: chapters, sections, and pages
      Solution: Define a Node superclass for all elements and
       a ParentNode superclass for all parent elements
       (chapters and sections)
+
    Practical Applications of Design
    Patterns in Ruby - Composite
     Code:
+
    Practical Applications of Design
    Patterns in Ruby
    Adapter

     Problem: An  existing object (object A) with a
     particular interface needs to interact with another
     object (object B) that requires a different interface

     Solution:
              Create an Adapter object that wraps object
     A to adapt its interface to what is expected of object
     B.
+
    Practical Applications of Design
    Patterns in Ruby - Adapter
     Example:
      Problem: Authentication library supports outside
       strategies with an authenticate method. We have a
       CompanyLogin implementation that has a login method
       instead.
      Solution: Create an CompanyAuthenticationAdapter
       that wraps CompanyLogin and provides an
       authenticate method instead that calls login
+
    Practical Applications of Design
    Patterns in Ruby
    Proxy

     Problem: there is a need to alter access to an object
     for aspect oriented reasons like caching, security, or
     performance, and ensure that this controlled access
     is enforced across the app

     Solution:Create a Proxy object that wraps the
     original object and alters access to it by adding a
     caching, security, or performance related layer
+
    Practical Applications of Design
    Patterns in Ruby - Proxy
     Example:
      Problem: we need to cache data retrieved from a web
       service via a client object
      Solution: Create a Proxy object that wraps the web
       service client and have it cache incoming data
+
    Practical Applications of Design
    Patterns in Ruby
    Decorator

     Problem:an object capabilities need to be enhanced
     without modifying the object

     Solution:Create a Decorator object that wraps the
     original object and adds the extra capabilities
+
    Practical Applications of Design
    Patterns in Ruby - Decorator
     Example:
      Problem: the view needs to render some model
       attributes as is as well as render some formatted
       versions of the model attributes (example a client
       description that includes name and address)
      Solution: Create a Decorator object that wraps the
       model providing its original attribute methods as well as
       extra formatted versions
+
    Practical Applications of Design
    Patterns in Ruby - Decorator
     Code:
+
    Practical Applications of Design
    Patterns in Ruby - Decorator
     Code:
+
    Practical Applications of Design
    Patterns in Ruby
    Mediator

     Problem:  there is a need to decouple multiple
     objects from each other while orchestrating a
     particular part of work

     Solution:introduce a Mediator object that is
     responsible for orchestrating the work between
     multiple objects
+
    Practical Applications of Design
    Patterns in Ruby - Mediator
     Example:
      Problem: work associated with processing an order
       requires orchestration of many objects, such as
       Order, PaymentGateway, AddressService, and has
       gone beyond the scope of what an Order object can
       handle
      Solution: introduce an OrderProcessor mediator object
       that is responsible for processing an order
+
    Practical Applications of Design
    Patterns in Ruby
    Bridge

     Problem: there is a need to implement an
     abstraction in different ways or using different
     libraries

     Solution:decouple abstraction from implementation
     by creating an implementation object separate from
     the abstraction object, which bridges the abstraction
     to the specific implementation
+
    Practical Applications of Design
    Patterns in Ruby - Bridge
     Example:
      Problem: models are coupled to ActiveRecord as their
       storage mechanism. We would like to decouple the
       ActiveRecord implementation to allow switching some
       models to MongoDB easily without affecting controllers
       and higher level models
      Solution: decouple models from their storage
       implementations by introducing a ModelRepository
       object for each Model
+
    Practical Applications of Design
    Patterns in Ruby
    Observer

     Problem:  there is a need to perform work outside an
     object’s responsibilities whenever the object state
     changes

     Solution:
              make the object an Observable object that
     allows subscribing to state notifications, and
     introduce Observer objects that observe the
     Observable and react according to changes
+
    Practical Applications of Design
    Patterns in Ruby - Observer
     Example:
      Problem: there is a need to send an email on every
       order state change and we do not want such specific
       logic to be tied to the order model directly
      Solution: introduce an EmailOrderObserver object that
       monitors Order for state transitions. Make Order an
       observable by having it provide a
       subscribe_to_state_transitions(observer) method to
       allow monitoring its state changes
+
    Practical Applications of Design
    Patterns in Ruby - Observer
     Code:
+
    Practical Applications of Design
    Patterns in Ruby
    Prototype

     Problem: there is a need to build a complex object a
     certain way, with several variations

     Solution:define a Prototype object for each of the
     variations, and implement a clone method that
     enables you to use them to instantiate objects easily
+
    Practical Applications of Design
    Patterns in Ruby - Prototype
     Example:
      Problem: need to create different kinds of User objects
       when writing tests that conform to different common
       roles: User, Admin, Visitor, etc…
      Solution: define each Deal type as a prototype using
       the FactoryGirl library
+
    Practical Applications of Design
    Patterns in Ruby - Prototype
     Code:
+
    Alternative Implementations in Ruby

     Strategy:   enumerate blocks per strategy name

     State:   enumerate blocks per state value

     Decorator:
     method_missing, Forwardable, Delegator, etc…

     Template    Method / Factory Method: abstract library

     Abstract   Factory: abstract library
+
    Deprecated Design Patterns in
    Ruby
     Thereare Design Patterns that have alternative
     implementations in Ruby that render them
     deprecated to an extent
      Command  Blocks
      Iterator  Iterator Methods (each, map, inject, etc…)
+
    Summary

       Design Patterns enable developers to honor the open-closed
        principle to keep their libraries open for extension, yet closed
        for modification

       Design Patterns help improve maintainability when behavior
        gets complex by adding structure that improves separation of
        concerns

       Design Patterns serve as a good design communication tool

       Design Patterns in Ruby often have alternative
        implementations due to language features like blocks and
        duck-typing
+
    Contact Info

     Andy    Maleh / Software Engineer / Groupon

     Blog:   http://andymaleh.blogspot.com

     Twitter:   @AndyMaleh
+
    References

     Design
           Patterns by the Gang of Four (ISBN-13:978-
     0201633610)

     HeadFirst Design Patterns (ISBN-13:978-
     0596007126)

More Related Content

Similar to Software Design Trilogy Part II - Design Patterns for Rubyists

12266422.ppt
12266422.ppt12266422.ppt
12266422.ppt
CSEC5
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
mithunsasidharan
 
Design pattern and their application
Design pattern and their applicationDesign pattern and their application
Design pattern and their application
Hiệp Tiến
 
project_proposal_osrf
project_proposal_osrfproject_proposal_osrf
project_proposal_osrf
om1234567890
 
React.js at Cortex
React.js at CortexReact.js at Cortex
React.js at Cortex
Geoff Harcourt
 
Docs at Weaveworks: DX from open source to SaaS and beyond
Docs at Weaveworks: DX from open source to SaaS and beyondDocs at Weaveworks: DX from open source to SaaS and beyond
Docs at Weaveworks: DX from open source to SaaS and beyond
Luke Marsden
 
Chennai Drupal Meet
Chennai Drupal MeetChennai Drupal Meet
Chennai Drupal Meet
sivaji2009
 
GoF Design patterns I: Introduction + Structural Patterns
GoF Design patterns I:   Introduction + Structural PatternsGoF Design patterns I:   Introduction + Structural Patterns
GoF Design patterns I: Introduction + Structural Patterns
Sameh Deabes
 
Multi-tenancy with Rails
Multi-tenancy with RailsMulti-tenancy with Rails
Multi-tenancy with Rails
Paul Gallagher
 
An Introduction to Domain Driven Design in PHP
An Introduction to Domain Driven Design in PHPAn Introduction to Domain Driven Design in PHP
An Introduction to Domain Driven Design in PHP
Chris Renner
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and Redux
Ali Sa'o
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
danhaley45372
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
Mojammel Haque
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
Prageeth Sandakalum
 
JOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and ReduxJOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and Redux
Jordan Open Source Association
 
Refactoring PHP
Refactoring PHPRefactoring PHP
Refactoring PHP
Adam Culp
 
From ActiveRecord to EventSourcing
From ActiveRecord to EventSourcingFrom ActiveRecord to EventSourcing
From ActiveRecord to EventSourcing
Emanuele DelBono
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
deonpmeyer
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
anguraju1
 
Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"
Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"
Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"
GlobalLogic Ukraine
 

Similar to Software Design Trilogy Part II - Design Patterns for Rubyists (20)

12266422.ppt
12266422.ppt12266422.ppt
12266422.ppt
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Design pattern and their application
Design pattern and their applicationDesign pattern and their application
Design pattern and their application
 
project_proposal_osrf
project_proposal_osrfproject_proposal_osrf
project_proposal_osrf
 
React.js at Cortex
React.js at CortexReact.js at Cortex
React.js at Cortex
 
Docs at Weaveworks: DX from open source to SaaS and beyond
Docs at Weaveworks: DX from open source to SaaS and beyondDocs at Weaveworks: DX from open source to SaaS and beyond
Docs at Weaveworks: DX from open source to SaaS and beyond
 
Chennai Drupal Meet
Chennai Drupal MeetChennai Drupal Meet
Chennai Drupal Meet
 
GoF Design patterns I: Introduction + Structural Patterns
GoF Design patterns I:   Introduction + Structural PatternsGoF Design patterns I:   Introduction + Structural Patterns
GoF Design patterns I: Introduction + Structural Patterns
 
Multi-tenancy with Rails
Multi-tenancy with RailsMulti-tenancy with Rails
Multi-tenancy with Rails
 
An Introduction to Domain Driven Design in PHP
An Introduction to Domain Driven Design in PHPAn Introduction to Domain Driven Design in PHP
An Introduction to Domain Driven Design in PHP
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and Redux
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
 
JOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and ReduxJOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and Redux
 
Refactoring PHP
Refactoring PHPRefactoring PHP
Refactoring PHP
 
From ActiveRecord to EventSourcing
From ActiveRecord to EventSourcingFrom ActiveRecord to EventSourcing
From ActiveRecord to EventSourcing
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"
Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"
Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"
 

More from Andy Maleh

Fukuoka Ruby Award 2023 - Opal
Fukuoka Ruby Award 2023 - OpalFukuoka Ruby Award 2023 - Opal
Fukuoka Ruby Award 2023 - Opal
Andy Maleh
 
Becoming a SOC2 Ruby Shop - Montreal.rb November, 5, 2022 Ruby Meetup
Becoming a SOC2 Ruby Shop - Montreal.rb November, 5, 2022 Ruby MeetupBecoming a SOC2 Ruby Shop - Montreal.rb November, 5, 2022 Ruby Meetup
Becoming a SOC2 Ruby Shop - Montreal.rb November, 5, 2022 Ruby Meetup
Andy Maleh
 
Montreal.rb 2022-10-05 - Glimmer DSL for SWT - Ruby Desktop Development GUI ...
 Montreal.rb 2022-10-05 - Glimmer DSL for SWT - Ruby Desktop Development GUI ... Montreal.rb 2022-10-05 - Glimmer DSL for SWT - Ruby Desktop Development GUI ...
Montreal.rb 2022-10-05 - Glimmer DSL for SWT - Ruby Desktop Development GUI ...
Andy Maleh
 
How I Built My Code Editor in Ruby
How I Built My Code Editor in RubyHow I Built My Code Editor in Ruby
How I Built My Code Editor in Ruby
Andy Maleh
 
Ultra Light and Maintainable Rails Wizards at RailsConf 2014
Ultra Light and Maintainable Rails Wizards at RailsConf 2014Ultra Light and Maintainable Rails Wizards at RailsConf 2014
Ultra Light and Maintainable Rails Wizards at RailsConf 2014
Andy Maleh
 
RailsConf 2014 Recap at Montreal.rb by Andy Maleh
RailsConf 2014 Recap at Montreal.rb by Andy MalehRailsConf 2014 Recap at Montreal.rb by Andy Maleh
RailsConf 2014 Recap at Montreal.rb by Andy Maleh
Andy Maleh
 
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
Andy Maleh
 
Software Craftsmanship VS Software Engineering
Software Craftsmanship VS Software EngineeringSoftware Craftsmanship VS Software Engineering
Software Craftsmanship VS Software Engineering
Andy Maleh
 
Rails Engine Patterns
Rails Engine PatternsRails Engine Patterns
Rails Engine Patterns
Andy Maleh
 
Software Craftsmanship vs Software Engineering (Lightning Talk)
Software Craftsmanship vs Software Engineering (Lightning Talk)Software Craftsmanship vs Software Engineering (Lightning Talk)
Software Craftsmanship vs Software Engineering (Lightning Talk)
Andy Maleh
 
Software Design Trilogy Part III - Domain Driven Design for Ruby on Rails App...
Software Design Trilogy Part III - Domain Driven Design for Ruby on Rails App...Software Design Trilogy Part III - Domain Driven Design for Ruby on Rails App...
Software Design Trilogy Part III - Domain Driven Design for Ruby on Rails App...
Andy Maleh
 
Software Design Trilogy Part I - Responsibility Driven Design for Rubyists
Software Design Trilogy Part I - Responsibility Driven Design for RubyistsSoftware Design Trilogy Part I - Responsibility Driven Design for Rubyists
Software Design Trilogy Part I - Responsibility Driven Design for Rubyists
Andy Maleh
 
The Rails Engine That Could - In Motion
The Rails Engine That Could - In MotionThe Rails Engine That Could - In Motion
The Rails Engine That Could - In Motion
Andy Maleh
 
The Rails Engine That Could
The Rails Engine That CouldThe Rails Engine That Could
The Rails Engine That Could
Andy Maleh
 
How I Learned To Apply Design Patterns
How I Learned To Apply Design PatternsHow I Learned To Apply Design Patterns
How I Learned To Apply Design Patterns
Andy Maleh
 
Simplifying Desktop Development With Glimmer
Simplifying Desktop Development With GlimmerSimplifying Desktop Development With Glimmer
Simplifying Desktop Development With Glimmer
Andy Maleh
 

More from Andy Maleh (16)

Fukuoka Ruby Award 2023 - Opal
Fukuoka Ruby Award 2023 - OpalFukuoka Ruby Award 2023 - Opal
Fukuoka Ruby Award 2023 - Opal
 
Becoming a SOC2 Ruby Shop - Montreal.rb November, 5, 2022 Ruby Meetup
Becoming a SOC2 Ruby Shop - Montreal.rb November, 5, 2022 Ruby MeetupBecoming a SOC2 Ruby Shop - Montreal.rb November, 5, 2022 Ruby Meetup
Becoming a SOC2 Ruby Shop - Montreal.rb November, 5, 2022 Ruby Meetup
 
Montreal.rb 2022-10-05 - Glimmer DSL for SWT - Ruby Desktop Development GUI ...
 Montreal.rb 2022-10-05 - Glimmer DSL for SWT - Ruby Desktop Development GUI ... Montreal.rb 2022-10-05 - Glimmer DSL for SWT - Ruby Desktop Development GUI ...
Montreal.rb 2022-10-05 - Glimmer DSL for SWT - Ruby Desktop Development GUI ...
 
How I Built My Code Editor in Ruby
How I Built My Code Editor in RubyHow I Built My Code Editor in Ruby
How I Built My Code Editor in Ruby
 
Ultra Light and Maintainable Rails Wizards at RailsConf 2014
Ultra Light and Maintainable Rails Wizards at RailsConf 2014Ultra Light and Maintainable Rails Wizards at RailsConf 2014
Ultra Light and Maintainable Rails Wizards at RailsConf 2014
 
RailsConf 2014 Recap at Montreal.rb by Andy Maleh
RailsConf 2014 Recap at Montreal.rb by Andy MalehRailsConf 2014 Recap at Montreal.rb by Andy Maleh
RailsConf 2014 Recap at Montreal.rb by Andy Maleh
 
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
Revised Rails Engine Patterns for Montreal.rb meetup Oct 16, 2012
 
Software Craftsmanship VS Software Engineering
Software Craftsmanship VS Software EngineeringSoftware Craftsmanship VS Software Engineering
Software Craftsmanship VS Software Engineering
 
Rails Engine Patterns
Rails Engine PatternsRails Engine Patterns
Rails Engine Patterns
 
Software Craftsmanship vs Software Engineering (Lightning Talk)
Software Craftsmanship vs Software Engineering (Lightning Talk)Software Craftsmanship vs Software Engineering (Lightning Talk)
Software Craftsmanship vs Software Engineering (Lightning Talk)
 
Software Design Trilogy Part III - Domain Driven Design for Ruby on Rails App...
Software Design Trilogy Part III - Domain Driven Design for Ruby on Rails App...Software Design Trilogy Part III - Domain Driven Design for Ruby on Rails App...
Software Design Trilogy Part III - Domain Driven Design for Ruby on Rails App...
 
Software Design Trilogy Part I - Responsibility Driven Design for Rubyists
Software Design Trilogy Part I - Responsibility Driven Design for RubyistsSoftware Design Trilogy Part I - Responsibility Driven Design for Rubyists
Software Design Trilogy Part I - Responsibility Driven Design for Rubyists
 
The Rails Engine That Could - In Motion
The Rails Engine That Could - In MotionThe Rails Engine That Could - In Motion
The Rails Engine That Could - In Motion
 
The Rails Engine That Could
The Rails Engine That CouldThe Rails Engine That Could
The Rails Engine That Could
 
How I Learned To Apply Design Patterns
How I Learned To Apply Design PatternsHow I Learned To Apply Design Patterns
How I Learned To Apply Design Patterns
 
Simplifying Desktop Development With Glimmer
Simplifying Desktop Development With GlimmerSimplifying Desktop Development With Glimmer
Simplifying Desktop Development With Glimmer
 

Recently uploaded

HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
Edge AI and Vision Alliance
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
Hiike
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
Intelisync
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 

Recently uploaded (20)

HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 

Software Design Trilogy Part II - Design Patterns for Rubyists

  • 1. + Design Patterns for Rubyists Who Said Dynamic Languages Can’t Have Patterns? Software Design Trilogy – Part II Andy Maleh / Software Engineer / Groupon
  • 2. + Outline  Design Patterns Pop Quiz  Practical Applications of Design Patterns in Ruby  Alternative Implementations in Ruby  Deprecated Design Patterns in Ruby  Summary
  • 3. + Design Patterns Pop Quiz  Who are the Gang of Four? 1. Group of communist party leaders in China 2. Rock band 3. Bunch of computer programmers
  • 4. + Design Patterns Pop Quiz  What is a Design Pattern?  Reusable solution to a common problem encountered at the software design level of abstraction.
  • 5. + Design Patterns Pop Quiz  What does a Design Pattern consist of?  Name  Problem  Solution  Consequences
  • 6. + Practical Applications of Design Patterns in Ruby Strategy  Problem: Need to customize behavior with multiple variations at run time, and possibly allow clients to provide their own customization as well  Solution: Encapsulate each behavior variation in a Strategy object
  • 7. + Practical Applications of Design Patterns in Ruby - Strategy  Example:  Problem: A customer can submit a payment in one of multiple methods, such as credit card, cashier’s check, and paypal. The code is littered with conditionals that alter behavior per payment type, making it quite involved and difficult to add a new payment type in the future or maintain existing code  Solution: Separate each payment type into its own PaymentStrategy object and let it handle that payment’s details
  • 8. + Practical Applications of Design Patterns in Ruby - Strategy  Code:
  • 9. + Practical Applications of Design Patterns in Ruby State  Problem: An abstraction transitions through multiple states and behaves differently under each  Solution: Encapsulate behavior variations in multiple State objects
  • 10. + Practical Applications of Design Patterns in Ruby - State  Example:  Problem: An order passes through multiple states before it is processed:  new  unverified  shipping  processed It can also be made inactive Developers are tired of all the conditionals littering their codebase everywhere about the different behaviors associated with each state  Solution: Encapsulate behavior associated with each state in its own OrderState object, including the transitions
  • 11. + Practical Applications of Design Patterns in Ruby - State  Code:
  • 12. + Practical Applications of Design Patterns in Ruby Composite  Problem: An object may be composed of other objects, like chapters consisting of sections in a book, yet both parent and child objects share common behavior  Solution: Define a child object superclass and a parent object superclass. The parent object superclass extends the child object superclass to share common behavior.
  • 13. + Practical Applications of Design Patterns in Ruby - Composite  Example:  Problem: An educational website needs to handle behavior for storing and formatting a hierarchy of information consistently: chapters, sections, and pages  Solution: Define a Node superclass for all elements and a ParentNode superclass for all parent elements (chapters and sections)
  • 14. + Practical Applications of Design Patterns in Ruby - Composite  Code:
  • 15. + Practical Applications of Design Patterns in Ruby Adapter  Problem: An existing object (object A) with a particular interface needs to interact with another object (object B) that requires a different interface  Solution: Create an Adapter object that wraps object A to adapt its interface to what is expected of object B.
  • 16. + Practical Applications of Design Patterns in Ruby - Adapter  Example:  Problem: Authentication library supports outside strategies with an authenticate method. We have a CompanyLogin implementation that has a login method instead.  Solution: Create an CompanyAuthenticationAdapter that wraps CompanyLogin and provides an authenticate method instead that calls login
  • 17. + Practical Applications of Design Patterns in Ruby Proxy  Problem: there is a need to alter access to an object for aspect oriented reasons like caching, security, or performance, and ensure that this controlled access is enforced across the app  Solution:Create a Proxy object that wraps the original object and alters access to it by adding a caching, security, or performance related layer
  • 18. + Practical Applications of Design Patterns in Ruby - Proxy  Example:  Problem: we need to cache data retrieved from a web service via a client object  Solution: Create a Proxy object that wraps the web service client and have it cache incoming data
  • 19. + Practical Applications of Design Patterns in Ruby Decorator  Problem:an object capabilities need to be enhanced without modifying the object  Solution:Create a Decorator object that wraps the original object and adds the extra capabilities
  • 20. + Practical Applications of Design Patterns in Ruby - Decorator  Example:  Problem: the view needs to render some model attributes as is as well as render some formatted versions of the model attributes (example a client description that includes name and address)  Solution: Create a Decorator object that wraps the model providing its original attribute methods as well as extra formatted versions
  • 21. + Practical Applications of Design Patterns in Ruby - Decorator  Code:
  • 22. + Practical Applications of Design Patterns in Ruby - Decorator  Code:
  • 23. + Practical Applications of Design Patterns in Ruby Mediator  Problem: there is a need to decouple multiple objects from each other while orchestrating a particular part of work  Solution:introduce a Mediator object that is responsible for orchestrating the work between multiple objects
  • 24. + Practical Applications of Design Patterns in Ruby - Mediator  Example:  Problem: work associated with processing an order requires orchestration of many objects, such as Order, PaymentGateway, AddressService, and has gone beyond the scope of what an Order object can handle  Solution: introduce an OrderProcessor mediator object that is responsible for processing an order
  • 25. + Practical Applications of Design Patterns in Ruby Bridge  Problem: there is a need to implement an abstraction in different ways or using different libraries  Solution:decouple abstraction from implementation by creating an implementation object separate from the abstraction object, which bridges the abstraction to the specific implementation
  • 26. + Practical Applications of Design Patterns in Ruby - Bridge  Example:  Problem: models are coupled to ActiveRecord as their storage mechanism. We would like to decouple the ActiveRecord implementation to allow switching some models to MongoDB easily without affecting controllers and higher level models  Solution: decouple models from their storage implementations by introducing a ModelRepository object for each Model
  • 27. + Practical Applications of Design Patterns in Ruby Observer  Problem: there is a need to perform work outside an object’s responsibilities whenever the object state changes  Solution: make the object an Observable object that allows subscribing to state notifications, and introduce Observer objects that observe the Observable and react according to changes
  • 28. + Practical Applications of Design Patterns in Ruby - Observer  Example:  Problem: there is a need to send an email on every order state change and we do not want such specific logic to be tied to the order model directly  Solution: introduce an EmailOrderObserver object that monitors Order for state transitions. Make Order an observable by having it provide a subscribe_to_state_transitions(observer) method to allow monitoring its state changes
  • 29. + Practical Applications of Design Patterns in Ruby - Observer  Code:
  • 30. + Practical Applications of Design Patterns in Ruby Prototype  Problem: there is a need to build a complex object a certain way, with several variations  Solution:define a Prototype object for each of the variations, and implement a clone method that enables you to use them to instantiate objects easily
  • 31. + Practical Applications of Design Patterns in Ruby - Prototype  Example:  Problem: need to create different kinds of User objects when writing tests that conform to different common roles: User, Admin, Visitor, etc…  Solution: define each Deal type as a prototype using the FactoryGirl library
  • 32. + Practical Applications of Design Patterns in Ruby - Prototype  Code:
  • 33. + Alternative Implementations in Ruby  Strategy: enumerate blocks per strategy name  State: enumerate blocks per state value  Decorator: method_missing, Forwardable, Delegator, etc…  Template Method / Factory Method: abstract library  Abstract Factory: abstract library
  • 34. + Deprecated Design Patterns in Ruby  Thereare Design Patterns that have alternative implementations in Ruby that render them deprecated to an extent  Command  Blocks  Iterator  Iterator Methods (each, map, inject, etc…)
  • 35. + Summary  Design Patterns enable developers to honor the open-closed principle to keep their libraries open for extension, yet closed for modification  Design Patterns help improve maintainability when behavior gets complex by adding structure that improves separation of concerns  Design Patterns serve as a good design communication tool  Design Patterns in Ruby often have alternative implementations due to language features like blocks and duck-typing
  • 36. + Contact Info  Andy Maleh / Software Engineer / Groupon  Blog: http://andymaleh.blogspot.com  Twitter: @AndyMaleh
  • 37. + References  Design Patterns by the Gang of Four (ISBN-13:978- 0201633610)  HeadFirst Design Patterns (ISBN-13:978- 0596007126)