SlideShare une entreprise Scribd logo
1  sur  59
www.italiancpp.org
Going native with less
coupling
Dependency Injection in C++
Italian C++ Community
Style 1: [Fake] OO Design
i=2
CONTROLLER /
MANAGER
Dumb
object
Dumb
object
Dumb
object
Dumb
object
Dumb
object
Dumb
object
Dumb
object
Dumb
object
Italian C++ Community
Style 2: [True] OO design
i=3
Smart
Object
Smart
Object
Smart
Object
Smart
Object
Smart
Object
Smart
Object
Smart
Object
Smart
Object
Smart
Object
Smart
Object
Smart
Object
Smart
Object
Smart
Object
Smart
Object
Italian C++ Community
OO Architecture Benefits
i=4
Up Scalability (extension)
Down Scalability
(contraction)
Reusability
Safety (testability)
Italian C++ Community i=5
The Wiring
Issue
In a True Modular Architecture, wiring is critical
Italian C++ Community
The Wiring Issue
When you've got a Good Architecture, you deal with
requirements changes by adding/removing/substituting objects
So, we need a simple way to:
Change the type of the objects
Create new objects
Modify the wiring of the objects
i=6
Italian C++ Community i=7
Life without a
CONTROLLER
Example taken from:
Carlo Pescio's blog – March 2012
Italian C++ Community
First Design
i=8
MinePlant
SumpPump
+Drain()
PumpEngine
+On()
+Off()
DigitalOutput
+Write()
GasSensor
<<interface>>
+IsCritical()
GasAlarm
+Watch()
1..*
Alarm
+On()
+Off()
SafeEngine
Italian C++ Community
Requirements Change
i=9
MinePlant
SumpPump
+Drain()
PumpEngine
+On()
+Off()
DigitalOutput
+Write()
GasSensor
<<interface>>
+IsCritical()
GasAlarm
+Watch()
1..*
Alarm
+On()
+Off()
SafeEngine
Italian C++ Community
Extension
The design is robust: I only need to add a class
But…
Who creates SafeEngine instead of PumpEngine?
How does SafeEngine get the pointer to the
GasSensor (the same used by GasAlarm)?
i=10
Italian C++ Community
Solution #1: Local Creation
Each class creates its own
dependencies
i=11
Italian C++ Community i=12
MinePlant
SumpPump
+Drain()
PumpEngine
+On()
+Off()
DigitalOutput
+Write()
GasSensor
<<interface>>
+IsCritical()
GasAlarm
+Watch()
1..*
Alarm
+On()
+Off()
SafeEngine <<create>>
Italian C++ Community
Solution #1: Consequences
The SumpPump constructor creates a SafeEngine
instead of a PumpEngine.
… but SafeEngine needs a pointer to the GasSensor
instance already used by GasAlarm.
So, we must pass it as a parameter to SumpPump
constructor.
i=13
Italian C++ Community
Solution #1: Properties
If I need to change the concrete
type, I have to modify the client.
It's difficult to reuse the same client
class (even in the same application).
i=14
Italian C++ Community
Solution #1: Summary
SafeEngine class added
SumpPump constructor modified
MinePlant modified
i=15
Italian C++ Community
Solution #2: Factory
(not the GoF factory)
Create objects without exposing
the instantiation logic to the client
i=16
Italian C++ Community i=17
<<create>>
Italian C++ Community
Solution #2: Consequences
The SumpPump constructor takes a Factory as
parameter.
PumpEngineFactory instantiates a PumpEngine.
SafeEngineFactory instantiates a SafeEngine.
SafeEngine still needs a pointer to the GasSensor
instance already used by GasAlarm, so we must pass
it to the SafeEngineFactory constructor.
i=18
Italian C++ Community
Solution #2: Summary
SafeEngine class added
SafeEngineFactory added
MinePlant modified
i=19
Italian C++ Community
Solution #3: Service Locator
It’s a registry containing the
instances to use
i=20
Italian C++ Community
Solution #3: Service Locator
i=21
class ServiceLocator
{
public:
static ServiceLocator& Instance();
shared_ptr< PumpEngine > Engine();
void Engine( const shared_ptr< PumpEngine >& engine );
private:
...
};
Italian C++ Community
Solution #3: Service Locator
i=22
// MinePlant:
auto e =
make_shared< SafeEngine >( engineOutput, gasSensor );
ServiceLocator::Instance().Engine( e );
// SumpPump:
SumpPump::SumpPump() :
engine( ServiceLocator::Instance().Engine() )
{
}
Italian C++ Community
Solution #3: Properties
Clients aware of the locator
Dependencies not explicit / evident
Dependencies not checked by compiler
i=23
Italian C++ Community
Solution #3: Summary
SafeEngine class added
MinePlant modified
i=24
Italian C++ Community
Solution #4: Dependency Injection
Dependency Injection is when
you have something setting the
dependencies for you.
i=25
Italian C++ Community
Solution #4: Dependency Injection
Classes don't create their own dependencies
They're passed from outside
i=26
Italian C++ Community
Dependency Injection
i=27
auto gasSensor = ...
auto alarmOutput = make_shared<DigitalOutput>("/dev/ttyS0");
auto alarm = make_shared<Alarm>(alarmOutput);
auto gasAlarm = make_shared<GasAlarm>(gasSensor,alarm);
auto engineOutput = make_shared<DigitalOutput>("/dev/ttyS1");
auto engine = make_shared<PumpEngine>(engineOutput);
auto pump = make_shared<SumpPump>(engine);
Italian C++ Community
Dependency Injection
i=28
auto gasSensor = ...
auto alarmOutput = make_shared<DigitalOutput>("/dev/ttyS0");
auto alarm = make_shared<Alarm>(alarmOutput);
auto gasAlarm = make_shared<GasAlarm>(gasSensor,alarm);
auto engineOutput = make_shared<DigitalOutput>("/dev/ttyS1");
// auto engine = make_shared<PumpEngine>(engineOutput);
auto engine = make_shared<SafeEngine>(engineOutput,gasSensor);
auto pump = make_shared<SumpPump>(engine);
Italian C++ Community
Solution #4: Properties
Complete separation between:
application logic (classes)
wiring (main/builder)
i=29
Italian C++ Community
Solution #4: Summary
SafeEngine class added
MinePlant modified (one liner)
i=30
Italian C++ Community i=31
Can we do
BETTER
SafeEngine must be added anyway.
… can we remove the one liner in MinePlant?
?
Italian C++ Community i=32
Configuration
DrivenWIRING
moving creation and wiring outside the code,
in a configuration file
Italian C++ Community
Why?
To easily get extensibility/contraction
(without having to touch zillion files and
recompile everything)
i=33
Italian C++ Community
From Identifiers to Strings
Improving Previous Solution:
Objects creation from string
Objects identified by name
Objects connected by name
i=34
Italian C++ Community
Run-time Reflection Missing…
Create("Foo") vs new Foo
Enumerate the dependencies
“Inject” the right object address in a
class dependency
i=35
Italian C++ Community
Solution #5: Dependency Injection +
Dependency Injection is when you
have something setting the
dependencies for you.
…this something is usually a
framework.
i=36
Italian C++ Community
Existing libraries (C++)
i=37
QtIOCContainer
Sauce
DICPP
Hypodermic2
Pococapsule
Main issues:
Compile time injection only
Code generators needed
Italian C++ Community
Enter Wallaroo Library
i=38
wallaroo.googlecode.com
wallarooC++ Dependency Injection
Italian C++ Community
Creating objects
i=39
Catalog catalog;
...
catalog.Create("alarmOutput","DigitalOutput","/dev/ttyS0");
catalog.Create("alarm","Alarm");
catalog.Create("gasAlarm","GasAlarm");
catalog.Create("engineOutput","DigitalOutput","/dev/ttyS1");
catalog.Create("pump","SumpPump");
catalog.Create("engine","SafeEngine");
Italian C++ Community
Creating objects (from cfg)
i=40
<parts>
<part>
<name>pump</name>
<class>SumpPump</class>
</part>
<part>
<name>engine</name>
<class>SafeEngine</class>
</part>
</parts>
...
Catalog catalog;
XmlConfiguration
file("wiring.xml");
file.Fill( catalog );
...
Italian C++ Community
Object lookup by name
i=41
shared_ptr< SumpPump > pump = catalog[ "pump" ];
Italian C++ Community
Connect Things by name (DSL)
i=42
Catalog catalog;
// fill catalog
...
use(catalog["alarmOutput"]).as("out").of(catalog["alarm" ]);
use(catalog["safeEngine"]).as("engine").of(catalog["pump"]);
Italian C++ Community
Connect Things by name (DSL)
i=43
Catalog catalog;
// fill catalog
...
wallaroo_within( catalog )
{
use( "alarmOutput" ).as( "out" ).of( "alarm" );
use( "safeEngine" ).as( "engine" ).of( "pump" );
}
Italian C++ Community
Connect Things by name (from cfg)
i=44
<wiring>
<wire>
<source>alarm</source>
<dest>alarmOutput</dest>
<collaborator>out</collaborator>
</wire>
<wire>
<source>pump</source>
<dest>safeEngine</dest>
<collaborator>engine</collaborator>
</wire>
</wiring>
Catalog catalog;
...
XmlConfiguration
file( "wiring.xml" );
file.Fill( catalog );
catalog.CheckWiring();
...
Italian C++ Community
Class Declaration
i=45
#include "wallaroo/registered.h"
using namespace wallaroo;
class SumpPump : public Part
{
public:
SumpPump( int id );
private:
Collaborator< Engine > engine;
};
Italian C++ Community
Class Registration
i=46
WALLAROO_REGISTER( SumpPump, int )
SumpPump::SumpPump( int id ) :
engine( "engine", RegistrationToken() )
{
...
}
// other methods definition here
...
Italian C++ Community
Shared Libraries – AKA plugins (code)
i=47
Plugin::Load( "safeengine" + Plugin::Suffix() );
// Plugin::Suffix() expands to .dll or .so according
to the OS
Italian C++ Community
Shared Libraries – AKA plugins (cfg)
i=48
<plugins>
<shared>safeengine</shared>
</plugins>
Catalog catalog;
XmlConfiguration file( "wiring.xml" );
// load the shared libraries specified in the configuration file:
file.LoadPlugins();
file.Fill( catalog );
// throws a WiringError exception if any dependency is missing:
catalog.CheckWiring();
Italian C++ Community
Collections
i=49
class Car : public wallaroo::Part
{
...
private:
Collaborator< Engine > engine;
Collaborator< AirConditioning, optional > airConditioning;
Collaborator< Airbag, collection > airbags;
Collaborator< Speaker, collection, std::list > speakers;
Collaborator< Seat, bounded_collection< 2, 6 > > seats;
};
Italian C++ Community
Checks
i=50
if ( !catalog.IsWiringOk() )
{
// error handling
}
catalog.CheckWiring() // throws exception
Italian C++ Community
Initialization
i=51
class Part
{
...
public:
virtual void Init() {}
...
};
catalog.Init() // calls Part::Init for each part in catalog
Italian C++ Community
Wallaroo Internals
WALLAROO_REGISTER declares a static object.
Its constructor creates a factory and puts it in a table, with the class
name as key.
Catalog::Create uses the factory to put a new instance in the catalog.
wallaroo::Part has a table of <name, Collaborator>
i=52
Italian C++ Community
Wallaroo Internals
shared_ptr< Foo > foo = catalog[ “foo” ];
catalog[ “foo” ] returns a class that defines operator
shared_ptr< T >()
Collaborator uses weak_ptr for the dependency
Collaborator defines operator shared_ptr() and operator->()
i=53
Italian C++ Community
Wallaroo Internals
i=54
wallaroo_within( catalog )
{
use("alarmOutput").as("out").of("alarm");
use("safeEngine").as("engine").of("pump");
}
for ( Context c(catalog);c.FirstTime();c.Terminate() )
{
use("alarmOutput").as("out").of("alarm");
use("safeEngine").as("engine").of("pump");
}
Italian C++ Community
Wallaroo Strengths
Lightweight (header file only)
Portable
Type Safe
DSL syntax for object creation and wiring
Configuration driven wiring (xml and json)
Shared library support (plugin)
C++11/14 or boost interface
No code generators
i=55
Italian C++ Community
Design is a balance of forces
Intrusive VS Non Intrusive
Non Intrusive Solutions can manage existing classes but
require code generators for configuration driven wiring
i=56
Italian C++ Community
Design is a balance of forces
Configuration-driven wiring
VS static type checking
By moving the wiring in a configuration file, we give up the
static type checking.
But it’s ok, since you build your system at startup.
i=57
Italian C++ Community
Action Points
Real OOD (no controllers / managers)
Manual Dependency Injection
Wallaroo (configuration-driven wiring)
i=58
Italian C++ Community
References & Credits
Me: @DPallastrelli
Me: it.linkedin.com/in/pallad
Rate me: https://joind.in/12277
Wallaroo: wallaroo.googlecode.com
i=59
MinePlant example from Carlo Pescio's blog (http://www.carlopescio.com/2012/03/life-
without-controller-case-1.html)
Wiring Picture: By Gael Mace (Own work (Personal photograph))
[CC-BY-3.0 (http://creativecommons.org/licenses/by/3.0)], via Wikimedia Commons

Contenu connexe

Tendances

Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all startedKernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all startedAnne Nicolas
 
Making Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVMMaking Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVMRafael Winterhalter
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVAVikram Kalyani
 
Chapitre 6 traitement des exceptions
Chapitre 6  traitement des exceptionsChapitre 6  traitement des exceptions
Chapitre 6 traitement des exceptionsAmir Souissi
 
Robot framework 을 이용한 기능 테스트 자동화
Robot framework 을 이용한 기능 테스트 자동화Robot framework 을 이용한 기능 테스트 자동화
Robot framework 을 이용한 기능 테스트 자동화Jaehoon Oh
 
Effective testing with pytest
Effective testing with pytestEffective testing with pytest
Effective testing with pytestHector Canto
 
Base de donnees Avancees et Intro à NoSQL.ppt
Base de donnees Avancees et Intro à  NoSQL.pptBase de donnees Avancees et Intro à  NoSQL.ppt
Base de donnees Avancees et Intro à NoSQL.pptIdriss22
 
Software Vulnerabilities in C and C++ (CppCon 2018)
Software Vulnerabilities in C and C++ (CppCon 2018)Software Vulnerabilities in C and C++ (CppCon 2018)
Software Vulnerabilities in C and C++ (CppCon 2018)Patricia Aas
 
Java - notions de bases pour développeur
Java - notions de bases pour développeurJava - notions de bases pour développeur
Java - notions de bases pour développeurJean David Olekhnovitch
 
Expanding the control over the operating system from the database
Expanding the control over the operating system from the databaseExpanding the control over the operating system from the database
Expanding the control over the operating system from the databaseBernardo Damele A. G.
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!Jakub Kubrynski
 
API Asynchrones en Java 8
API Asynchrones en Java 8API Asynchrones en Java 8
API Asynchrones en Java 8José Paumard
 

Tendances (20)

Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all startedKernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
 
Inside the jvm
Inside the jvmInside the jvm
Inside the jvm
 
JVM Under The Hood WDI.pdf
JVM Under The Hood WDI.pdfJVM Under The Hood WDI.pdf
JVM Under The Hood WDI.pdf
 
Making Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVMMaking Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVM
 
JAVA 8
JAVA 8JAVA 8
JAVA 8
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 
Chapitre 6 traitement des exceptions
Chapitre 6  traitement des exceptionsChapitre 6  traitement des exceptions
Chapitre 6 traitement des exceptions
 
Robot framework 을 이용한 기능 테스트 자동화
Robot framework 을 이용한 기능 테스트 자동화Robot framework 을 이용한 기능 테스트 자동화
Robot framework 을 이용한 기능 테스트 자동화
 
Effective testing with pytest
Effective testing with pytestEffective testing with pytest
Effective testing with pytest
 
Base de donnees Avancees et Intro à NoSQL.ppt
Base de donnees Avancees et Intro à  NoSQL.pptBase de donnees Avancees et Intro à  NoSQL.ppt
Base de donnees Avancees et Intro à NoSQL.ppt
 
Software Vulnerabilities in C and C++ (CppCon 2018)
Software Vulnerabilities in C and C++ (CppCon 2018)Software Vulnerabilities in C and C++ (CppCon 2018)
Software Vulnerabilities in C and C++ (CppCon 2018)
 
Spring boot jpa
Spring boot jpaSpring boot jpa
Spring boot jpa
 
Java - notions de bases pour développeur
Java - notions de bases pour développeurJava - notions de bases pour développeur
Java - notions de bases pour développeur
 
Expanding the control over the operating system from the database
Expanding the control over the operating system from the databaseExpanding the control over the operating system from the database
Expanding the control over the operating system from the database
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
 
Kernel crashdump
Kernel crashdumpKernel crashdump
Kernel crashdump
 
API Asynchrones en Java 8
API Asynchrones en Java 8API Asynchrones en Java 8
API Asynchrones en Java 8
 

En vedette

Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)Daniele Pallastrelli
 
C++ Dependency Management 2.0
C++ Dependency Management 2.0C++ Dependency Management 2.0
C++ Dependency Management 2.0Patrick Charrier
 
CMake - Introduction and best practices
CMake - Introduction and best practicesCMake - Introduction and best practices
CMake - Introduction and best practicesDaniel Pfeifer
 
CMake: Improving Software Quality and Process
CMake: Improving Software Quality and ProcessCMake: Improving Software Quality and Process
CMake: Improving Software Quality and ProcessMarcus Hanwell
 
Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014biicode
 
Multithreading with Boost Thread and Intel TBB
Multithreading with Boost Thread and Intel TBBMultithreading with Boost Thread and Intel TBB
Multithreading with Boost Thread and Intel TBBPatrick Charrier
 
Long Life Software
Long Life SoftwareLong Life Software
Long Life SoftwareMike Long
 
Water alternating gas (WAG) - A Enhanced Oil Recovery technique
Water alternating gas (WAG) - A Enhanced Oil Recovery techniqueWater alternating gas (WAG) - A Enhanced Oil Recovery technique
Water alternating gas (WAG) - A Enhanced Oil Recovery techniqueIbrahim Muhammad
 
Intelligent well completio nm
Intelligent well completio nmIntelligent well completio nm
Intelligent well completio nmbasil_c
 
Intelligent well completion
Intelligent well completionIntelligent well completion
Intelligent well completionAkshaya Mishra
 
Clean Code II - Dependency Injection
Clean Code II - Dependency InjectionClean Code II - Dependency Injection
Clean Code II - Dependency InjectionTheo Jungeblut
 
Agile for Embedded & System Software Development : Presented by Priyank KS
Agile for Embedded & System Software Development : Presented by Priyank KS Agile for Embedded & System Software Development : Presented by Priyank KS
Agile for Embedded & System Software Development : Presented by Priyank KS oGuild .
 
Interfaces to ubiquitous computing
Interfaces to ubiquitous computingInterfaces to ubiquitous computing
Interfaces to ubiquitous computingswati sonawane
 
iBeacons: Security and Privacy?
iBeacons: Security and Privacy?iBeacons: Security and Privacy?
iBeacons: Security and Privacy?Jim Fenton
 
Demystifying dependency Injection: Dagger and Toothpick
Demystifying dependency Injection: Dagger and ToothpickDemystifying dependency Injection: Dagger and Toothpick
Demystifying dependency Injection: Dagger and ToothpickDanny Preussler
 
Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...
Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...
Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...Paolo Sammicheli
 

En vedette (20)

Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)
 
