Title: Session 2A: Design Patterns
1Sitecore for Experts Sitecore skills for
real men (and Max -))
- Session 2A Design Patterns
- Wednesday 14 March 2007
2Todays Program
- Introduction to the subject
- Decorator
- Factory Façades
- Observer
- Questions
- Homework!
- End
3Next Months Program
- Refresh of the subject
- Prototyping Commands
- Async Client
- Algoritmic / Concurrency Semphore
- Questions
- End
4Introduction
5Everything 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
6Christopher Alexander
- American architect
- Building on huge projects(even cities!)
7Building 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!
8In the 80s
- Two computer scientist worked on this idea
Ward Cunningham FoundereXtreme Programming
Wikis
Kent Back FoundereXtreme Programming Agile
Programming
990s 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
10These 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...
11Pattern 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!)
12Last 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...
13Decorator
14Introduction the case
- Sometimes youve got a object which identifies
itself by a property / action. - Example
- Santa Claus a Person with
- a red nose...
15Introduction 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
16Introduction the solution
- Santa is also Person
- Class SantaClaus Person
-
- public Color Nose get return Color.Red
-
- Yep Ive Decorated that Person, hes now Santa.
17Introduction implementing the pattern
- Class SantaClaus Person
-
- SantaClaus(Person)
- public Person Person get set
- public Color Nose get return Color.Red
18Introduction the UML again
19Practical 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!
20Sitecore usage
- Simplify access to data
- CustomItem
- Security UserItem / RoleItem
- Content MediaItem
- Presentation DeviceItem / LayoutItem
- Structure TemplateItem / MasterItem
- CustomField
- HtmlField
- LinkField
- SecurityField
- XmlField
21Advantages of the pattern
- Functionality on runtime
- Easily to implement, no rocket sience
- Provides better interface for libraries
- Code readability improves
22Disadvantages 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......
23Additional 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
24Factory Façades
25Introduction Factory
- Factory Building objects.
26Introduction Factory
- Class Factory
- public ICar CreateCar(CarType)
- public CarChair CreateCarChair(Material)
- public Wheel CreateWheel()
-
27Introduction Façade
- Façade Doing something somewhere else you dont
want to know about.
28Introduction 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)
-
-
29Practical 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
30Practical 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
31Combining 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
32Sitecore usage
- Sitecore.ConfigurationClass Factory
33Advantages of the patterns
- Factory Construction on runtime
- Factory Construction without writing complex
code - Façade Hiding implementation and communication
details of other subsystems
34Disadvantages 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
35Last 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
36Additional reading
- Factory method
- http//en.wikipedia.org/wiki/Factory_method_patter
n - Façade
- http//en.wikipedia.org/wiki/FaC3A7ade_pattern
37Observer
38Introduction
- You want to be updated when something changes
- For example
- Someone places a bid on Ebay
- A user clicks on a button
39Introduction 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
40Practical usage
- All systems which run more then one independant
proces - To keep different threads synchronized
- Connect between object who havent got a direct
relation
41Sitecore usage
- Item handling (creating, editing, deleting)
- Publishing (before, during, after)
- Shell (Window events)
42Advantages of the pattern
- No needs of creating not existing relations
- When good implemented
- Incredible fast
- Thread-safe
- Native support in .NET languages
43Disadvantages and possible problems
- Sometimes hard to imagine
- Hard to debug
44Additional 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 -
45Questions?
46Homework
- 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.
47The 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.