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
Best Practices in Qt Quick/QML - Part II
 
PDF
In-Depth Model/View with QML
 
PPTX
Best Practices in Qt Quick/QML - Part I
 
PDF
Qt Application Programming with C++ - Part 1
PPTX
Qt Qml
PDF
QVariant, QObject — Qt's not just for GUI development
 
PPTX
Introduction to Qt
PDF
Best Practices in Qt Quick/QML - Part III
 
Best Practices in Qt Quick/QML - Part II
 
In-Depth Model/View with QML
 
Best Practices in Qt Quick/QML - Part I
 
Qt Application Programming with C++ - Part 1
Qt Qml
QVariant, QObject — Qt's not just for GUI development
 
Introduction to Qt
Best Practices in Qt Quick/QML - Part III
 

What's hot (20)

PPTX
Qt for beginners part 1 overview and key concepts
 
PPTX
UI Programming with Qt-Quick and QML
PPTX
[Webinar] Qt Test-Driven Development Using Google Test and Google Mock
 
PDF
Qt Application Programming with C++ - Part 2
PDF
Best Practices in Qt Quick/QML - Part 3
 
PDF
Introduction to QML
PDF
Cloud-Native CI/CD on Kubernetes with Tekton Pipelines
PDF
Best Practices in Qt Quick/QML - Part 1 of 4
 
PDF
Lessons Learned from Building 100+ C++/Qt/QML Devices
 
PDF
Best Practices in Qt Quick/QML - Part 4
 
ODP
Qt 5 - C++ and Widgets
PDF
Best Practices in Qt Quick/QML - Part IV
 
PDF
An introduction to Google test framework
PDF
Configurable horizontal pod autoscaler
ODP
Unit testing with Qt test
PPTX
Qt Framework Events Signals Threads
PPT
Agile QA presentation
PDF
Introduction to Robot Framework – Exove
PDF
JUnit 5 - The Next Generation
PDF
Qt multi threads
Qt for beginners part 1 overview and key concepts
 
UI Programming with Qt-Quick and QML
[Webinar] Qt Test-Driven Development Using Google Test and Google Mock
 
Qt Application Programming with C++ - Part 2
Best Practices in Qt Quick/QML - Part 3
 
Introduction to QML
Cloud-Native CI/CD on Kubernetes with Tekton Pipelines
Best Practices in Qt Quick/QML - Part 1 of 4
 
Lessons Learned from Building 100+ C++/Qt/QML Devices
 
Best Practices in Qt Quick/QML - Part 4
 
Qt 5 - C++ and Widgets
Best Practices in Qt Quick/QML - Part IV
 
An introduction to Google test framework
Configurable horizontal pod autoscaler
Unit testing with Qt test
Qt Framework Events Signals Threads
Agile QA presentation
Introduction to Robot Framework – Exove
JUnit 5 - The Next Generation
Qt multi threads
Ad

Viewers also liked (14)

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...
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
Ab(Using) the MetaCPAN API for Fun and Profit v2013
PDF
jQuery Mobile Jump Start
PDF
WordCamp Milwaukee 2012 - Contributing to Open Source
PDF
Getting Started with Meteor
PDF
jQuery Mobile Deep Dive
PDF
PhoneGap in a Day
PDF
node.js in action
PDF
jQuery Mobile, Backbone.js, and ASP.NET MVC
PDF
Apereo OAE - Architectural overview
PDF
Designing the User Experience - UCCSC 2013
PDF
AppEngine Performance Tuning
PDF
Js memory
PDF
Responsive Design and jQuery Mobile
PDF
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
PDF
State of Pyramid - Brasilia 2013
PDF
Cassandra at scale
PDF
RailsAdmin - Overview and Best practices
PDF
Upgrades and migrations
PDF
Introduction to java
PDF
DjangoCon 2013 - How to Write Fast and Efficient Unit Tests in Django
Ab(Using) the MetaCPAN API for Fun and Profit v2013
jQuery Mobile Jump Start
WordCamp Milwaukee 2012 - Contributing to Open Source
Getting Started with Meteor
jQuery Mobile Deep Dive
PhoneGap in a Day
node.js in action
jQuery Mobile, Backbone.js, and ASP.NET MVC
Apereo OAE - Architectural overview
Designing the User Experience - UCCSC 2013
AppEngine Performance Tuning
Js memory
Responsive Design and jQuery Mobile
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
State of Pyramid - Brasilia 2013
Cassandra at scale
RailsAdmin - Overview and Best practices
Upgrades and migrations
Introduction to java
DjangoCon 2013 - How to Write Fast and Efficient Unit Tests in Django

More from Ynon Perek (20)

PDF
Regexp
PDF
Html5 intro
PDF
09 performance
PDF
Mobile Web Intro
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
Web Application Security
PDF
JavaScript DOM Manipulations
PDF
Mongodb Intro
PDF
Node JS
Regexp
Html5 intro
09 performance
Mobile Web Intro
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
Web Application Security
JavaScript DOM Manipulations
Mongodb Intro
Node JS

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
KodekX | Application Modernization Development
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Big Data Technologies - Introduction.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Empathic Computing: Creating Shared Understanding
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation_ Review paper, used for researhc scholars
Understanding_Digital_Forensics_Presentation.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KodekX | Application Modernization Development
Dropbox Q2 2025 Financial Results & Investor Presentation
Big Data Technologies - Introduction.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Reach Out and Touch Someone: Haptics and Empathic Computing
The Rise and Fall of 3GPP – Time for a Sabbatical?
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
MYSQL Presentation for SQL database connectivity
MIND Revenue Release Quarter 2 2025 Press Release
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Spectroscopy.pptx food analysis technology
Empathic Computing: Creating Shared Understanding
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf

Qt Design Patterns