Session 2A: Design Patterns - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

Session 2A: Design Patterns

Description:

Elvis and Bob were still alive ... These days. Fundamental Patterns ... Something you'll learn in a day. Answer to everything... Last but not least... Decorator ... – PowerPoint PPT presentation

Number of Views:126
Avg rating:3.0/5.0
Slides: 48
Provided by: alexde6
Category:

less

Transcript and Presenter's Notes

Title: Session 2A: Design Patterns


1
Sitecore for Experts Sitecore skills for
real men (and Max -))
  • Session 2A Design Patterns
  • Wednesday 14 March 2007

2
Todays Program
  • Introduction to the subject
  • Decorator
  • Factory Façades
  • Observer
  • Questions
  • Homework!
  • End

3
Next Months Program
  • Refresh of the subject
  • Prototyping Commands
  • Async Client
  • Algoritmic / Concurrency Semphore
  • Questions
  • End

4
Introduction
5
Everything was better in the 70s
  • Less computers
  • Feminism wasnt fully propagated -)
  • Elvis and Bob were still alive
  • Buildings could be constructed without any
    environmental restrictions

6
Christopher Alexander
  • American architect
  • Building on huge projects(even cities!)

7
Building patterns
  • Christopher looked at his work and discovered
  • Building and infrastructure design have some many
    repeatable steps.
  • Those steps have got the same results
  • Simplified results to unified building blocks
  • Proved their existence afterwards
  • Patterns were born!

8
In the 80s
  • Two computer scientist worked on this idea

Ward Cunningham FoundereXtreme Programming
Wikis
Kent Back FoundereXtreme Programming Agile
Programming
9
90s Gang of Four
  • Ralph Johnson, Richard Helm, Erich Gamma, and
    John Vlissides
  • Gamma presented the others his Thesis work on
    OOP-SLA 90
  • 4 years of work contributed toDesign Pattern
    Elements of Reusable Object Oriented Software

10
These days
  • Design Patterns are part of regular IT- education
  • All original patterns still exists(!!)
  • Some are added and some slightly improved
  • Used as base for nearly all applications
  • Invented the idea of Application blocks(design
    pattern on Architecture level)
  • Sitecore uses m...

11
Pattern Categories
  • Fundamental PatternsFeeding the general need of
    a developer such as Interface design
  • Creational PatternsPatterns which gives you
    handhelds to construct your objects
  • Structural PatternsThe design of the relations
    between objects
  • Behavioural PatternsWays to cocommunicate
    between objects
  • Concurrency PatternsBest pratices on
    implementing concurrency(not just threads!)

12
Last but not least...
  • Design patterns arent...
  • Hard to understand
  • Codesnippets you directly apply on your code
  • Something youll learn in a day
  • Answer to everything...

13
Decorator
14
Introduction the case
  • Sometimes youve got a object which identifies
    itself by a property / action.
  • Example
  • Santa Claus a Person with
  • a red nose...

15
Introduction the case
  • Santa has the same properties like every Person
  • Class Person
  • public string FirstName get set
  • public string LastName get set
  • public DateTime BirthDate get set

16
Introduction the solution
  • Santa is also Person
  • Class SantaClaus Person
  • public Color Nose get return Color.Red
  • Yep Ive Decorated that Person, hes now Santa.

17
Introduction implementing the pattern
  • Class SantaClaus Person
  • SantaClaus(Person)
  • public Person Person get set
  • public Color Nose get return Color.Red

18
Introduction the UML again
19
Practical usage
  • GUI componentsBuilding up functionalityWindow,
    WindowWithBorder, WindowWithBorderAndScrollBar
  • StreamsRuntime base functionalityStream,
    TextStream, IOStream, NetStream
  • Security layersHiding field which shouldnt be
    accessed by other developer. Make your base
    private and just display the fields you want!

20
Sitecore usage
  • Simplify access to data
  • CustomItem
  • Security UserItem / RoleItem
  • Content MediaItem
  • Presentation DeviceItem / LayoutItem
  • Structure TemplateItem / MasterItem
  • CustomField
  • HtmlField
  • LinkField
  • SecurityField
  • XmlField

21
Advantages of the pattern
  • Functionality on runtime
  • Easily to implement, no rocket sience
  • Provides better interface for libraries
  • Code readability improves

22
Disadvantages and possible problems
  • More classes, might be (a bit) slower
  • Original class doesnt define the
    possiblitiesE.g. What kind of CustomField is X?
  • Requires a well organized object structureE.g.
    Should be possible but where is that Decorator?
    Owwwww.... There it is......

