SlideShare a Scribd company logo
C++/Qt Design
Patterns
Ynon Perek
ynon@ynonperek.com
http://ynonperek.com
Sunday, May 5, 13
Good Code Bad Code
Sunday, May 5, 13
When OO
Goes Wrong
Sunday, May 5, 13
Java InputValidation
• In the past, Java Swing
provided only JTextField for
input texts
• JNumericTextField quickly
appeared by the community
Sunday, May 5, 13
Java InputValidation
Sunday, May 5, 13
OO Fail
• Too many classes
• Hard to maintain
• Frustrated Developers
Sunday, May 5, 13
Enter Design Patterns
• Reusable and tested solutions
• Sustainable for the long run
Sunday, May 5, 13
Enter Design Patterns
• Started by Christopher
Alexander in 1977
• And he was an Architect
Sunday, May 5, 13
Example: Street Caffe
“The street cafe
provides a unique
setting, special to
cities: a place where
people can sit lazily,
legitimately, be on
view, and watch the
world go by...”
Sunday, May 5, 13
Patterns Are Not
• Not a detailed solution
• Not a guide to help you build it
Sunday, May 5, 13
Patterns Are
• Describing existing and working solutions
• Your job: Apply to your
world
Sunday, May 5, 13
Patterns In Software
• Defined by GoF
• Categorized:
• Creational
• Behavioral
• Structural
Sunday, May 5, 13
Using Patterns
• Understand
• Memorize
• Apply Repeatedly
Sunday, May 5, 13
Patterns In Qt
• Plenty !
• We’ll show 2:
• Strategy
• Proxy
Sunday, May 5, 13
Strategy Pattern
Sunday, May 5, 13
The Problem
• An input text provides different validation
techniques
• Only one is used at any given time
Sunday, May 5, 13
Approach #1
Sunday, May 5, 13
Why Is It Bad
• Class Explosion
• Hard to reuse validation code
Sunday, May 5, 13
The Better Way
Sunday, May 5, 13
Why Is It Awesome
• Can reuse validators code
• Reduce class explosion
Sunday, May 5, 13
The Pattern
• A validator encapsulates the validation
algorithm
• The general pattern is called Strategy
Sunday, May 5, 13
Intent
• Define a family of algorithms, encapsulate each
one, and make them interchangeable.
Sunday, May 5, 13
Motivation
• Client code is simpler if the algorithm is
external to it
• No need to support an algorithm we don’t
use
• We need to make it easy to add new
algorithms
Sunday, May 5, 13
Applicability
• Configure a class with one of many
behaviors
• Use different variants of an algorithm
• Eliminate Switch blocks
Sunday, May 5, 13
Structure
Sunday, May 5, 13
Participants
• Strategy
• ConcreteStrategy
• Context
Sunday, May 5, 13
Collaborations
• Strategy + Context = Algorithm
• Context forwards requests to Strategy
Sunday, May 5, 13
Consequences
• Families of related algorithms
• Eliminate switch blocks
• Allow different implementations
• Strategy and Context interface can get
complicated
• Increased number of objects
Sunday, May 5, 13
More Examples
• Layout management
• QAbstractNetworkCache
Sunday, May 5, 13
Keep In Mind
• A Default Strategy will make everyone
happy
• Allow clients to create their own strategies
and pass as parameters
Sunday, May 5, 13
Demo
Sunday, May 5, 13
Q & A
Sunday, May 5, 13
Proxy Pattern
Sunday, May 5, 13
______________
< Let's do COW >
--------------
 ^__^
 (oo)_______
(__) )/
||----w |
|| ||
Sunday, May 5, 13
Example Code
QString s1 = getText();
QString s2 = getText();
QString s3 = s1;
// "Hello World" is kept in memory only once
s1.append(" And now it's copied");
Sunday, May 5, 13
The Proxy Way
• A QString is just a proxy to the data
• Implicitly shared
Sunday, May 5, 13
class MyCow
{
public:
MyCow &operator=(const MyCow &other)
{
m_data = other.m_data;
}
void write()
{
m_data = new Data( m_data );
m_data.write();
}
private:
Data *m_data;
};
The Proxy Way
Sunday, May 5, 13
Proxy Types
• Virtual Proxy
• Protection Proxy
• Remote Proxy
Sunday, May 5, 13
Proxy: Pattern Details
Sunday, May 5, 13
Intent
• Provide a surrogate or placeholder for another
object
Sunday, May 5, 13
Motivation
• Save time by lazy loading heavy resources
• Easy Cows
Sunday, May 5, 13
Applicability
• Access remote resources
• Create resources on demand
• Enforce per-object access control
Sunday, May 5, 13
Structure
Sunday, May 5, 13
Participants
• Proxy
• Subject
• Real Subject
Sunday, May 5, 13
Collaborations
• Proxy forwards requests to Real Subject
Sunday, May 5, 13
Consequences
• Hide the fact that an object is remote
• Optimize load times (virtual proxy)
• Can use for Copy-On-Write
Sunday, May 5, 13
Qt Cow
• Not really “By The Book”
• QString has a Data struct
• Does all work by itself
Sunday, May 5, 13
Qt Patterns
• Patterns are part of any framework (Qt
Included)
• Understanding patterns helps us better
understand the framework
Sunday, May 5, 13
Thanks For
Listening !
Slides at:
http://ynonperek.com
Questions / Comments:
ynon@ynonperek.com
Stock Photos from:
http://123rf.com
Sunday, May 5, 13
Thanks For Listening
Sunday, May 5, 13

