advanced oo patterns - qafoo.com · php uk conference 2011 tobias schlitt february 25, 2011....

112
Advanced OO Patterns PHP UK Conference 2011 Tobias Schlitt <[email protected]> February 25, 2011

Upload: others

Post on 18-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Advanced OO PatternsPHP UK Conference 2011

Tobias Schlitt <[email protected]>

February 25, 2011

Advanced OO Patterns 2 / 40

Outline

Introduction

Dependency injection

Lazy initialization

Data storage

Advanced OO Patterns 3 / 40

About me

I Degree in computer sience

I More than 10 years ofprofessional PHP

I Open source enthusiastI Apache Zeta ComponentsI ArbitI PHPUnitI . . .

Advanced OO Patterns 3 / 40

About me

I Degree in computer sience

I More than 10 years ofprofessional PHP

I Open source enthusiastI Apache Zeta ComponentsI ArbitI PHPUnitI . . .

Advanced OO Patterns 3 / 40

About me

I Degree in computer sience

I More than 10 years ofprofessional PHP

I Open source enthusiastI Apache Zeta ComponentsI ArbitI PHPUnitI . . .

Advanced OO Patterns 3 / 40

About me

I Degree in computer sience

I More than 10 years ofprofessional PHP

I Open source enthusiastI Apache Zeta ComponentsI ArbitI PHPUnitI . . .

Co-Founder ofQafoo GmbH

http://qafoo.com

Qafoopassion for software quality

Advanced OO Patterns 3 / 40

About me

I Degree in computer sience

I More than 10 years ofprofessional PHP

I Open source enthusiastI Apache Zeta ComponentsI ArbitI PHPUnitI . . .

Co-Founder ofQafoo GmbH

http://qafoo.com

Qafoopassion for software quality

We help people to producehigh quality PHP code.

Advanced OO Patterns 4 / 40

Disclaimer

I This talk cannot be in depth

I This talk shall inspire you

I This talk will not show UML diagrams

I This talk will show you quite some code

I This talk can seriously harm your coding habbits

Advanced OO Patterns 4 / 40

Disclaimer

I This talk cannot be in depth

I This talk shall inspire you

I This talk will not show UML diagrams

I This talk will show you quite some code

I This talk can seriously harm your coding habbits

Advanced OO Patterns 4 / 40

Disclaimer

I This talk cannot be in depth

I This talk shall inspire you

I This talk will not show UML diagrams

I This talk will show you quite some code

I This talk can seriously harm your coding habbits

Advanced OO Patterns 4 / 40

Disclaimer

I This talk cannot be in depth

I This talk shall inspire you

I This talk will not show UML diagrams

I This talk will show you quite some code

I This talk can seriously harm your coding habbits

Advanced OO Patterns 4 / 40

Disclaimer

I This talk cannot be in depth

I This talk shall inspire you

I This talk will not show UML diagrams

I This talk will show you quite some code

I This talk can seriously harm your coding habbits

Advanced OO Patterns 5 / 40

Patterns are . . .

I . . . universal solutions.

I . . . good habbits.

I . . . coding templates.

Advanced OO Patterns 5 / 40

Patterns are . . .

I . . . universal solutions.

I . . . good habbits.

I . . . coding templates.

Advanced OO Patterns 5 / 40

Patterns are . . .

I . . . universal solutions.

I . . . good habbits.

I . . . coding templates.

Advanced OO Patterns 5 / 40

Patterns are . . .

I . . . universal solutions.

I . . . good habbits.

I . . . coding templates.

Advanced OO Patterns 5 / 40

Patterns are . . .

I . . . universal solutions.

I . . . good habbits.

I . . . coding templates.

Advanced OO Patterns 5 / 40

Patterns are . . .

I . . . universal solutions.

I . . . good habbits.

I . . . coding templates.

Advanced OO Patterns 5 / 40

Patterns are . . .

I . . . universal solutions.

I . . . good habbits.

I . . . coding templates.

. . . names for proven ideas how a certain class of problemscan be solved.

Advanced OO Patterns 6 / 40

Patterns are not . . .

I . . . appliable to every problem.

I . . . directly transferable to code.

I . . . always the best solution.

Advanced OO Patterns 7 / 40

Pattern classification

I Creational

I Structural

I Behavioural

I Architectural

Advanced OO Patterns 8 / 40

Well known patterns

Factory

Adapter

Iterator

Singleton

Signal / Observer

Visitor

Advanced OO Patterns 8 / 40

Well known patterns

Factory

Adapter

Iterator

Singleton

Signal / Observer

Visitor

Advanced OO Patterns 9 / 40

Outline

Introduction

Dependency injection

Lazy initialization

Data storage

Advanced OO Patterns 10 / 40

Goals of good OO design

I Modular

I Flexible

I Reusable

I Testable

I S.O.L.I.D.(http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod)

Advanced OO Patterns 10 / 40

Goals of good OO design

I Modular

I Flexible

I Reusable

I Testable

I S.O.L.I.D.(http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod)

Advanced OO Patterns 10 / 40

Goals of good OO design

I Modular

I Flexible

I Reusable

I Testable