C++ Dependency Management 2.0
C++ Dependency Management 2.0C++ Dependency Management 2.0
C++ Dependency Management 2.0
 
CMake - Introduction and best practices
CMake - Introduction and best practicesCMake - Introduction and best practices
CMake - Introduction and best practices
 
CMake: Improving Software Quality and Process
CMake: Improving Software Quality and ProcessCMake: Improving Software Quality and Process
CMake: Improving Software Quality and Process
 
Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Multithreading with Boost Thread and Intel TBB
Multithreading with Boost Thread and Intel TBBMultithreading with Boost Thread and Intel TBB
Multithreading with Boost Thread and Intel TBB
 
Long Life Software
Long Life SoftwareLong Life Software
Long Life Software
 
Water alternating gas (WAG) - A Enhanced Oil Recovery technique
Water alternating gas (WAG) - A Enhanced Oil Recovery techniqueWater alternating gas (WAG) - A Enhanced Oil Recovery technique
Water alternating gas (WAG) - A Enhanced Oil Recovery technique
 
Intelligent well completio nm
Intelligent well completio nmIntelligent well completio nm
Intelligent well completio nm
 
Intelligent well completion
Intelligent well completionIntelligent well completion
Intelligent well completion
 
Clean Code II - Dependency Injection
Clean Code II - Dependency InjectionClean Code II - Dependency Injection
Clean Code II - Dependency Injection
 
