Agile Saturday VI




        Choose a pattern
         for a problem!


Anton Keks
anton@codeborne.com
The Increasing Complexity

Complexity of software systems
                                    Time

New programming paradigms
Higher level of abstraction
HW Assembler Structural   (objects+functions?)
  CPUs  Procedural    <OOP>                ...
What are design patterns?
●
    Anywhere in the world you can find recurring
    patterns
●   Design patterns
    –   are building blocks of a system's
        architecture
    –   are recurring solutions to design problems
        that you see over
    –   identify and specify abstractions that are
        above the level of single classes and
        instances
Pattern classification
The GOF book defines 3 major types of patterns:
–   Creational patterns – substitute the new keyword. This
    gives your program more flexibility in deciding which
    objects need to be created for a given case
–   Structural patterns - compose groups of objects into
    larger structures, such as complex user interfaces or
    accounting data
–   Behavioral patterns - define the communication
    between objects and how the flow is controlled in a
    complex program.
Decorator
Adapter
 Composite      Strategy
                 State
                   Observer
     Factory
                Iterator
  Builder
    Prototype
Software Design: The Good
●
    Loose coupling
●
    Immutability
●
    Encapsulation
●   Short code blocks
●   Appropriate typing
●
    Duplication avoidance
●   Simplicity
●
    Consistent terminology
●   Clear naming
Bad, bad, bad!
●
    Loose coupling                       Tight coupling
●
    Immutability                Mutable data structures
●
    Encapsulation                          Tramp data
●   Short code blocks        Monster functions/methods
●   Appropriate typing           String/Integer overuse
●
    Duplication avoidance                   Copy-Paste
●   Simplicity                        Over-engineering
●
    Consistent terminology            Mixture of terms
●   Clear naming                     Runtime surprises
Let's get to business!
●   Next slides describe problems
       – What is given
          (existing code)
       – What we want to achieve
          (using a pattern)

●
    As usual, the code is on github:
       http://github.com/angryziber/patterns
Problem 1: Pizza
●
    We have:
    –   A pizzeria
    –   Multitude of options for pizzas: name, toppings,
        thickness, size, delivery address, etc
●   We want:
    –   Allow users to “order” custom pizzas
    –   Disallow changing the order once the pizza is
        already in the oven
    –   Keep the ordering code readable
Problem 2: Unzipper
●
    We have:
    –   unzip() method
    –   It is able to decompress a single compressed
        stream
●   We need:
    –   Support multiple-volume archives
    –   Compressed data is split into multiple floppies
Problem 3: Maps
●
    We have:
    –   Map data of entire Tallinn with all the streets,
        buildings, etc
    –   The MapVisualizer class that can draw the full map
        on the screen
●
    We want:
    –   User to see only a section of the map at any given
        time
    –   User to be able to move around the map
Problem 4: CD Player
●
    We have:
    –   A CD player with buttons play, stop, next, eject
●
    We want:
    –   Play to switch the player on
    –   Play again to pause playback, again to resume
    –   Stop to turn off the player
    –   Eject to open the disk tray (if no disk – no playback
        possible)
    –   etc, etc, etc
Problem 5: Vending Machine
●
    We have:
    –   A vending machine that can sell any small enough
        items
●
    We want:
    –   To deploy several such machines selling bottles of
        pepsi, sushi boxes, and Hintai magazines
    –   Each machine to sell items of one type
Problem 6: Spreadsheet
●
    We have:
    –   A spreadsheet-type program
    –   Containing a table for entering data and formulas
●   We want:
    –   The program to support charts
    –   The charts to be updated automatically if user
        changes the data in the table
    –   To sell the chart add-on separately for only $99.99
Problem 7: Battlefield
●
    We have:
    –   A tank
    –   Several types of bullets
    –   Lots of enemies around, e.g. airplanes, other
        tanks, guys with Molotov-coctails
●
    We want:
    –   To survive
    –   To kill 'em all
Problem 8: TCP over DNS
●
    We have:
    –    A paid Wi-Fi network (e.g. in an airport)
    –    Lots of spare time, but no credit card
●   We want:
    –    To be able to read emails and surf Facebook
    –    (Most likely, during our next trip)
●   Background info:
     –   Paid Wi-Fi usually resolves DNS just fine
     –   DNS packets can contain any payload