I S.O.L.I.D.(http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod)

Advanced OO Patterns 10 / 40

Goals of good OO design

I Modular

I Flexible

I Reusable

I Testable

I S.O.L.I.D.(http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod)

Advanced OO Patterns 10 / 40

Goals of good OO design

I Modular

I Flexible

I Reusable

I Testable

I S.O.L.I.D.(http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod)

Advanced OO Patterns 11 / 40

What’s the problem here?

1 <?php23 c l a s s MessageD i spatche r4 {5 p u b l i c f u n c t i o n c o n s t r u c t ( )6 {7 $ t h i s−>messenge r s [ ] = new

JabberMessenger ( ) ;8 $ t h i s−>messenge r s [ ] = new

Mai lMessenger ( ) ;9 }

10 }1112 c l a s s Mai lMessenger implements Messenger13 {14 p u b l i c f u n c t i o n sendMessage ( $ t e x t )15 {16 myLogger : : g e t I n s t a n c e ( )−>l og ( $ t e x t

) ;17 $ t h i s−>sendMa i l ( /∗ . . . ∗/ ) ;18 }19 }

I Inflexible

I Not reusable

I Hardly testable

Advanced OO Patterns 11 / 40

What’s the problem here?

1 <?php23 c l a s s MessageD i spatche r4 {5 p u b l i c f u n c t i o n c o n s t r u c t ( )6 {7 $ t h i s−>messenge r s [ ] = new

JabberMessenger ( ) ;8 $ t h i s−>messenge r s [ ] = new

Mai lMessenger ( ) ;9 }

10 }1112 c l a s s Mai lMessenger implements Messenger13 {14 p u b l i c f u n c t i o n sendMessage ( $ t e x t )15 {16 myLogger : : g e t I n s t a n c e ( )−>l og ( $ t e x t

) ;17 $ t h i s−>sendMa i l ( /∗ . . . ∗/ ) ;18 }19 }

I Inflexible

I Not reusable

I Hardly testable

Advanced OO Patterns 11 / 40

What’s the problem here?

1 <?php23 c l a s s MessageD i spatche r4 {5 p u b l i c f u n c t i o n c o n s t r u c t ( )6 {7 $ t h i s−>messenge r s [ ] = new

JabberMessenger ( ) ;8 $ t h i s−>messenge r s [ ] = new

Mai lMessenger ( ) ;9 }

10 }1112 c l a s s Mai lMessenger implements Messenger13 {14 p u b l i c f u n c t i o n sendMessage ( $ t e x t )15 {16 myLogger : : g e t I n s t a n c e ( )−>l og ( $ t e x t

) ;17 $ t h i s−>sendMa i l ( /∗ . . . ∗/ ) ;18 }19 }

I Inflexible

I Not reusable

I Hardly testable

Advanced OO Patterns 11 / 40

What’s the problem here?

1 <?php23 c l a s s MessageD i spatche r4 {5 p u b l i c f u n c t i o n c o n s t r u c t ( )6 {7 $ t h i s−>messenge r s [ ] = new

JabberMessenger ( ) ;8 $ t h i s−>messenge r s [ ] = new

Mai lMessenger ( ) ;9 }

10 }1112 c l a s s Mai lMessenger implements Messenger13 {14 p u b l i c f u n c t i o n sendMessage ( $ t e x t )15 {16 myLogger : : g e t I n s t a n c e ( )−>l og ( $ t e x t

) ;17 $ t h i s−>sendMa i l ( /∗ . . . ∗/ ) ;18 }19 }

I Inflexible

I Not reusable

I Hardly testable

Advanced OO Patterns 12 / 40

Injecting dependencies

1 <?php23 $messenger = new MessageDi spatche r (4 a r r a y (5 new JabberMessenger ( ’ j a b b e r . example . org ’ , ’ u s e r ’ , ’ pa s s ’ ) ,6 new Mai lMessenger (7 new Mai lSmtpTransport ( ’ ma i l . example . org ’ , ’ u s e r ’ , ’ pa s s ’ ) ,8 $ l o g g e r = new Logger (9 new Logg i ngD i spa t che r (

10 a r r a y (11 new Sys l ogLogge r ( ) ,12 new F i l eSy s t emLogge r ( ’ l o g / e r r o r s . l o g ’ )13 )14 )15 )16 )17 ) ,18 $ l o g g e r19 ) ;

Advanced OO Patterns 12 / 40

Injecting dependencies

1 <?php23 $messenger = new MessageDi spatche r (4 a r r a y (5 new JabberMessenger ( ’ j a b b e r . example . org ’ , ’ u s e r ’ , ’ pa s s ’ ) ,6 new Mai lMessenger (7 new Mai lSmtpTransport ( ’ ma i l . example . org ’ , ’ u s e r ’ , ’ pa s s ’ ) ,8 $ l o g g e r = new Logger (9 new Logg i ngD i spa t che r (

10 a r r a y (11 new Sys l ogLogge r ( ) ,12 new F i l eSy s t emLogge r ( ’ l o g / e r r o r s . l o g ’ )13 )14 )15 )16 )17 ) ,18 $ l o g g e r19 ) ;