Agile for Embedded & System Software Development : Presented by Priyank KS
Agile for Embedded & System Software Development : Presented by Priyank KS Agile for Embedded & System Software Development : Presented by Priyank KS
Agile for Embedded & System Software Development : Presented by Priyank KS
 
3.7 heap sort
3.7 heap sort3.7 heap sort
3.7 heap sort
 
Interfaces to ubiquitous computing
Interfaces to ubiquitous computingInterfaces to ubiquitous computing
Interfaces to ubiquitous computing
 
iBeacons: Security and Privacy?
iBeacons: Security and Privacy?iBeacons: Security and Privacy?
iBeacons: Security and Privacy?
 
Intelligent well completions
Intelligent well completionsIntelligent well completions
Intelligent well completions
 
Demystifying dependency Injection: Dagger and Toothpick
Demystifying dependency Injection: Dagger and ToothpickDemystifying dependency Injection: Dagger and Toothpick
Demystifying dependency Injection: Dagger and Toothpick
 
Dependency Injection with Apex
Dependency Injection with ApexDependency Injection with Apex
Dependency Injection with Apex
 
Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...
Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...
Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...
 

Similaire à Going native with less coupling: Dependency Injection in C++

Meetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son applicationMeetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son applicationThibault Even
 
generate IP CORES
generate IP CORESgenerate IP CORES
generate IP CORESguest296013
 
