Design Patterns and MVC
Sway Wang
2014.12.25
Outline
• What is Design Pattern?
– Singleton pattern
– Strategy pattern
– Observer pattern
• What is MVC?
– C# example
– Ruby on Rails
• Conclusion
What is Design Pattern?
• Patterns are formalized best practices that the
programmer can use to solve common problems when
designing an application or system
The History of Design Pattern
• Christopher Alexander (1977)
• Kent Beck and Ward Cunningham (1987)
• Gamma, Erich; Richard Helm, Ralph Johnson,
and John Vlissides
– Design Patterns: Elements of Reusable Object-
Oriented Software (1995)
History
Gang of Four (GoF) Patterns
• Creational Patterns
(abstracting the object-instantiation process)
– Factory Method Abstract Factory
– Singleton Builder
– Prototype
• Structural Patterns
(how objects/classes can be combined to form larger structures)
– Adapter Bridge
– Composite Decorator
– Facade Flyweight
– Proxy
Gang of Four (GoF) patterns
• Behavioral Patterns
(communication between objects)
– Command Interpreter Iterator
– Mediator Observer State
– Strategy Chain of Responsibility Visitor
– Template Method
Singleton Pattern
• It restricts the instantiation of a class to
one object
Singleton Pattern(C#)
Is Your Singleton Broken?
• The singleton pattern must be carefully
constructed in multi-threaded applications
• [OSDC 2014] fr3@K - Is Your Singleton
Broken? - YouTube
Is Your Singleton Broken?
• The singleton pattern must be carefully
constructed in multi-threaded applications
• [OSDC 2014] fr3@K - Is Your Singleton
Broken? – YouTube
• Global variable issue
Strategy Pattern
• It enables an algorithm's behavior to be
selected at runtime
Strategy Pattern: Duck Example
Now We Need the Ducks to Fly
Now We Need the Ducks to Fly
Decoy Duck
How About a Interface?
Duplicate code
Strategy Pattern: Duck Example
Strategy Pattern: Duck Example
Strategy Pattern: Duck Example
Strategy Pattern: Duck Example
Strategy Pattern: Duck Example
Setting Behavior Dynamically
Strategy Pattern: Duck Example
Design Principle
多用合成,少用繼承
Open-Closed Principle(OCP)
Software entities (classes, modules,
functions, etc.) should be open for
extension, but closed for modification
一個軟體應該要夠開放使得它可以被擴充,
但是也要夠封閉以避免不必要的修改
Strategy Pattern
• It enables an algorithm's behavior to be
selected at runtime
Observer Pattern
• Newspaper subscription
• Publishers + Subscribers = Observer Pattern
Observer Pattern
• Newspaper subscription
• Publishers + Subscribers = Observer Pattern
Observer Pattern
Observer Pattern
Observer Example (C#)
Subject Example (C#)
Subject Example (C#)
The History of MVC
• MVC is really old and was first described in
1979 by Trygve Reenskaug
• Smalltalk at Xerox PARC.
• Today MVC is used in most modern web and
GUI frameworks
The History of MVC
• Ruby On Rails: A popular Ruby web-
framework
• Apple Cocoa: Apple's framework for
developing Mac OS and iOS applications
• ASP.Net Framework: Microsoft's web-
framework for implementing web applications
• ... And many many more languages and
frameworks
MVC
View
Controller
Model
Perl CGI – 計數器
PHP計數器
Ruby on Rails
缺點
• HTML和邏輯綁在一起
• 網站規模變大後不易修改
• 難以分工
• Web Framework
– 導入OO概念
– 制定好了一套規範和慣例,讓開發者在該架構下來進行
開發
Web Framework
• 導入OO概念
• 制定好了一套規範和慣例,讓開發者在該架
構下來進行開發
Conclusion
• 要精通OOP一定要會Design Pattern
• Design Pattern三種境界
– 什麼東西都套pattern
– 知道什麼時候該用pattern
– KISS principle
• MVC架構的AP後續維護較容易
• 設計良好的程式碼當未來需求變更時,較容易改
變

Design patterns and MV

Editor's Notes

  • #25 Open Close Principle
  • #38 TODO: MVC Sample code MVC v.s. MVVM MVC好處
  • #39 TODO: MVC Sample code MVC v.s. MVVM MVC好處
  • #40 TODO: MVC Sample code MVC v.s. MVVM MVC好處
  • #41 TODO: MVC Sample code MVC v.s. MVVM MVC好處