Advanced OO Patterns 12 / 40

Injecting dependencies

1 <?php23 $messenger = new MessageDi spatche r (4 a r r a y (5 new JabberMessenger ( ’ j a b b e r . example . org ’ , ’ u s e r ’ , ’ pa s s ’ ) ,6 new Mai lMessenger (7 new Mai lSmtpTransport ( ’ ma i l . example . org ’ , ’ u s e r ’ , ’ pa s s ’ ) ,8 $ l o g g e r = new Logger (9 new Logg i ngD i spa t che r (

10 a r r a y (11 new Sys l ogLogge r ( ) ,12 new F i l eSy s t emLogge r ( ’ l o g / e r r o r s . l o g ’ )13 )14 )15 )16 )17 ) ,18 $ l o g g e r19 ) ;

Advanced OO Patterns 12 / 40

Injecting dependencies

1 <?php23 $messenger = new MessageDi spatche r (4 a r r a y (5 new JabberMessenger ( ’ j a b b e r . example . org ’ , ’ u s e r ’ , ’ pa s s ’ ) ,6 new Mai lMessenger (7 new Mai lSmtpTransport ( ’ ma i l . example . org ’ , ’ u s e r ’ , ’ pa s s ’ ) ,8 $ l o g g e r = new Logger (9 new Logg i ngD i spa t che r (

10 a r r a y (11 new Sys l ogLogge r ( ) ,12 new F i l eSy s t emLogge r ( ’ l o g / e r r o r s . l o g ’ )13 )14 )15 )16 )17 ) ,18 $ l o g g e r19 ) ;

Advanced OO Patterns 12 / 40

Injecting dependencies

1 <?php23 $messenger = new MessageDi spatche r (4 a r r a y (5 new JabberMessenger ( ’ j a b b e r . example . org ’ , ’ u s e r ’ , ’ pa s s ’ ) ,6 new Mai lMessenger (7 new Mai lSmtpTransport ( ’ ma i l . example . org ’ , ’ u s e r ’ , ’ pa s s ’ ) ,8 $ l o g g e r = new Logger (9 new Logg i ngD i spa t che r (

10 a r r a y (11 new Sys l ogLogge r ( ) ,12 new F i l eSy s t emLogge r ( ’ l o g / e r r o r s . l o g ’ )13 )14 )15 )16 )17 ) ,18 $ l o g g e r19 ) ;

Advanced OO Patterns 12 / 40

Injecting dependencies

1 <?php23 $messenger = new MessageDi spatche r (4 a r r a y (5 new JabberMessenger ( ’ j a b b e r . example . org ’ , ’ u s e r ’ , ’ pa s s ’ ) ,6 new Mai lMessenger (7 new Mai lSmtpTransport ( ’ ma i l . example . org ’ , ’ u s e r ’ , ’ pa s s ’ ) ,8 $ l o g g e r = new Logger (9 new Logg i ngD i spa t che r (

10 a r r a y (11 new Sys l ogLogge r ( ) ,12 new F i l eSy s t emLogge r ( ’ l o g / e r r o r s . l o g ’ )13 )14 )15 )16 )17 ) ,18 $ l o g g e r19 ) ;

Advanced OO Patterns 13 / 40

Dependency injection

I ProsI FlexibilityI ReusabilityI ModularityI Testability

I ConsI Complex object graphsI Long parameter listsI Object ballast

Advanced OO Patterns 14 / 40

Dependency injection container

1 <?php23 c l a s s Dep end en c y I n j e c t i o nCon t a i n e r4 {5 p r o t e c t e d $mes sageD i spa tche r ;6 p r o t e c t e d $ l o g g e r ;78 p u b l i c f u n c t i o n c o n s t r u c t (9 MessageDi spa tche r $messageDi spatche r ,

10 Logger $ l o g g e r11 )12 {13 $ t h i s−>messageD i spa t che r ;14 $ t h i s−>l o g g e r ;15 }1617 p u b l i c f u n c t i o n ge tMes sageD i spa t che r ( )18 {19 r e t u r n $ t h i s−>messageD i spa t che r ;20 }2122 p u b l i c f u n c t i o n ge tLogge r ( )23 {24 r e t u r n $ t h i s−>l o g g e r ;25 }26 }

Advanced OO Patterns 15 / 40

Outline

Introduction

Dependency injection

Lazy initialization

Data storage

Advanced OO Patterns 16 / 40

Motivation

I Building the full object graph isI ComplexI Time consumingI Memory consuming

I Do you use all objects in every request?

I Idea: Initialize object when needed for the first time

Advanced OO Patterns 16 / 40

Motivation

I Building the full object graph isI ComplexI Time consumingI Memory consuming

I Do you use all objects in every request?

I Idea: Initialize object when needed for the first time

Advanced OO Patterns 16 / 40

Motivation

I Building the full object graph isI ComplexI Time consumingI Memory consuming

I Do you use all objects in every request?

I Idea: Initialize object when needed for the first time

Advanced OO Patterns 17 / 40

Simple lazy initialization

