SlideShare a Scribd company logo
1 of 46
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後續維護較容易
• 設計良好的程式碼當未來需求變更時,較容易改
變

More Related Content

Similar to Design Patterns and MVC Framework Concepts

Segue to design patterns
Segue to design patternsSegue to design patterns
Segue to design patternsRahul Singh
 
Solid Principles Of Design (Design Series 01)
Solid Principles Of Design (Design Series 01)Solid Principles Of Design (Design Series 01)
Solid Principles Of Design (Design Series 01)Heartin Jacob
 
Nodejs Chapter 3 - Design Pattern
Nodejs Chapter 3 - Design PatternNodejs Chapter 3 - Design Pattern
Nodejs Chapter 3 - Design PatternTalentica Software
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General IntroductionAsma CHERIF
 
ASP.NET MVC Best Practices malisa ncube
ASP.NET MVC Best Practices   malisa ncubeASP.NET MVC Best Practices   malisa ncube
ASP.NET MVC Best Practices malisa ncubeMalisa Ncube
 
Design patterns in Swift Ch4: Iterator & Template Method
Design patterns in Swift Ch4: Iterator & Template MethodDesign patterns in Swift Ch4: Iterator & Template Method
Design patterns in Swift Ch4: Iterator & Template MethodChihyang Li
 
MWLUG 2015 - An Introduction to MVC
MWLUG 2015 - An Introduction to MVCMWLUG 2015 - An Introduction to MVC
MWLUG 2015 - An Introduction to MVCUlrich Krause
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2Julie Iskander
 
An Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesAn Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesUlrich Krause
 
Online TechTalk  "Patterns in Embedded SW Design"
Online TechTalk  "Patterns in Embedded SW Design"Online TechTalk  "Patterns in Embedded SW Design"
Online TechTalk  "Patterns in Embedded SW Design"GlobalLogic Ukraine
 
Developing Modeling Tool for RM-ODP with Eclipse Sirius
Developing Modeling Tool for RM-ODP with Eclipse SiriusDeveloping Modeling Tool for RM-ODP with Eclipse Sirius
Developing Modeling Tool for RM-ODP with Eclipse SiriusObeo
 
Exploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with ArcheometerExploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with ArcheometerAgustin Ramos
 
Software variability management - 2017
Software variability management - 2017Software variability management - 2017
Software variability management - 2017XavierDevroey
 
Developing Modeling Tool for RM-ODP with Eclipse Sirius
Developing Modeling Tool for RM-ODP with Eclipse SiriusDeveloping Modeling Tool for RM-ODP with Eclipse Sirius
Developing Modeling Tool for RM-ODP with Eclipse SiriusAkira Tanaka
 

Similar to Design Patterns and MVC Framework Concepts (20)

Segue to design patterns
Segue to design patternsSegue to design patterns
Segue to design patterns
 
Solid Principles Of Design (Design Series 01)
Solid Principles Of Design (Design Series 01)Solid Principles Of Design (Design Series 01)
Solid Principles Of Design (Design Series 01)
 
Nodejs Chapter 3 - Design Pattern
Nodejs Chapter 3 - Design PatternNodejs Chapter 3 - Design Pattern
Nodejs Chapter 3 - Design Pattern
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
 
ASP.NET MVC Best Practices malisa ncube
ASP.NET MVC Best Practices   malisa ncubeASP.NET MVC Best Practices   malisa ncube
ASP.NET MVC Best Practices malisa ncube
 
Niyum framework
Niyum frameworkNiyum framework
Niyum framework
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
Design Patterns.ppt
Design Patterns.pptDesign Patterns.ppt
Design Patterns.ppt
 
Design patterns in Swift Ch4: Iterator & Template Method
Design patterns in Swift Ch4: Iterator & Template MethodDesign patterns in Swift Ch4: Iterator & Template Method
Design patterns in Swift Ch4: Iterator & Template Method
 
MWLUG 2015 - An Introduction to MVC
MWLUG 2015 - An Introduction to MVCMWLUG 2015 - An Introduction to MVC
MWLUG 2015 - An Introduction to MVC
 
10-DesignPatterns.ppt
10-DesignPatterns.ppt10-DesignPatterns.ppt
10-DesignPatterns.ppt
 
L03 Design Patterns
L03 Design PatternsL03 Design Patterns
L03 Design Patterns
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2
 
An Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesAn Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPages
 
Online TechTalk  "Patterns in Embedded SW Design"
Online TechTalk  "Patterns in Embedded SW Design"Online TechTalk  "Patterns in Embedded SW Design"
Online TechTalk  "Patterns in Embedded SW Design"
 
Developing Modeling Tool for RM-ODP with Eclipse Sirius
Developing Modeling Tool for RM-ODP with Eclipse SiriusDeveloping Modeling Tool for RM-ODP with Eclipse Sirius
Developing Modeling Tool for RM-ODP with Eclipse Sirius
 
Exploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with ArcheometerExploring Elixir Codebases with Archeometer
Exploring Elixir Codebases with Archeometer
 
Software variability management - 2017
Software variability management - 2017Software variability management - 2017
Software variability management - 2017
 
Developing Modeling Tool for RM-ODP with Eclipse Sirius
Developing Modeling Tool for RM-ODP with Eclipse SiriusDeveloping Modeling Tool for RM-ODP with Eclipse Sirius
Developing Modeling Tool for RM-ODP with Eclipse Sirius
 

Design Patterns and MVC Framework Concepts

Editor's Notes

  1. Open Close Principle
  2. TODO: MVC Sample code MVC v.s. MVVM MVC好處
  3. TODO: MVC Sample code MVC v.s. MVVM MVC好處
  4. TODO: MVC Sample code MVC v.s. MVVM MVC好處
  5. TODO: MVC Sample code MVC v.s. MVVM MVC好處