SlideShare a Scribd company logo
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

PPTX
How to select password(Chinese)
PPTX
Solid
PPTX
Introduction to Python
PDF
Modern Perl for Non-Perl Programmers
PPTX
Duck Typing and Multiple Inheritance
PPTX
Refactoring
PPTX
MCU compare
PPT
Introduction to Perl
How to select password(Chinese)
Solid
Introduction to Python
Modern Perl for Non-Perl Programmers
Duck Typing and Multiple Inheritance
Refactoring
MCU compare
Introduction to Perl

Similar to Design patterns and MV (20)

PPT
Design patterns
PPT
5 Design Patterns Explained
PPTX
design pattern is the computer scicence subject
PPTX
Design pattern of software words computer .pptx
PPT
Design patterns
PPTX
Cs 1023 lec 2 (week 1) edit 1
PPTX
Cs 1023 lec 2 (week 1) edit 1
PPTX
Design patterns - The Good, the Bad, and the Anti-Pattern
PPT
Jump Start To Ooad And Design Patterns
PPS
Jump start to OOP, OOAD, and Design Pattern
PPTX
Design Patterns
PPTX
Segue to design patterns
PPT
Design patterns ppt
PPTX
Oops design pattern_amitgupta
PPTX
Design patterns
PPTX
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
PDF
Common design patterns in php
PPT
Introduction To Design Patterns
PPT
Design pattern
PPTX
JS Design patterns in Web technologies including oop techniques.pptx
Design patterns
5 Design Patterns Explained
design pattern is the computer scicence subject
Design pattern of software words computer .pptx
Design patterns
Cs 1023 lec 2 (week 1) edit 1
Cs 1023 lec 2 (week 1) edit 1
Design patterns - The Good, the Bad, and the Anti-Pattern
Jump Start To Ooad And Design Patterns
Jump start to OOP, OOAD, and Design Pattern
Design Patterns
Segue to design patterns
Design patterns ppt
Oops design pattern_amitgupta
Design patterns
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Common design patterns in php
Introduction To Design Patterns
Design pattern
JS Design patterns in Web technologies including oop techniques.pptx
Ad

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好處