1 <?php23 c l a s s D a t a b a s e I n i t i a l i z e r4 {5 p r o t e c t e d $dsn ;67 p r o t e c t e d $db ;89 p u b l i c f u n c t i o n c o n s t r u c t ( $dsn )

10 {11 $ t h i s−>dsn = $dsn ;12 }1314 p u b l i c f u n c t i o n getDatabase ( )15 {16 i f ( $ t h i s−>db === n u l l )17 {18 $ t h i s−>db = new Database ( $ t h i s−>dsn ) ;19 }20 r e t u r n $ t h i s−>db ;21 }22 }

Advanced OO Patterns 17 / 40

Simple lazy initialization

1 <?php23 c l a s s D a t a b a s e I n i t i a l i z e r4 {5 p r o t e c t e d $dsn ;67 p r o t e c t e d $db ;89 p u b l i c f u n c t i o n c o n s t r u c t ( $dsn )

10 {11 $ t h i s−>dsn = $dsn ;12 }1314 p u b l i c f u n c t i o n getDatabase ( )15 {16 i f ( $ t h i s−>db === n u l l )17 {18 $ t h i s−>db = new Database ( $ t h i s−>dsn ) ;19 }20 r e t u r n $ t h i s−>db ;21 }22 }

Advanced OO Patterns 17 / 40

Simple lazy initialization

1 <?php23 c l a s s D a t a b a s e I n i t i a l i z e r4 {5 p r o t e c t e d $dsn ;67 p r o t e c t e d $db ;89 p u b l i c f u n c t i o n c o n s t r u c t ( $dsn )

10 {11 $ t h i s−>dsn = $dsn ;12 }1314 p u b l i c f u n c t i o n getDatabase ( )15 {16 i f ( $ t h i s−>db === n u l l )17 {18 $ t h i s−>db = new Database ( $ t h i s−>dsn ) ;19 }20 r e t u r n $ t h i s−>db ;21 }22 }

Advanced OO Patterns 18 / 40

Combining DIC and lazy initialization

I Inject everything

I Initialize only when needed

I Resolve dependencies automatically

Advanced OO Patterns 19 / 40

The Arbit DIC

I Exemplary for a high-end DICI Shared objects (initialized once)I Closures for lazy initializationI Inherent dependency resolution

I Base class for custom DICs

I Heavy use of interceptors

I No implementation details,see here: http://bit.ly/arbitDIC

Advanced OO Patterns 19 / 40

The Arbit DIC

I Exemplary for a high-end DICI Shared objects (initialized once)I Closures for lazy initializationI Inherent dependency resolution

I Base class for custom DICs

I Heavy use of interceptors

I No implementation details,see here: http://bit.ly/arbitDIC

Advanced OO Patterns 19 / 40

The Arbit DIC

I Exemplary for a high-end DICI Shared objects (initialized once)I Closures for lazy initializationI Inherent dependency resolution

I Base class for custom DICs

I Heavy use of interceptors

I No implementation details,see here: http://bit.ly/arbitDIC

Advanced OO Patterns 19 / 40

The Arbit DIC

I Exemplary for a high-end DICI Shared objects (initialized once)I Closures for lazy initializationI Inherent dependency resolution

I Base class for custom DICs

I Heavy use of interceptors

I No implementation details,see here: http://bit.ly/arbitDIC

Advanced OO Patterns 20 / 40

Using the Arbit DIC

3 c l a s s a rb i tEnv i ronmentDIC ex t end s a r b i tD e p e n d e n c y I n j e c t i o nCon t a i n e r4 {

13 p u b l i c f u n c t i o n i n i t i a l i z e ( )14 {

38 }39 }

Advanced OO Patterns 21 / 40

Using the Arbit DIC

3 c l a s s a rb i tEnv i ronmentDIC ex t end s a r b i tD e p e n d e n c y I n j e c t i o nCon t a i n e r4 {

13 p u b l i c f u n c t i o n i n i t i a l i z e ( )14 {

15 $ t h i s−>cache = f u n c t i o n ( $d i c )16 {17 r e t u r n new a r b i t F i l e s y s t emCa c h e ( $d ic−>r eque s t−>c o n t r o l l e r ) ;18 } ;

38 }39 }

Advanced OO Patterns 22 / 40

Using the Arbit DIC

3 c l a s s a rb i tEnv i ronmentDIC ex t end s a r b i tD e p e n d e n c y I n j e c t i o nCon t a i n e r4 {

13 p u b l i c f u n c t i o n i n i t i a l i z e ( )14 {

20 $ t h i s−>messenger = f u n c t i o n ( $d i c )21 {22 r e t u r n new a rb i tMe s s eng e r ( a r r a y (23 ’ ema i l ’ => $d ic−>mai lMessenger ,24 ) ) ;25 } ;

38 }39 }

Advanced OO Patterns 23 / 40

Using the Arbit DIC