More Related Content

PDF
In-Depth Model/View with QML
 
PDF
Best Practices in Qt Quick/QML - Part II
 
PDF
Qt Application Programming with C++ - Part 2
PDF
02 - Basics of Qt
PDF
Lessons Learned from Building 100+ C++/Qt/QML Devices
 
PPTX
UI Programming with Qt-Quick and QML
PPTX
Best Practices in Qt Quick/QML - Part I
 
PPTX
Qt Framework Events Signals Threads
In-Depth Model/View with QML
 
Best Practices in Qt Quick/QML - Part II
 
Qt Application Programming with C++ - Part 2
02 - Basics of Qt
Lessons Learned from Building 100+ C++/Qt/QML Devices
 
UI Programming with Qt-Quick and QML
Best Practices in Qt Quick/QML - Part I
 
Qt Framework Events Signals Threads

What's hot (20)

PDF
JUnit & Mockito, first steps
PPT
Qt Technical Presentation
PPTX
Laravel overview
PDF
Qt Application Programming with C++ - Part 1
PDF
PDF
Intro to Asynchronous Javascript
PDF
Best Practices in Qt Quick/QML - Part 1 of 4
 
PDF
Best Practices in Qt Quick/QML - Part IV
 
PDF
Java threads
PDF
Introduction to Qt programming
PPTX
Introduction to Qt
ODP
Using Mockito
PPTX
Introduction to JUnit
ODP
Qt 5 - C++ and Widgets
PDF
Mocking in Java with Mockito
PPTX
Laravel introduction
PPT
Exception Handling in JAVA
PDF
Spark Summit EU talk by Ted Malaska
PDF
Intro to GraphQL
PPTX
java 8 new features
JUnit & Mockito, first steps
Qt Technical Presentation
Laravel overview
Qt Application Programming with C++ - Part 1
Intro to Asynchronous Javascript
Best Practices in Qt Quick/QML - Part 1 of 4
 
Best Practices in Qt Quick/QML - Part IV
 
Java threads
Introduction to Qt programming
Introduction to Qt
Using Mockito
Introduction to JUnit
Qt 5 - C++ and Widgets
Mocking in Java with Mockito
Laravel introduction
Exception Handling in JAVA
Spark Summit EU talk by Ted Malaska
Intro to GraphQL
java 8 new features
Ad

Viewers also liked (15)

PDF
Best Practices in Qt Quick/QML - Part III
 
PDF
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
PDF
Starting Development for Nokia N9
ODP
Qt Workshop
PDF
Qt State Machine Framework
PDF
Cutest technology of them all - Forum Nokia Qt Webinar December 2009
PDF
WebGL and three.js
KEY
Introduction to WebGL and Three.js
PDF
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
PDF
The Anatomy of Real World Apps - Dissecting cross-platform apps written using...
PPT
Diagramas de casos de uso
PPTX
Diagramas de estados
PPT
UML: CASOS DE USO
PDF
WebGL and Three.js
PDF
SOLUTION MANUAL OF OPERATING SYSTEM CONCEPTS BY ABRAHAM SILBERSCHATZ, PETER B...
Best Practices in Qt Quick/QML - Part III
 
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
Starting Development for Nokia N9
Qt Workshop
Qt State Machine Framework
Cutest technology of them all - Forum Nokia Qt Webinar December 2009
WebGL and three.js
Introduction to WebGL and Three.js
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
The Anatomy of Real World Apps - Dissecting cross-platform apps written using...
Diagramas de casos de uso
Diagramas de estados
UML: CASOS DE USO
WebGL and Three.js
SOLUTION MANUAL OF OPERATING SYSTEM CONCEPTS BY ABRAHAM SILBERSCHATZ, PETER B...
Ad