Contiki Operating system tutorial
Contiki Operating system tutorialContiki Operating system tutorial
Contiki Operating system tutorialSalah Amean
 
Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)Unity Technologies
 
8.3 program structure (1 hour)
8.3 program structure (1 hour)8.3 program structure (1 hour)
8.3 program structure (1 hour)akmalfahmi
 
Angular performance slides
Angular performance slidesAngular performance slides
Angular performance slidesDavid Barreto
 
Idea to Production - with Gitlab and Kubernetes
Idea to Production  - with Gitlab and KubernetesIdea to Production  - with Gitlab and Kubernetes
Idea to Production - with Gitlab and KubernetesSimon Dittlmann
 
The Ring programming language version 1.3 book - Part 62 of 88
The Ring programming language version 1.3 book - Part 62 of 88The Ring programming language version 1.3 book - Part 62 of 88
The Ring programming language version 1.3 book - Part 62 of 88Mahmoud Samir Fayed
 
Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)Peter R. Egli
 
The Ring programming language version 1.2 book - Part 59 of 84
The Ring programming language version 1.2 book - Part 59 of 84The Ring programming language version 1.2 book - Part 59 of 84
The Ring programming language version 1.2 book - Part 59 of 84Mahmoud Samir Fayed
 
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...Andrey Karpov
 