3 c l a s s a rb i tEnv i ronmentDIC ex t end s a r b i tD e p e n d e n c y I n j e c t i o nCon t a i n e r4 {

13 p u b l i c f u n c t i o n i n i t i a l i z e ( )14 {

27 $ t h i s−>mai lMessenge r = f u n c t i o n ( $d i c )28 {29 r e t u r n new a rb i tMa i lMe s s e ng e r (30 $d ic−>mai lT ranspo r t ,31 $d ic−>c o n f i g u r a t i o n−>main ,32 $d ic−>c o n f i g u r a t i o n−>p r o j e c t ( $d ic−>r eque s t−>c o n t r o l l e r ) ,33 $d ic−>v iews−>d e c o r a t o r34 ) ;35 } ;

38 }39 }

Advanced OO Patterns 24 / 40

More about DIC

I Pimple - A small PHP 5.3 dependency injection containerhttps://github.com/fabpot/Pimple

I Bucket - Basic di-container for phphttps://github.com/troelskn/bucket

I Symfony Dependency Injection http://components.

symfony-project.org/dependency-injection/

I Martin Fowler on Dependency Injectionhttp://martinfowler.com/articles/injection.html

Advanced OO Patterns 25 / 40

Outline

Introduction

Dependency injection

Lazy initialization

Data storage

Advanced OO Patterns 26 / 40

The situation

Model (application) Storage

Advanced OO Patterns 26 / 40

The situation

Model (application) Storage

store

restore

Advanced OO Patterns 27 / 40

Challenges

I Model and storage structure differI Object relational impedance mismatch

I Different access approachesI Query language differences

I Storage back end might change

I Back ends could even be mixed

I . . .

Advanced OO Patterns 27 / 40

Challenges

I Model and storage structure differI Object relational impedance mismatch

I Different access approachesI Query language differences

I Storage back end might change

I Back ends could even be mixed

I . . .

Advanced OO Patterns 27 / 40

Challenges

I Model and storage structure differI Object relational impedance mismatch

I Different access approachesI Query language differences

I Storage back end might change

I Back ends could even be mixed

I . . .

Advanced OO Patterns 27 / 40

Challenges

I Model and storage structure differI Object relational impedance mismatch

I Different access approachesI Query language differences

I Storage back end might change

I Back ends could even be mixed

I . . .

Advanced OO Patterns 27 / 40

Challenges

I Model and storage structure differI Object relational impedance mismatch

I Different access approachesI Query language differences

I Storage back end might change

I Back ends could even be mixed

I . . .

Advanced OO Patterns 27 / 40

Challenges

I Model and storage structure differI Object relational impedance mismatch

I Different access approachesI Query language differences

I Storage back end might change

I Back ends could even be mixed

I . . .

Advanced OO Patterns 27 / 40

Challenges

I Model and storage structure differI Object relational impedance mismatch

I Different access approachesI Query language differences

I Storage back end might change

I Back ends could even be mixed

I . . .

Advanced OO Patterns 28 / 40

Active Record

1 <?php23 c l a s s I n v o i c e e x t end s Ac t i v eReco rd4 {5 p r o t e c t e d $ i d ;6 p r o t e c t e d $ p o s i t i o n s ;7 p r o t e c t e d $vat ;8 // . . .9

10 p u b l i c f u n c t i o n c a l c u l a t eV a l u e ( )11 { /∗ b u s i n e s s l o g i c ∗/ }12 }1314 c l a s s Ac t i v eReco rd15 {16 p u b l i c f u n c t i o n i n s e r t ( )17 { /∗ . . . ∗/ }18 p u b l i c f u n c t i o n update ( )19 { /∗ . . . ∗/ }20 }

Advanced OO Patterns 28 / 40

Active Record

1 <?php23 c l a s s I n v o i c e e x t end s Ac t i v eReco rd4 {5 p r o t e c t e d $ i d ;6 p r o t e c t e d $ p o s i t i o n s ;7 p r o t e c t e d $vat ;8 // . . .9

10 p u b l i c f u n c t i o n c a l c u l a t eV a l u e ( )11 { /∗ b u s i n e s s l o g i c ∗/ }12 }1314 c l a s s Ac t i v eReco rd15 {16 p u b l i c f u n c t i o n i n s e r t ( )17 { /∗ . . . ∗/ }18 p u b l i c f u n c t i o n update ( )19 { /∗ . . . ∗/ }20 }

Advanced OO Patterns 28 / 40

Active Record

1 <?php23 c l a s s I n v o i c e e x t end s Ac t i v eReco rd4 {5 p r o t e c t e d $ i d ;6 p r o t e c t e d $ p o s i t i o n s ;7 p r o t e c t e d $vat ;8 // . . .9

10 p u b l i c f u n c t i o n c a l c u l a t eV a l u e ( )11 { /∗ b u s i n e s s l o g i c ∗/ }12 }1314 c l a s s Ac t i v eReco rd15 {16 p u b l i c f u n c t i o n i n s e r t ( )17 { /∗ . . . ∗/ }18 p u b l i c f u n c t i o n update ( )19 { /∗ . . . ∗/ }20 }

Advanced OO Patterns 28 / 40

Active Record

