Interface Oriented Design.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
Interface-Oriented Design Ken Pugh The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Pragmatic Bookshelf Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragmaticprogrammer.com Copyright © 2006 The Pragmatic Programmers LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN 0-9766940-5-0 Printed on acid-free paper with 85% recycled, 30% post-consumer content. First printing, June 2006 Version: 2006-7-23 Dedicated to Nameless II, the cat who sat on my lap while I typed this book. Contents Preface ix Road Map .............................. x Who Should Read This Book ................... xi About the Cover ........................... xii So, What Else Is in Here? ..................... xii Acknowledgments .......................... xiii I All about Interfaces 1 1 Introduction to Interfaces 2 1.1 Pizza-Ordering Interface ................... 2 1.2 Real-Life Interfaces ...................... 5 1.3 Things to Remember ..................... 11 2 Interface Contracts 12 2.1 The Three Laws of Interfaces ................ 12 2.2 Design by Contract ...................... 17 2.3 Testing Interfaces against Contracts ........... 23 2.4 Levels of Contracts ...................... 27 2.5 Contractual Quality ..................... 29 2.6 Things to Remember ..................... 30 3 Interface Ingredients 32 3.1 Data Interfaces and Service Interfaces .......... 32 3.2 Data Access Interface Structures ............. 35 3.3 Alternative Interfaces .................... 41 3.4 Stateless versus Stateful Interfaces ............ 44 3.5 Transformation Considerations .............. 47 3.6 Multiple Interfaces ...................... 51 3.7 Things to Remember ..................... 52 CONTENTS vi 4 What Should Be in an Interface? 53 4.1 Cohesiveness ......................... 53 4.2 A Printer Interface ...................... 54 4.3 Coupling ............................ 58 4.4 Interface Measures ...................... 60 4.5 Things to Remember ..................... 63 5 Inheritance and Interfaces 64 5.1 Inheritance and Interfaces ................. 64 5.2 Polymorphism ........................ 65 5.3 Hierarchies .......................... 68 5.4 An Interface Alternative for InputStream ......... 76 5.5 Things to Remember ..................... 82 6 Remote Interfaces 83 6.1 Introduction .......................... 83 6.2 Procedural and Document Interfaces ........... 85 6.3 Facets of External Interfaces ................ 88 6.4 Discovery of Services ..................... 91 6.5 More on Document Style .................. 93 6.6 Security ............................ 99 6.7 Testing ............................. 100 6.8 Things to Remember ..................... 101 II Developing with Interfaces 102 7 A Little Process 103 7.1 The Agile Model ........................ 103 7.2 Vision ............................. 104 7.3 Conceptualization ...................... 104 7.4 Analysis and Design ..................... 110 7.5 Interface-Oriented Design .................. 110 7.6 Design ............................. 116 7.7 Implementation ........................ 120 7.8 Things to Remember ..................... 120 III Interfaces in the Real World 121 CONTENTS vii 8 Link Checker 122 8.1 Vision ............................. 122 8.2 Conceptualization ...................... 122 8.3 Analysis ............................ 124 8.4 Design ............................. 126 8.5 Tests .............................. 132 8.6 Implementation ........................ 134 8.7 Retrospective ......................... 138 8.8 Things to Remember ..................... 138 9 Web Conglomerator 140 9.1 Vision ............................. 140 9.2 Conceptualization ...................... 140 9.3 Analysis ............................ 142 9.4 Testing ............................. 145 9.5 Design ............................. 146 9.6 Implementation ........................ 148 9.7 Retrospective ......................... 151 9.8 Things to Remember ..................... 152 10 Service Registry 154 10.1 Vision ............................. 154 10.2 Conceptualization ...................... 156 10.3 Analysis ............................ 157 10.4 Design ............................. 164 10.5 Implementation ........................ 165 10.6 Published Interface ..................... 169 10.7 The Next Iterations ...................... 172 10.8 Things to Remember ..................... 175 11 Patterns 177 11.1 Introduction .......................... 177 11.2 Factory Method ........................ 177 11.3 Proxy .............................. 179 11.4 Decorator ........................... 181 11.5 Adapter ............................ 183 11.6 Façade ............................. 184 11.7 Composite ........................... 185 11.8 Things to Remember ..................... 186 CONTENTS viii A Appendix 187 A.1 More about Document Style ................ 187 A.2 Service-Oriented Architecture ............... 189 A.3 Collections and Collection Methods ............ 192 A.4 Configuration ......................... 196 A.5 Another Service Registry Iteration ............. 197 A.6 Other Interface Issues .................... 199 Preface Interface-Oriented Design explores how you can develop software with interfaces that interact with each other. We’ll look at techniques for breaking down solutions into these interacting interfaces and then for determining appropriate implementations for these interfaces to create well-structured programs. We have plenty of examples that will show youwaystocreateeffectivedesignscomposed of interfaces to objects, components, and services. And we’ll even have some fun along the way. You’ve probably learned about (and experienced) software development using object-oriented design. Interface-oriented design concentrates on the interfaces of modules, which may or may not be implemented with object-oriented languages. Designs that emphasize interfaces are loosely coupled—and that’s a good thing. If you have only an interface to which to code, you cannot write code dependent on an implementa- tion, which helps keep us honest. Distributed computing, such as service-oriented architectures, places a particular emphasis on interfaces. The interfaces may be procedure oriented (such as Remote Procedure Calls) or document oriented (such as web services). We’ll explore the transparency and loose coupling traits that are key to distributed interfaces to help you build better distributed systems. Inheritance is often a tricky technique to get correct—it is often one of the most abused features in object-oriented languages. We’ll look at designs that employ inheritance versus ones that emphasize interfaces to demonstrate the trade-offs between the two. This ongoing emphasis on interfaces may seem a bit extreme. But by looking at one extreme, you’ll start to see a different viewpoint that can give you fresh insights into your current approach to software develop- ment. ROAD MAP x Here then is a road map for our journey through interface-oriented design. Road Map Chapter 1, Introduction to Interfaces We’ll start by ordering pizza. One should never read a book on an empty stomach, so we’ll use the activities of finding a suitable pizza shop and ordering a pizza as a nonprogramming introduc- tion to interfaces. We’ll then briefly look at some code and textual interfaces as introductory background for topics we’ll explore in later chapters. Chapter 2, Interface Contracts It’s hard to use an interface if an implementation provides no guarantee of working successfully. We’ll see how the Three Laws of Interfaces applies to implementations and how Design by Con- tract helps in understanding an interface’s protocol. Finally, you’ll need to test an implementation to verify that it lives up to its side of the contract. Chapter 3, Interface Ingredients You can structure interfaces in many ways, including pull versus push and stateful versus stateless interfaces. We’ll explore the trade-offs and benefits of these facets and finish by outlining how to transform an interface from one facet to another. Chapter 4, What Should Be in an Interface? An interface should have cohesive functionality. There are no absolute rules to what makes a cohesive interface, but we’ll look at different sets of interfaces to explore the concept of cohesiveness and see how it helps development. Chapter 5, Inheritance and Interfaces Inheritance in object-oriented programs is often overused. We’ll investigate better ways to organize designs