marat kamenschykov. gof patterns + rails =?
TRANSCRIPT
Rails + GoF Patterns
= ?Marat KamenschikovSvitla Systems Inc.
GoF = Gang of Four:
Erich GammaRichard Helm
Ralph JohnsonJohn Vlissides
Design Patterns: Elements of Reusable Object-Oriented
Software
With foreword of Grady BoochPrinted on October 21, 1994
as of July 2010, the book was in its 38th printing
500,000 copies have been sold in English and in 13 other languages.
23 reusable design patterns
3 parts:
• Structural patterns
• Creational patterns
• Behavioral patterns
Creational patterns
• Create objects for you, rather than having you instantiate objects directly
• 5 patterns: – Abstract Factory– Builder (Строитель)– Factory Method– Prototype – Singleton (Одиночка)
Structural patterns
• Class and object composition• 7 patterns:
– Adapter (Адаптер)– Bridge – Composite– Decorator– Façade (Фасад)– Flyweight (Приспособленец)– Proxy (Заместитель)
Behavioral patterns
• Communication between objects• 11 patterns:
– Chain of responsibility– Command– Interpreter– Iterator– Mediator– Memento– Observer– State– Strategy– Template method– Visitor
Project overview
• Web-interface for IP-telephony service subscribers
• 3 WSDL services
• 3+ XML services
• 5 foreign MySQL DBs
• Local MySQL DB for structuring the incoming info
DB
XML
WSDL
WSDL
WSDL
XMLXML
DB
DB
DB
DB
Web-appwith DB
Purpose of my talk:
• Which GoF patterns we were able to use?
• How did they couple with Rails?
• Results: what is good and what is bad
Project challenges
• Not GUI-oriented Servers Object structure
• Requirement to store settings in local DB on login to support several parallel sessions
• Rails
Not GUI-oriented Servers Object structure
• Web-app: single system with a straight structure – 1 root object and others are associated
• Services and remote DBs: distributed system with various authenticating procedures and ways to retrieve data
Ideal data structure (initial web-app design)
Blacklists
CallForwarding
Privacy
Message
Folder
Address
BusinessModel
Tier CreditCard
Bundle
Subscription
Feature
Number
Account
Inbox
GoogleVoice
System
Device
Services structure
Service
XML1_2
Address
XML1_3
CreditCard
XML1_1
User
customer_pk
Number
Device
pin
Login
xml_session WSDLLogin
wsdl_session
VMForwarding
VMMessages
ForeignDBs
It’s almost completely different!!!
And not only in data structure!!!
Façade
Façade
Façade
Singleton
Builder
Flyweight
Flyweight
Proxy
Façade
class ConnectionAdapter
end
Singleton
@@
Flyweight
Id | name 1 | Nick
User < ARid: 1name: Nick
User.find(1)User.find_by_id(1)
User.find_by_name(‘Nick’)
Builder
• Tried to build ideal structure of what we had: FAIL!
Finally
CallForwarding
Blacklists
VoicemailNotification
Privacy
Number
Message
Folder
Action1
Subscription
Bundle
Tier
Action2
BusinessModel
UserSession
CreditCard
Action3
Address
GoogleVoiceDevice
Proxy
UserSession < AR::Base
Results
• UserSession is the only singleton and eventually Façade in Rails
• Don’t build ideal data model, build what is needed
• Identity Map requires different approach to associations but still awesome
• Lazy loading is cool!
Thank you!