1 <?php23 c l a s s I n v o i c e e x t end s Ac t i v eReco rd4 {5 p r o t e c t e d $ i d ;6 p r o t e c t e d $ p o s i t i o n s ;7 p r o t e c t e d $vat ;8 // . . .9

10 p u b l i c f u n c t i o n c a l c u l a t eV a l u e ( )11 { /∗ b u s i n e s s l o g i c ∗/ }12 }1314 c l a s s Ac t i v eReco rd15 {16 p u b l i c f u n c t i o n i n s e r t ( )17 { /∗ . . . ∗/ }18 p u b l i c f u n c t i o n update ( )19 { /∗ . . . ∗/ }20 }

Advanced OO Patterns 29 / 40

Active Record

I Combines storage and business logic

I Commonly storage logic in base class

I Model class corresponds to database record

Advanced OO Patterns 30 / 40

Evaluation

Pros

I Very easy to use

I Very few code towrite

Cons

I Model structure = storagestructure

I Classes become complexI Business logicI Storage logic

I Broken object semantics

I Changes ripple over

I Hard to exchange storage logic

I Really hard to test!

Advanced OO Patterns 30 / 40

Evaluation

Pros

I Very easy to use

I Very few code towrite

Cons

I Model structure = storagestructure

I Classes become complexI Business logicI Storage logic

I Broken object semantics

I Changes ripple over

I Hard to exchange storage logic

I Really hard to test!

Advanced OO Patterns 30 / 40

Evaluation

Pros

I Very easy to use

I Very few code towrite

Cons

I Model structure = storagestructure

I Classes become complexI Business logicI Storage logic

I Broken object semantics

I Changes ripple over

I Hard to exchange storage logic

I Really hard to test!

Advanced OO Patterns 30 / 40

Evaluation

Pros

I Very easy to use

I Very few code towrite

Cons

I Model structure = storagestructure

I Classes become complexI Business logicI Storage logic

I Broken object semantics

I Changes ripple over

I Hard to exchange storage logic

I Really hard to test!

Advanced OO Patterns 30 / 40

Evaluation

Pros

I Very easy to use

I Very few code towrite

Cons

I Model structure = storagestructure

I Classes become complexI Business logicI Storage logic

I Broken object semantics

I Changes ripple over

I Hard to exchange storage logic

I Really hard to test!

Advanced OO Patterns 30 / 40

Evaluation

Pros

I Very easy to use

I Very few code towrite

Cons

I Model structure = storagestructure

I Classes become complexI Business logicI Storage logic

I Broken object semantics

I Changes ripple over

I Hard to exchange storage logic

I Really hard to test!

Advanced OO Patterns 30 / 40

Evaluation

Pros

I Very easy to use

I Very few code towrite

Cons

I Model structure = storagestructure

I Classes become complexI Business logicI Storage logic

I Broken object semantics

I Changes ripple over

I Hard to exchange storage logic

I Really hard to test!

Advanced OO Patterns 30 / 40

Evaluation

Pros

I Very easy to use

I Very few code towrite

Cons

I Model structure = storagestructure

I Classes become complexI Business logicI Storage logic

I Broken object semantics

I Changes ripple over

I Hard to exchange storage logic

I Really hard to test!

Advanced OO Patterns 31 / 40

Lesson learned . . .

De-couple business and storage logic!

Advanced OO Patterns 32 / 40

Row Data Gateway

1 <?php23 c l a s s I n v o i c e4 {5 p r o t e c t e d $data ;6 // . . .78 p u b l i c f u n c t i o n c o n s t r u c t ( I nvo i c eGateway $data )9 { $ t h i s−>data = $data ; }

1011 p u b l i c f u n c t i o n c a l c u l a t eV a l u e ( )12 { /∗ b u s i n e s s l o g i c ∗/ }13 }1415 c l a s s I nvo i c eGateway16 {17 p r o t e c t e d $ i d ;18 p r o t e c t e d $ p o s i t i o n s ;19 p r o t e c t e d $vat ;2021 p u b l i c f u n c t i o n i n s e r t ( )22 { /∗ . . . ∗/ }23 p u b l i c f u n c t i o n update ( )24 { /∗ . . . ∗/ }25 }

Advanced OO Patterns 32 / 40

Row Data Gateway

1 <?php23 c l a s s I n v o i c e4 {5 p r o t e c t e d $data ;6 // . . .78 p u b l i c f u n c t i o n c o n s t r u c t ( I nvo i c eGateway $data )9 { $ t h i s−>data = $data ; }

1011 p u b l i c f u n c t i o n c a l c u l a t eV a l u e ( )12 { /∗ b u s i n e s s l o g i c ∗/ }13 }1415 c l a s s I nvo i c eGateway16 {17 p r o t e c t e d $ i d ;18 p r o t e c t e d $ p o s i t i o n s ;19 p r o t e c t e d $vat ;2021 p u b l i c f u n c t i o n i n s e r t ( )22 { /∗ . . . ∗/ }23 p u b l i c f u n c t i o n update ( )24 { /∗ . . . ∗/ }25 }

Advanced OO Patterns 32 / 40

Row Data Gateway