Similar to Qt Design Patterns (20)

PDF
(eBook PDF) Starting Out with C++: From Control Structures through Objects 8t...
PDF
(eBook PDF) Starting Out with C++: From Control Structures through Objects, B...
PDF
(eBook PDF) Starting Out with C++: From Control Structures through Objects 8t...
PDF
(eBook PDF) Starting Out with C++: From Control Structures through Objects 8t...
PDF
(eBook PDF) Starting Out with C++: From Control Structures through Objects 8t...
PDF
(eBook PDF) Starting Out with C++: From Control Structures through Objects 8t...
PDF
Starting Out with C++: From Control Structures through Objects 8th Edition, (...
PDF
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
PDF
Download Complete The Rust Programming Language 2nd Edition Steve Klabnik PDF...
PDF
The Rust Programming Language Steve Klabnik
PDF
The Rust Programming Language Steve Klabnik
PDF
The Rust Programming Language 2nd Edition Steve Klabnik
PDF
(eBook PDF) Starting Out with C++: Early Objects 9th Edition
PDF
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
PDF
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
PDF
(eBook PDF) Starting Out with C++: Early Objects 9th Edition
PDF
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
PDF
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
PDF
(Ebook) The Rust Programming Language, Second Edition by Steve Klabnik, Carol...
PPT
(eBook PDF) Starting Out with C++: From Control Structures through Objects 8t...
(eBook PDF) Starting Out with C++: From Control Structures through Objects, B...
(eBook PDF) Starting Out with C++: From Control Structures through Objects 8t...
(eBook PDF) Starting Out with C++: From Control Structures through Objects 8t...
(eBook PDF) Starting Out with C++: From Control Structures through Objects 8t...
(eBook PDF) Starting Out with C++: From Control Structures through Objects 8t...
Starting Out with C++: From Control Structures through Objects 8th Edition, (...
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
Download Complete The Rust Programming Language 2nd Edition Steve Klabnik PDF...
The Rust Programming Language Steve Klabnik
The Rust Programming Language Steve Klabnik
The Rust Programming Language 2nd Edition Steve Klabnik
(eBook PDF) Starting Out with C++: Early Objects 9th Edition
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
(eBook PDF) Starting Out with C++: Early Objects 9th Edition
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
Starting Out with C++: Early Objects 9th Edition by Tony Gaddis (eBook PDF)
(Ebook) The Rust Programming Language, Second Edition by Steve Klabnik, Carol...

More from Ynon Perek (20)

PDF
Regexp
PDF
Html5 intro
PDF
09 performance
PDF
Mobile Web Intro
PDF
Qt multi threads
PDF
Vimperl
PDF
Syllabus
PDF
Mobile Devices
PDF
Network
PDF
Architecture app
PDF
Cryptography
PDF
Unit Testing JavaScript Applications
PDF
How to write easy-to-test JavaScript
PDF
Introduction to Selenium and Ruby
PDF
Introduction To Web Application Testing
PDF
Accessibility
PDF
Angularjs
PDF
Js memory
PDF
Web Application Security
PDF
JavaScript DOM Manipulations
Regexp
Html5 intro
09 performance
Mobile Web Intro
Qt multi threads
Vimperl
Syllabus
Mobile Devices
Network
Architecture app
Cryptography
Unit Testing JavaScript Applications
How to write easy-to-test JavaScript
Introduction to Selenium and Ruby
Introduction To Web Application Testing
Accessibility
Angularjs
Js memory
Web Application Security
JavaScript DOM Manipulations

Recently uploaded (20)

PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
1. Introduction to Computer Programming.pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Approach and Philosophy of On baking technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Machine Learning_overview_presentation.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
cuic standard and advanced reporting.pdf
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Getting Started with Data Integration: FME Form 101
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
gpt5_lecture_notes_comprehensive_20250812015547.pdf
1. Introduction to Computer Programming.pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Building Integrated photovoltaic BIPV_UPV.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Reach Out and Touch Someone: Haptics and Empathic Computing
MYSQL Presentation for SQL database connectivity
Approach and Philosophy of On baking technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
Machine Learning_overview_presentation.pptx
Assigned Numbers - 2025 - Bluetooth® Document
cuic standard and advanced reporting.pdf
SOPHOS-XG Firewall Administrator PPT.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Digital-Transformation-Roadmap-for-Companies.pptx
Spectral efficient network and resource selection model in 5G networks
Getting Started with Data Integration: FME Form 101
“AI and Expert System Decision Support & Business Intelligence Systems”

Qt Design Patterns