High performance web programming with C++14
High performance web programming with C++14High performance web programming with C++14
High performance web programming with C++14Matthieu Garrigues
 
The Ring programming language version 1.7 book - Part 82 of 196
The Ring programming language version 1.7 book - Part 82 of 196The Ring programming language version 1.7 book - Part 82 of 196
The Ring programming language version 1.7 book - Part 82 of 196Mahmoud Samir Fayed
 
embeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptxembeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptxsangeetaSS
 
The Ring programming language version 1.4 book - Part 22 of 30
The Ring programming language version 1.4 book - Part 22 of 30The Ring programming language version 1.4 book - Part 22 of 30
The Ring programming language version 1.4 book - Part 22 of 30Mahmoud Samir Fayed
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdfVcTrn1
 
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docxfelicidaddinwoodie
 

Similaire à Going native with less coupling: Dependency Injection in C++ (20)

Meetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son applicationMeetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son application
 
generate IP CORES
generate IP CORESgenerate IP CORES
generate IP CORES
 
Contiki Operating system tutorial
Contiki Operating system tutorialContiki Operating system tutorial
Contiki Operating system tutorial
 
Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)
 
8.3 program structure (1 hour)
8.3 program structure (1 hour)8.3 program structure (1 hour)
8.3 program structure (1 hour)
 