Thank you!




Codeborne is the only true extreme
programming company in the region.

Come and join us for an experience of
a lifetime you can't find elsewhere!

                job@codeborne.com

Choose a pattern for a problem

  • 1.
    Agile Saturday VI Choose a pattern for a problem! Anton Keks anton@codeborne.com
  • 2.
    The Increasing Complexity Complexityof software systems Time New programming paradigms Higher level of abstraction HW Assembler Structural (objects+functions?) CPUs Procedural <OOP> ...
  • 3.
    What are designpatterns? ● Anywhere in the world you can find recurring patterns ● Design patterns – are building blocks of a system's architecture – are recurring solutions to design problems that you see over – identify and specify abstractions that are above the level of single classes and instances
  • 4.
    Pattern classification The GOFbook defines 3 major types of patterns: – Creational patterns – substitute the new keyword. This gives your program more flexibility in deciding which objects need to be created for a given case – Structural patterns - compose groups of objects into larger structures, such as complex user interfaces or accounting data – Behavioral patterns - define the communication between objects and how the flow is controlled in a complex program.
  • 5.
    Decorator Adapter Composite Strategy State Observer Factory Iterator Builder Prototype
  • 6.
    Software Design: TheGood ● Loose coupling ● Immutability ● Encapsulation ● Short code blocks ● Appropriate typing ● Duplication avoidance ● Simplicity ● Consistent terminology ● Clear naming
  • 7.
    Bad, bad, bad! ● Loose coupling Tight coupling ● Immutability Mutable data structures ● Encapsulation Tramp data ● Short code blocks Monster functions/methods ● Appropriate typing String/Integer overuse ● Duplication avoidance Copy-Paste ● Simplicity Over-engineering ● Consistent terminology Mixture of terms ● Clear naming Runtime surprises
  • 8.
    Let's get tobusiness! ● Next slides describe problems – What is given (existing code) – What we want to achieve (using a pattern) ● As usual, the code is on github: http://github.com/angryziber/patterns
  • 9.
    Problem 1: Pizza ● We have: – A pizzeria – Multitude of options for pizzas: name, toppings, thickness, size, delivery address, etc ● We want: – Allow users to “order” custom pizzas – Disallow changing the order once the pizza is already in the oven – Keep the ordering code readable
  • 10.
    Problem 2: Unzipper ● We have: – unzip() method – It is able to decompress a single compressed stream ● We need: – Support multiple-volume archives – Compressed data is split into multiple floppies
  • 11.
    Problem 3: Maps ● We have: – Map data of entire Tallinn with all the streets, buildings, etc – The MapVisualizer class that can draw the full map on the screen ● We want: – User to see only a section of the map at any given time – User to be able to move around the map
  • 12.
    Problem 4: CDPlayer ● We have: – A CD player with buttons play, stop, next, eject ● We want: – Play to switch the player on – Play again to pause playback, again to resume – Stop to turn off the player – Eject to open the disk tray (if no disk – no playback possible) – etc, etc, etc
  • 13.
    Problem 5: VendingMachine ● We have: – A vending machine that can sell any small enough items ● We want: – To deploy several such machines selling bottles of pepsi, sushi boxes, and Hintai magazines – Each machine to sell items of one type
  • 14.
    Problem 6: Spreadsheet ● We have: – A spreadsheet-type program – Containing a table for entering data and formulas ● We want: – The program to support charts – The charts to be updated automatically if user changes the data in the table – To sell the chart add-on separately for only $99.99
  • 15.
    Problem 7: Battlefield ● We have: – A tank – Several types of bullets – Lots of enemies around, e.g. airplanes, other tanks, guys with Molotov-coctails ● We want: – To survive – To kill 'em all
  • 16.
    Problem 8: TCPover DNS ● We have: – A paid Wi-Fi network (e.g. in an airport) – Lots of spare time, but no credit card ● We want: – To be able to read emails and surf Facebook – (Most likely, during our next trip) ● Background info: – Paid Wi-Fi usually resolves DNS just fine – DNS packets can contain any payload
  • 17.
    Thank you! Codeborne isthe only true extreme programming company in the region. Come and join us for an experience of a lifetime you can't find elsewhere! job@codeborne.com