1 <?php23 c l a s s I n v o i c e4 {5 p r o t e c t e d $data ;6 // . . .78 p u b l i c f u n c t i o n c o n s t r u c t ( I nvo i c eGateway $data )9 { $ t h i s−>data = $data ; }

1011 p u b l i c f u n c t i o n c a l c u l a t eV a l u e ( )12 { /∗ b u s i n e s s l o g i c ∗/ }13 }1415 c l a s s I nvo i c eGateway16 {17 p r o t e c t e d $ i d ;18 p r o t e c t e d $ p o s i t i o n s ;19 p r o t e c t e d $vat ;2021 p u b l i c f u n c t i o n i n s e r t ( )22 { /∗ . . . ∗/ }23 p u b l i c f u n c t i o n update ( )24 { /∗ . . . ∗/ }25 }

Advanced OO Patterns 32 / 40

Row Data Gateway

1 <?php23 c l a s s I n v o i c e4 {5 p r o t e c t e d $data ;6 // . . .78 p u b l i c f u n c t i o n c o n s t r u c t ( I nvo i c eGateway $data )9 { $ t h i s−>data = $data ; }

1011 p u b l i c f u n c t i o n c a l c u l a t eV a l u e ( )12 { /∗ b u s i n e s s l o g i c ∗/ }13 }1415 c l a s s I nvo i c eGateway16 {17 p r o t e c t e d $ i d ;18 p r o t e c t e d $ p o s i t i o n s ;19 p r o t e c t e d $vat ;2021 p u b l i c f u n c t i o n i n s e r t ( )22 { /∗ . . . ∗/ }23 p u b l i c f u n c t i o n update ( )24 { /∗ . . . ∗/ }25 }

Advanced OO Patterns 33 / 40

Row Data Gateway

I Decouples storage from business logic

I Still convenient OO interface for storage

I Still some objects coupled to storage

I (Table Data Gateway is a somewhat similar approach)

Advanced OO Patterns 33 / 40

Row Data Gateway

I Decouples storage from business logic

I Still convenient OO interface for storage

I Still some objects coupled to storage

I (Table Data Gateway is a somewhat similar approach)

Advanced OO Patterns 34 / 40

Evaluation

Pros

I Easy to useI Few code to write

I Gateways can begenerated

I Easier to test

Cons

I Some objects model DBstructure

I Changes still ripple over

I Still hard to exchangestorage logic

Advanced OO Patterns 34 / 40

Evaluation

Pros

I Easy to useI Few code to write

I Gateways can begenerated

I Easier to test

Cons

I Some objects model DBstructure

I Changes still ripple over

I Still hard to exchangestorage logic

Advanced OO Patterns 34 / 40

Evaluation

Pros

I Easy to useI Few code to write

I Gateways can begenerated

I Easier to test

Cons

I Some objects model DBstructure

I Changes still ripple over

I Still hard to exchangestorage logic

Advanced OO Patterns 34 / 40

Evaluation

Pros

I Easy to useI Few code to write

I Gateways can begenerated

I Easier to test

Cons

I Some objects model DBstructure

I Changes still ripple over

I Still hard to exchangestorage logic

Advanced OO Patterns 34 / 40

Evaluation

Pros

I Easy to useI Few code to write

I Gateways can begenerated

I Easier to test

Cons

I Some objects model DBstructure

I Changes still ripple over

I Still hard to exchangestorage logic

Advanced OO Patterns 35 / 40

Data Mapper