Angular performance slides
Angular performance slidesAngular performance slides
Angular performance slides
 
Idea to Production - with Gitlab and Kubernetes
Idea to Production  - with Gitlab and KubernetesIdea to Production  - with Gitlab and Kubernetes
Idea to Production - with Gitlab and Kubernetes
 
The Ring programming language version 1.3 book - Part 62 of 88
The Ring programming language version 1.3 book - Part 62 of 88The Ring programming language version 1.3 book - Part 62 of 88
The Ring programming language version 1.3 book - Part 62 of 88
 
Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)
 
Cpp
CppCpp
Cpp
 
Angular 2 in-1
Angular 2 in-1 Angular 2 in-1
Angular 2 in-1
 
The Ring programming language version 1.2 book - Part 59 of 84
The Ring programming language version 1.2 book - Part 59 of 84The Ring programming language version 1.2 book - Part 59 of 84
The Ring programming language version 1.2 book - Part 59 of 84
 
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...
 
2621008 - C++ 1
2621008 -  C++ 12621008 -  C++ 1
2621008 - C++ 1
 
High performance web programming with C++14
High performance web programming with C++14High performance web programming with C++14
High performance web programming with C++14
 
The Ring programming language version 1.7 book - Part 82 of 196
The Ring programming language version 1.7 book - Part 82 of 196The Ring programming language version 1.7 book - Part 82 of 196
The Ring programming language version 1.7 book - Part 82 of 196
 
embeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptxembeddedc-lecture1-160404055102.pptx
embeddedc-lecture1-160404055102.pptx
 
The Ring programming language version 1.4 book - Part 22 of 30
The Ring programming language version 1.4 book - Part 22 of 30The Ring programming language version 1.4 book - Part 22 of 30
The Ring programming language version 1.4 book - Part 22 of 30
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdf
 
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx
 

Dernier

Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROmotivationalword821
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 

Dernier (20)

Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTRO
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 