23
Additional reading
  • General
  • http//www.dofactory.com/Patterns/PatternDecorator
    .aspx
  • http//en.wikipedia.org/wiki/Decorator_pattern
  • Sitecore specific Alexey Rusakovs blog
  • http//www.alexeyrusakov.com/sitecoreblog/2007/02/
    04/CustomItemSitecoreAPIPatternExplained.aspx
  • http//www.alexeyrusakov.com/sitecoreblog/2007/02/
    08/CustomFieldSitecoreAPIPatternExplained.asp
    x

24
Factory Façades
25
Introduction Factory
  • Factory Building objects.

26
Introduction Factory
  • Class Factory
  • public ICar CreateCar(CarType)
  • public CarChair CreateCarChair(Material)
  • public Wheel CreateWheel()

27
Introduction Façade
  • Façade Doing something somewhere else you dont
    want to know about.

28
Introduction Façade
  • Class Façade
  • public CRMConnection crm get set
  • public BizTalkConnToSAP bizSap get set
  • public Façade()
  • public bool ExistsInSapAndCrm(Person p)
  • return crm.Exists(p) bizSap.Exists(p)

29
Practical usage Factory
  • Building objects which are hard to construct
  • Construct object which need some more actions
    direct after construction
  • Examples
  • Configreaders
  • Document creation
  • Parameters as a part of a connection

30
Practical usage Façade
  • You dont want to know anything about the other
    subsystems
  • You want to make sure this is really the end of
    the other subsystem
  • So communications starts and stops here

31
Combining Factory Façade
  • Software is built on blocks(subsystem)
  • Factories allows you to create objects for
    communication with all different subsystems
    (interfaces)
  • Those interfaces are façades
  • Or sometimes objects are constructed using
    façades when they need data from different sources

32
Sitecore usage
  • Sitecore.ConfigurationClass Factory

33
Advantages of the patterns
  • Factory Construction on runtime
  • Factory Construction without writing complex
    code
  • Façade Hiding implementation and communication
    details of other subsystems

34
Disadvantages and possible problems
  • Factory No abilities to control the amount of
    instances
  • Façades As youre hiding details, you might do
    not efficient reuse your code

35
Last note
  • Sitecore.Configuration.Factory might also contain
    another pattern Abstract Factory.
  • Not described as it would take to much time.
  • See also http//en.wikipedia.org/wiki/Abstract_fa
    ctory_pattern

36
Additional reading
  • Factory method
  • http//en.wikipedia.org/wiki/Factory_method_patter
    n
  • Façade
  • http//en.wikipedia.org/wiki/FaC3A7ade_pattern

37
Observer
38
Introduction
  • You want to be updated when something changes
  • For example
  • Someone places a bid on Ebay
  • A user clicks on a button

39
Introduction the steps
  • First initialize a dispatcher
  • Add observers / listeners
  • Send notify to
  • All listeners
  • Listeners with a specific signatureE.g. Those
    who accept specific arguments

40
Practical usage
  • All systems which run more then one independant
    proces
  • To keep different threads synchronized
  • Connect between object who havent got a direct
    relation

41
Sitecore usage
  • Item handling (creating, editing, deleting)
  • Publishing (before, during, after)
  • Shell (Window events)

42
Advantages of the pattern
  • No needs of creating not existing relations
  • When good implemented
  • Incredible fast
  • Thread-safe
  • Native support in .NET languages

43
Disadvantages and possible problems
  • Sometimes hard to imagine
  • Hard to debug

44
Additional reading
  • General
  • http//en.wikipedia.org/wiki/Observer_pattern
  • http//www.dofactory.com/Patterns/PatternObserver.
    aspx
  • Sitecore specific
  • http//sdn5.sitecore.net/Articles/API/Using20Even
    ts.aspx

45
Questions?
  • This is your last change

46
Homework
  • Find out what Sitecore.Data.Comparers.
    ComparerFactory does. Write it down in at most
    200 words so anyone who isn't technical
    understands it (my and your mum).
  • Provide me a sample implementation of another
    comparer and explain in approx. 15 steps what the
    system does to retrieve end return the comparer.
  • Find out what the Abstract Factory Pattern is.
    Promote 2 method in the whole Sitecore API which
    might have implemented this pattern. Explain in
    at most 250 per method why and how. You may make
    1 drawing which applies for both methods.
  • Sent it to a.degroot_at_lectric.nl before Monday 2
    April.

47
The end
  • Thank you for your attention!
  • And please, think about it and spread the word
  • Mistakes, corrections and additions can be mailed
    to Alex de Groot a.degroot_at_lectric.nlThe
    presentation is part of LECTRIC / Alex de Groot
    but can be used without any premission.
Write a Comment
User Comments (0)
About PowerShow.com