1 <?php23 c l a s s I n v o i c e4 {5 p r o t e c t e d $ i d ;6 p r o t e c t e d $ p o s i t i o n s ;7 p r o t e c t e d $vat ;8 // . . .9

10 p u b l i c f u n c t i o n c a l c u l a t eV a l u e ( )11 { /∗ b u s i n e s s l o g i c ∗/ }12 }1314 i n t e r f a c e Invo i ceMappe r15 {16 p u b l i c f u n c t i o n s t o r e ( I n v o i c e $ i n v o i c e ) ;17 p u b l i c f u n c t i o n update ( I n v o i c e $ i n v o i c e ) ;18 }1920 c l a s s DbInvoiceMapper implements Invo i ceMappe r21 {22 // . . .23 }2425 ?>

Advanced OO Patterns 35 / 40

Data Mapper

1 <?php23 c l a s s I n v o i c e4 {5 p r o t e c t e d $ i d ;6 p r o t e c t e d $ p o s i t i o n s ;7 p r o t e c t e d $vat ;8 // . . .9

10 p u b l i c f u n c t i o n c a l c u l a t eV a l u e ( )11 { /∗ b u s i n e s s l o g i c ∗/ }12 }1314 i n t e r f a c e Invo i ceMappe r15 {16 p u b l i c f u n c t i o n s t o r e ( I n v o i c e $ i n v o i c e ) ;17 p u b l i c f u n c t i o n update ( I n v o i c e $ i n v o i c e ) ;18 }1920 c l a s s DbInvoiceMapper implements Invo i ceMappe r21 {22 // . . .23 }2425 ?>

Advanced OO Patterns 35 / 40

Data Mapper

1 <?php23 c l a s s I n v o i c e4 {5 p r o t e c t e d $ i d ;6 p r o t e c t e d $ p o s i t i o n s ;7 p r o t e c t e d $vat ;8 // . . .9

10 p u b l i c f u n c t i o n c a l c u l a t eV a l u e ( )11 { /∗ b u s i n e s s l o g i c ∗/ }12 }1314 i n t e r f a c e Invo i ceMappe r15 {16 p u b l i c f u n c t i o n s t o r e ( I n v o i c e $ i n v o i c e ) ;17 p u b l i c f u n c t i o n update ( I n v o i c e $ i n v o i c e ) ;18 }1920 c l a s s DbInvoiceMapper implements Invo i ceMappe r21 {22 // . . .23 }2425 ?>

Advanced OO Patterns 35 / 40

Data Mapper

1 <?php23 c l a s s I n v o i c e4 {5 p r o t e c t e d $ i d ;6 p r o t e c t e d $ p o s i t i o n s ;7 p r o t e c t e d $vat ;8 // . . .9

10 p u b l i c f u n c t i o n c a l c u l a t eV a l u e ( )11 { /∗ b u s i n e s s l o g i c ∗/ }12 }1314 i n t e r f a c e Invo i ceMappe r15 {16 p u b l i c f u n c t i o n s t o r e ( I n v o i c e $ i n v o i c e ) ;17 p u b l i c f u n c t i o n update ( I n v o i c e $ i n v o i c e ) ;18 }1920 c l a s s DbInvoiceMapper implements Invo i ceMappe r21 {22 // . . .23 }2425 ?>

Advanced OO Patterns 36 / 40

Data Mapper

I Decouple storage from model

I No OO modelling of DB structure

I Model does not even know a database exists

Advanced OO Patterns 37 / 40

Evaluation

Pros

I Complete decoupling

I Model is not aware of storageI Clean storage interface

I Implement different storages!

I DB changes only affectmapping layer

I Model changes only affectmapping layer

I Nice for testing

Cons

I Quite some codeto write

I Mapping canbecome complex

Advanced OO Patterns 37 / 40

Evaluation

Pros

I Complete decoupling

I Model is not aware of storageI Clean storage interface

I Implement different storages!

I DB changes only affectmapping layer

I Model changes only affectmapping layer

I Nice for testing

Cons

I Quite some codeto write

I Mapping canbecome complex

Advanced OO Patterns 37 / 40

Evaluation

Pros

I Complete decoupling

I Model is not aware of storageI Clean storage interface

I Implement different storages!

I DB changes only affectmapping layer

I Model changes only affectmapping layer

I Nice for testing

Cons

I Quite some codeto write

I Mapping canbecome complex

Advanced OO Patterns 37 / 40

Evaluation

Pros

I Complete decoupling

I Model is not aware of storageI Clean storage interface

I Implement different storages!

I DB changes only affectmapping layer

I Model changes only affectmapping layer

I Nice for testing

Cons

I Quite some codeto write

I Mapping canbecome complex

Advanced OO Patterns 37 / 40

Evaluation

Pros

I Complete decoupling

I Model is not aware of storageI Clean storage interface

I Implement different storages!

I DB changes only affectmapping layer

I Model changes only affectmapping layer

I Nice for testing

Cons

I Quite some codeto write

I Mapping canbecome complex

Advanced OO Patterns 37 / 40

Evaluation

Pros

I Complete decoupling

I Model is not aware of storageI Clean storage interface

I Implement different storages!

I DB changes only affectmapping layer

I Model changes only affectmapping layer

I Nice for testing

Cons

I Quite some codeto write

I Mapping canbecome complex

Advanced OO Patterns 37 / 40

Evaluation

Pros

I Complete decoupling

I Model is not aware of storageI Clean storage interface

I Implement different storages!

I DB changes only affectmapping layer

I Model changes only affectmapping layer

I Nice for testing

Cons

I Quite some codeto write

I Mapping canbecome complex

Advanced OO Patterns 37 / 40

Evaluation

Pros

I Complete decoupling

I Model is not aware of storageI Clean storage interface

I Implement different storages!

I DB changes only affectmapping layer

I Model changes only affectmapping layer

I Nice for testing

Cons

I Quite some codeto write

I Mapping canbecome complex

Advanced OO Patterns 38 / 40

Attribution

The ideas behind the storage patterns are fromPatterns of Enterprise Application Architecture

by Martin Fowler

Highy recommended!

Advanced OO Patterns 39 / 40

Conclusion

I Patterns are not the holy grail!

I They assign names to good ideas

I They help you to talk about concepts

I They can inspire you

Advanced OO Patterns 39 / 40

Conclusion

I Patterns are not the holy grail!

I They assign names to good ideas

I They help you to talk about concepts

I They can inspire you

Advanced OO Patterns 40 / 40

Thanks for listening

Are there any questions left?

Advanced OO Patterns 40 / 40

Thanks for listening

Please rate this talk athttp://joind.in/2512

and / or give me some feedback right now!

Advanced OO Patterns 40 / 40

Thanks for listening

Please rate this talk athttp://joind.in/2512

and / or give me some feedback right now!

Stay in touch

I Tobias Schlitt

I [email protected]

I @tobySen / @qafoo

Rent a PHP quality expert:http://qafoo.com