Going native with less coupling: Dependency Injection in C++

  • 1. www.italiancpp.org Going native with less coupling Dependency Injection in C++
  • 2. Italian C++ Community Style 1: [Fake] OO Design i=2 CONTROLLER / MANAGER Dumb object Dumb object Dumb object Dumb object Dumb object Dumb object Dumb object Dumb object
  • 3. Italian C++ Community Style 2: [True] OO design i=3 Smart Object Smart Object Smart Object Smart Object Smart Object Smart Object Smart Object Smart Object Smart Object Smart Object Smart Object Smart Object Smart Object Smart Object
  • 4. Italian C++ Community OO Architecture Benefits i=4 Up Scalability (extension) Down Scalability (contraction) Reusability Safety (testability)
  • 5. Italian C++ Community i=5 The Wiring Issue In a True Modular Architecture, wiring is critical
  • 6. Italian C++ Community The Wiring Issue When you've got a Good Architecture, you deal with requirements changes by adding/removing/substituting objects So, we need a simple way to: Change the type of the objects Create new objects Modify the wiring of the objects i=6
  • 7. Italian C++ Community i=7 Life without a CONTROLLER Example taken from: Carlo Pescio's blog – March 2012
  • 8. Italian C++ Community First Design i=8 MinePlant SumpPump +Drain() PumpEngine +On() +Off() DigitalOutput +Write() GasSensor <<interface>> +IsCritical() GasAlarm +Watch() 1..* Alarm +On() +Off() SafeEngine
  • 9. Italian C++ Community Requirements Change i=9 MinePlant SumpPump +Drain() PumpEngine +On() +Off() DigitalOutput +Write() GasSensor <<interface>> +IsCritical() GasAlarm +Watch() 1..* Alarm +On() +Off() SafeEngine
  • 10. Italian C++ Community Extension The design is robust: I only need to add a class But… Who creates SafeEngine instead of PumpEngine? How does SafeEngine get the pointer to the GasSensor (the same used by GasAlarm)? i=10
  • 11. Italian C++ Community Solution #1: Local Creation Each class creates its own dependencies i=11
  • 12. Italian C++ Community i=12 MinePlant SumpPump +Drain() PumpEngine +On() +Off() DigitalOutput +Write() GasSensor <<interface>> +IsCritical() GasAlarm +Watch() 1..* Alarm +On() +Off() SafeEngine <<create>>
  • 13. Italian C++ Community Solution #1: Consequences The SumpPump constructor creates a SafeEngine instead of a PumpEngine. … but SafeEngine needs a pointer to the GasSensor instance already used by GasAlarm. So, we must pass it as a parameter to SumpPump constructor. i=13
  • 14. Italian C++ Community Solution #1: Properties If I need to change the concrete type, I have to modify the client. It's difficult to reuse the same client class (even in the same application). i=14
  • 15. Italian C++ Community Solution #1: Summary SafeEngine class added SumpPump constructor modified MinePlant modified i=15
  • 16. Italian C++ Community Solution #2: Factory (not the GoF factory) Create objects without exposing the instantiation logic to the client i=16
  • 17. Italian C++ Community i=17 <<create>>
  • 18. Italian C++ Community Solution #2: Consequences The SumpPump constructor takes a Factory as parameter. PumpEngineFactory instantiates a PumpEngine. SafeEngineFactory instantiates a SafeEngine. SafeEngine still needs a pointer to the GasSensor instance already used by GasAlarm, so we must pass it to the SafeEngineFactory constructor. i=18
  • 19. Italian C++ Community Solution #2: Summary SafeEngine class added SafeEngineFactory added MinePlant modified i=19
  • 20. Italian C++ Community Solution #3: Service Locator It’s a registry containing the instances to use i=20
  • 21. Italian C++ Community Solution #3: Service Locator i=21 class ServiceLocator { public: static ServiceLocator& Instance(); shared_ptr< PumpEngine > Engine(); void Engine( const shared_ptr< PumpEngine >& engine ); private: ... };
  • 22. Italian C++ Community Solution #3: Service Locator i=22 // MinePlant: auto e = make_shared< SafeEngine >( engineOutput, gasSensor ); ServiceLocator::Instance().Engine( e ); // SumpPump: SumpPump::SumpPump() : engine( ServiceLocator::Instance().Engine() ) { }
  • 23. Italian C++ Community Solution #3: Properties Clients aware of the locator Dependencies not explicit / evident Dependencies not checked by compiler i=23
  • 24. Italian C++ Community Solution #3: Summary SafeEngine class added MinePlant modified i=24
  • 25. Italian C++ Community Solution #4: Dependency Injection Dependency Injection is when you have something setting the dependencies for you. i=25
  • 26. Italian C++ Community Solution #4: Dependency Injection Classes don't create their own dependencies They're passed from outside i=26
  • 27. Italian C++ Community Dependency Injection i=27 auto gasSensor = ... auto alarmOutput = make_shared<DigitalOutput>("/dev/ttyS0"); auto alarm = make_shared<Alarm>(alarmOutput); auto gasAlarm = make_shared<GasAlarm>(gasSensor,alarm); auto engineOutput = make_shared<DigitalOutput>("/dev/ttyS1"); auto engine = make_shared<PumpEngine>(engineOutput); auto pump = make_shared<SumpPump>(engine);
  • 28. Italian C++ Community Dependency Injection i=28 auto gasSensor = ... auto alarmOutput = make_shared<DigitalOutput>("/dev/ttyS0"); auto alarm = make_shared<Alarm>(alarmOutput); auto gasAlarm = make_shared<GasAlarm>(gasSensor,alarm); auto engineOutput = make_shared<DigitalOutput>("/dev/ttyS1"); // auto engine = make_shared<PumpEngine>(engineOutput); auto engine = make_shared<SafeEngine>(engineOutput,gasSensor); auto pump = make_shared<SumpPump>(engine);
  • 29. Italian C++ Community Solution #4: Properties Complete separation between: application logic (classes) wiring (main/builder) i=29
  • 30. Italian C++ Community Solution #4: Summary SafeEngine class added MinePlant modified (one liner) i=30
  • 31. Italian C++ Community i=31 Can we do BETTER SafeEngine must be added anyway. … can we remove the one liner in MinePlant? ?
  • 32. Italian C++ Community i=32 Configuration DrivenWIRING moving creation and wiring outside the code, in a configuration file
  • 33. Italian C++ Community Why? To easily get extensibility/contraction (without having to touch zillion files and recompile everything) i=33
  • 34. Italian C++ Community From Identifiers to Strings Improving Previous Solution: Objects creation from string Objects identified by name Objects connected by name i=34
  • 35. Italian C++ Community Run-time Reflection Missing… Create("Foo") vs new Foo Enumerate the dependencies “Inject” the right object address in a class dependency i=35
  • 36. Italian C++ Community Solution #5: Dependency Injection + Dependency Injection is when you have something setting the dependencies for you. …this something is usually a framework. i=36
  • 37. Italian C++ Community Existing libraries (C++) i=37 QtIOCContainer Sauce DICPP Hypodermic2 Pococapsule Main issues: Compile time injection only Code generators needed
  • 38. Italian C++ Community Enter Wallaroo Library i=38 wallaroo.googlecode.com wallarooC++ Dependency Injection
  • 39. Italian C++ Community Creating objects i=39 Catalog catalog; ... catalog.Create("alarmOutput","DigitalOutput","/dev/ttyS0"); catalog.Create("alarm","Alarm"); catalog.Create("gasAlarm","GasAlarm"); catalog.Create("engineOutput","DigitalOutput","/dev/ttyS1"); catalog.Create("pump","SumpPump"); catalog.Create("engine","SafeEngine");
  • 40. Italian C++ Community Creating objects (from cfg) i=40 <parts> <part> <name>pump</name> <class>SumpPump</class> </part> <part> <name>engine</name> <class>SafeEngine</class> </part> </parts> ... Catalog catalog; XmlConfiguration file("wiring.xml"); file.Fill( catalog ); ...
  • 41. Italian C++ Community Object lookup by name i=41 shared_ptr< SumpPump > pump = catalog[ "pump" ];
  • 42. Italian C++ Community Connect Things by name (DSL) i=42 Catalog catalog; // fill catalog ... use(catalog["alarmOutput"]).as("out").of(catalog["alarm" ]); use(catalog["safeEngine"]).as("engine").of(catalog["pump"]);
  • 43. Italian C++ Community Connect Things by name (DSL) i=43 Catalog catalog; // fill catalog ... wallaroo_within( catalog ) { use( "alarmOutput" ).as( "out" ).of( "alarm" ); use( "safeEngine" ).as( "engine" ).of( "pump" ); }
  • 44. Italian C++ Community Connect Things by name (from cfg) i=44 <wiring> <wire> <source>alarm</source> <dest>alarmOutput</dest> <collaborator>out</collaborator> </wire> <wire> <source>pump</source> <dest>safeEngine</dest> <collaborator>engine</collaborator> </wire> </wiring> Catalog catalog; ... XmlConfiguration file( "wiring.xml" ); file.Fill( catalog ); catalog.CheckWiring(); ...
  • 45. Italian C++ Community Class Declaration i=45 #include "wallaroo/registered.h" using namespace wallaroo; class SumpPump : public Part { public: SumpPump( int id ); private: Collaborator< Engine > engine; };
  • 46. Italian C++ Community Class Registration i=46 WALLAROO_REGISTER( SumpPump, int ) SumpPump::SumpPump( int id ) : engine( "engine", RegistrationToken() ) { ... } // other methods definition here ...
  • 47. Italian C++ Community Shared Libraries – AKA plugins (code) i=47 Plugin::Load( "safeengine" + Plugin::Suffix() ); // Plugin::Suffix() expands to .dll or .so according to the OS
  • 48. Italian C++ Community Shared Libraries – AKA plugins (cfg) i=48 <plugins> <shared>safeengine</shared> </plugins> Catalog catalog; XmlConfiguration file( "wiring.xml" ); // load the shared libraries specified in the configuration file: file.LoadPlugins(); file.Fill( catalog ); // throws a WiringError exception if any dependency is missing: catalog.CheckWiring();
  • 49. Italian C++ Community Collections i=49 class Car : public wallaroo::Part { ... private: Collaborator< Engine > engine; Collaborator< AirConditioning, optional > airConditioning; Collaborator< Airbag, collection > airbags; Collaborator< Speaker, collection, std::list > speakers; Collaborator< Seat, bounded_collection< 2, 6 > > seats; };
  • 50. Italian C++ Community Checks i=50 if ( !catalog.IsWiringOk() ) { // error handling } catalog.CheckWiring() // throws exception
  • 51. Italian C++ Community Initialization i=51 class Part { ... public: virtual void Init() {} ... }; catalog.Init() // calls Part::Init for each part in catalog
  • 52. Italian C++ Community Wallaroo Internals WALLAROO_REGISTER declares a static object. Its constructor creates a factory and puts it in a table, with the class name as key. Catalog::Create uses the factory to put a new instance in the catalog. wallaroo::Part has a table of <name, Collaborator> i=52
  • 53. Italian C++ Community Wallaroo Internals shared_ptr< Foo > foo = catalog[ “foo” ]; catalog[ “foo” ] returns a class that defines operator shared_ptr< T >() Collaborator uses weak_ptr for the dependency Collaborator defines operator shared_ptr() and operator->() i=53
  • 54. Italian C++ Community Wallaroo Internals i=54 wallaroo_within( catalog ) { use("alarmOutput").as("out").of("alarm"); use("safeEngine").as("engine").of("pump"); } for ( Context c(catalog);c.FirstTime();c.Terminate() ) { use("alarmOutput").as("out").of("alarm"); use("safeEngine").as("engine").of("pump"); }
  • 55. Italian C++ Community Wallaroo Strengths Lightweight (header file only) Portable Type Safe DSL syntax for object creation and wiring Configuration driven wiring (xml and json) Shared library support (plugin) C++11/14 or boost interface No code generators i=55
  • 56. Italian C++ Community Design is a balance of forces Intrusive VS Non Intrusive Non Intrusive Solutions can manage existing classes but require code generators for configuration driven wiring i=56
  • 57. Italian C++ Community Design is a balance of forces Configuration-driven wiring VS static type checking By moving the wiring in a configuration file, we give up the static type checking. But it’s ok, since you build your system at startup. i=57
  • 58. Italian C++ Community Action Points Real OOD (no controllers / managers) Manual Dependency Injection Wallaroo (configuration-driven wiring) i=58
  • 59. Italian C++ Community References & Credits Me: @DPallastrelli Me: it.linkedin.com/in/pallad Rate me: https://joind.in/12277 Wallaroo: wallaroo.googlecode.com i=59 MinePlant example from Carlo Pescio's blog (http://www.carlopescio.com/2012/03/life- without-controller-case-1.html) Wiring Picture: By Gael Mace (Own work (Personal photograph)) [CC-BY-3.0 (http://creativecommons.org/licenses/by/3.0)], via Wikimedia Commons