Published on June 18, 2007
Objects of Desire A Few Questions (and Answers) on Modern Object-Oriented Development Kevlin Henney firstname.lastname@example.org
Agenda • Intent Some answers concerning OO and related topics • Content A brief history of objects Linguistics Encapsulation Class hierarchies Patterns Testing PHP Conference 2007 2
A Brief History of Objects History rarely happens in the right order or at the right time, but the job of aa History rarely happens in the right order or at the right time, but the job of historian is to make it appear as if it did. historian is to make it appear as if it did. James Burke James Burke PHP Conference 2007 3
Back to the Future • How old is OO? 40 years old this year (Simula 67, Norway, 1967) • When did OO become mainstream? Around the mid 1990s (1997 is often quoted) • Where is OO used? Everywhere... and then some • What about the future? Objects will remain mainstream, but they are not the only game in town PHP Conference 2007 4
Linguistics Language is not simply aareporting device for experience but aadefining Language is not simply reporting device for experience but defining framework for it. framework for it. Benjamin Whorf Benjamin Whorf PHP Conference 2007 5
General Linguistics • Is there a relationship between language and OO style? In spite of rumours to the contrary, there is a strong relationship between language features and appropriate OO style • What can we learn from other languages? A lot! • Which is the best OO language? It depends PHP Conference 2007 6
Type Systems • How do static and dynamic type systems compare? They each both strengths and weaknesses • How does PHP5's type system measure up? It is inherently dynamic, so some of the features borrowed from static type systems are surprising • What about type hints? These appear to combine all the problems of static typing with all the weaknesses of dynamic typing PHP Conference 2007 7
Encapsulation encapsulate enclose (something) in or as if in aacapsule. encapsulate enclose (something) in or as if in capsule. express the essential feature of (someone or something) succinctly. express the essential feature of (someone or something) succinctly. enclose (a message or signal) in aaset of codes which allow use by or enclose (a message or signal) in set of codes which allow use by or transfer through different computer systems or networks. transfer through different computer systems or networks. provide an interface for (a piece of software or hardware) to allow or provide an interface for (a piece of software or hardware) to allow or simplify access for the user. simplify access for the user. The New Oxford Dictionary of English The New Oxford Dictionary of English PHP Conference 2007 8
In a Capsule • How do you use public, protected and private? Declare data private, steer clear of protected, and only make methods public if you know why and where you're going to use them and how to test them • So, encapsulation is about data privacy? No, it relates to stability, coherence and ease of use of an object — data privacy is a by-product • How big should a class be? Big enough, but not too big PHP Conference 2007 9
Train Wrecks and Vampires • Is chaining calls on function results useful? PHP4's model was, quite frankly, surprising • What's the Law of Demeter? Don't talk to strangers • What's a train wreck? A violation of the Law of Demeter • But what about collections? Return iterators and offer focused methods, rather than uncontrolled access to private collections PHP Conference 2007 10
Class Hierarchies The history of all hitherto existing society is the history of class struggles. The history of all hitherto existing society is the history of class struggles. Karl Marx and Friedrich Engels Karl Marx and Friedrich Engels PHP Conference 2007 11
Substitutability • What is LSP? The Liskov Substitution Principle • What is the Liskov Substitution Principle? Organise class hierarchies according to type substitutability, not according to commonality of implementation • What about reuse of implementation? Favour delegation, steer clear of ad hoc subclassing — spaghetti inheritance is hard to digest PHP Conference 2007 12
Interfaces • What is the common role of interfaces? In statically typed languages, the interface construct, or equivalent, promotes polymorphism and reduces dependencies — inheritance of implementation is the strongest form of coupling in an OO system In dynamically typed languages, the notion of usage type is normally left as an informal protocol or is expressed as predicate or constraint on objects • What is the role of interfaces in PHP5? Good question PHP Conference 2007 13
Patterns The enormous success of design patterns is aatestimonial to the commonality The enormous success of design patterns is testimonial to the commonality seen by object programmers. The success of the book Design Patterns, however, seen by object programmers. The success of the book Design Patterns, however, has stifled any diversity in expressing these patterns. has stifled any diversity in expressing these patterns. Kent Beck Kent Beck PHP Conference 2007 14
Shameless Plug PHP Conference 2007 15
Pitter Pattern Brain Drop • What is a pattern? A good pattern captures a proven solution practice along with the problem it addresses and the context in which it applies • Are patterns only about OO design? No • How do you apply patterns? Unconsciously and tacitly or consciously and explicitly — but watch out for patternitis PHP Conference 2007 16
The Good, the Bad and the Ugly • What are the top five patterns to know? It depends • If dependencies are so bad, why is Decorator recommended? They are, and it isn't: it was documented as an OO pattern nearly 15 years ago, but Interceptor is now considered better for much of what Decorator does • Do patterns make code more complicated? Only if used to make code more complicated PHP Conference 2007 17
The Epicycle Problem • What is the trouble with Singleton? It is normally used by coincidence, it introduces a centralised point of coupling, it complicates testing and it comes with various lifecycle problems • What are the alternatives? Focus on making essential object relationships explicit, e.g. using patterns like Context Object • What about the Monostate pattern? This is also known as the Borg pattern, which tells you everything you need to know PHP Conference 2007 18
Testing Science is what we have learned about how to keep from fooling ourselves. Science is what we have learned about how to keep from fooling ourselves. Richard Feynman Richard Feynman PHP Conference 2007 19
Motivation for Testing PHP Conference 2007 20
More Motivation PHP Conference 2007 21
And More PHP Conference 2007 22
Testing, Testing, One, Two, One, Two • How soon should testing be introduced? Either as soon as you know what to test — why wait? — or as soon as you don't know what to test — use testing to help drive detailed design and clarification of requirements • What is the relationship between testing and design? A test is a proposition about how something is used, so it can be used as a form of specification and clarification of needs and knowledge PHP Conference 2007 23
In Closing... The only thing to do with good advice is to pass it on. It is never any use to oneself. The only thing to do with good advice is to pass it on. It is never any use to oneself. Oscar Wilde Oscar Wilde PHP Conference 2007 24
Kevlin Henney added, ... links for "It Is Possible to Do Object-Oriented Programming in Java" http://www. slideshare.net/Kevlin/it-is-p ossible-to-do ...
Kevlin Henney. Kevlin Henney (mailto:email@example.com, ... architecture, distributed object systems, and languages, including ...
View Kevlin Henney’s professional profile on LinkedIn. LinkedIn is the world's largest business network, helping professionals like Kevlin Henney ...
Objects of Value Kevlin Henney firstname.lastname@example.org ... use new objects with this pattern that represent the meaningful quantities of your business.
... Kevlin Henney (Curbralan) - Codemotion Rome 2015 ... although not apparently as mainstream as object-oriented and procedural programming, ...
Code: Kevlin Henney . Sign up or log in to save this event to your list and see who's attending! Tweet Share. Code is the ... manifestos of desire.
Kevlin Henney is an author who writes on the subject of ... presenting Patterns for Concurrent and Networked Objects. In 2003, Henney presented at ...
Kevlin Henney: Oct 22, 2009 11:04 AM: Posted in group: software_craftsmanship: ... > kinds of objects, Data objects and Behavior objects. Behavior objects