Foundations of Object-Oriented Programming Using .NET 2.0 Patterns solves the object-oriented and pattern-programming problem by mixing the two—the book teaches object-oriented concepts using patterns, or a solutions-based approach. The book's material is organized around tasks and patterns, and illustrated through development problems and solutions that include persistence, code efficiency, and good design.
This book is of special interest to those who want to learn how to use .NET 2.0 Generics in conjunction with patterns. This unique book is based on the author's lectures, and the information unfolds in a practical manner.
Le informazioni nella sezione "Riassunto" possono far riferimento a edizioni diverse di questo titolo.
Christian Gross is a consultant with vast experience in the client/server world. He has consulted for Microsoft on DNA solutions, and he has held consulting positions with Daimler Benz, Microsoft, NatWest, and other major corporations. Gross was a contributor to Professional Active Server Pages, Professional SQL Server 6.5 Administration, Professional NT Internet Information Server Administration, and Programming Microsoft Windows 2000 Unleashed. He is the author of A Programmer's Introduction to Windows DNA.
Foundations of
Object-Oriented
Programming Using
.NET 2.0 Patterns
■ ■ ■
Christian Gross
Foundations of Object-Oriented Programming Using .NET 2.0 Patterns
Copyright © 2006 by Christian Gross
Lead Editor: Jonathan Hassell
Technical Reviewer: Brian Myers
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore,
Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser
Project Manager: Sofia Marchant
Copy Edit Manager: Nicole LeClerc
Copy Editor: Ami Knox
Assistant Production Director: Kari Brooks-Copony
Production Editor: Linda Marousek
Compositor: Susan Glinert Stevens
Proofreader: Elizabeth Berry
Indexer: Valerie Perry
Interior Designer: Van Winkle Design Group
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Library of Congress Cataloging-in-Publication Data
Gross, Christian.
Foundations of object-oriented programming using .NET 2.0 patterns / Christian Gross.
p. cm.
ISBN 1-59059-540-8
1. Object-oriented programming (Computer science) 2. Microsoft .NET. 3. Software patterns. I. Title.
QA76.64.G8 2005
005.1'17--dc22
2005025961
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or
visit http://www.springeronline.com.
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA
94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com in the Source Code section.
You will need to answer questions pertaining to this book in order to successfully download the code.
v
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xi
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
■CHAPTER 1 Essentials of Object-Oriented Programming . . . . . . . . . . . . . . 1
Understanding Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Modular vs. Reusable Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Using Scope to Control Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Understanding Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Differences Between Inheritance and Structs . . . . . . . . . . . . . . . . . . . 8
Simple Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Inheritance Using Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Inheritance in a Software-Engineering Context . . . . . . . . . . . . . . . . . . . . . 14
Writing Generic Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
The Case for Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Some Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Asynchronous Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Some Final Thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
■CHAPTER 2 Logging, Errors, and Test-Driven Development . . . . . . . . . . 23
Implementing Logging Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
A Simple Log4net Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Generating Messages Using the ILog Interface . . . . . . . . . . . . . . . . . 26
Managing a Configuration File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Setting Up a Real-Life Configuration . . . . . . . . . . . . . . . . . . . . . . . . . 29
Implementing ToString. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Implementing Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Classifying an Error and an Exception . . . . . . . . . . . . . . . . . . . . . . . . 37
Implementing an Error Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Implementing an Exception Strategy . . . . . . . . . . . . . . . . . . . . . . . . . 47
vi ■CONTENTS
Using NUnit for Test-Driven Development . . . . . . . . . . . . . . . . . . . . . . . . . 52
Understanding Test-Driven Development . . . . . . . . . . . . . . . . . . . . . 52
Enabling NUnit in Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Running NUnit Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
More Detailed NUnit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Some Final Thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
■CHAPTER 3 Defining the Foundation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Defining a Base for the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Defining Intentions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Implementing Test-Driven Development . . . . . . . . . . . . . . . . . . . . . . 63
Implementing the Bridge Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Keeping Options Open Using Interfaces . . . . . . . . . . . . . . . . . . . . . . . 69
Too Much of a Good Thing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
What About .NET 1.x? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Bridge Pattern Implementation Variations . . . . . . . . . . . . . . . . . . . . . . . . . 71
Implementing Application Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Controller Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Implementing a Default Base Class . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Interface and Class Design Decisions . . . . . . . . . . . . . . . . . . . . . . . . 80
Instantiating Types with the Factory Pattern . . . . . . . . . . . . . . . . . . . . . . . 84
The Need for a Helper Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Implementing a Plug-In Architecture . . . . . . . . . . . . . . . . . . . . . . . . . 85
Creating Objects According to a Plan . . . . . . . . . . . . . . . . . . . . . . . . . 86
Cloning Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Some Final Thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
■CHAPTER 4 Application Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Making an Application Work Properly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Extensibility and Maintainability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Using Black Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Pipes and Filters Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
An Example: Buying a Television from Amazon.com . . . . . . . . . . . . 93
Architecting the Television Selection System . . . . . . . . . . . . . . . . . . 94
Implementing the Television Selection System. . . . . . . . . . . . . . . . . 95
Some Final Notes About the Pipes and Filters Pattern . . . . . . . . . . 104
■CONTENTS vii
Client-Dispatcher-Server Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Defining the Client-Dispatcher-Server Pattern Architecture . . . . . 106
A Static Dispatcher Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
A Dynamic Dispatcher Architecture . . . . . . . . . . . . . . . . . . . . . . . . . 108
Architecting the Client-Dispatcher-Server Pattern . . . . . . . . . . . . . 110
Implementing the Assembly Directory Resolver . . . . . . . . . . . . . . . 113
Implementing a Web Service Resolver. . . . . . . . . . . . . . . . . . . . . . . 118
Micro-Kernel Pa...
Le informazioni nella sezione "Su questo libro" possono far riferimento a edizioni diverse di questo titolo.
EUR 3,32 per la spedizione in U.S.A.
Destinazione, tempi e costiEUR 3,53 per la spedizione in U.S.A.
Destinazione, tempi e costiDa: HPB-Red, Dallas, TX, U.S.A.
paperback. Condizione: Good. Connecting readers with great books since 1972! Used textbooks may not include companion materials such as access codes, etc. May have some wear or writing/highlighting. We ship orders daily and Customer Service is our top priority! Codice articolo S_370420715
Quantità: 1 disponibili
Da: ThriftBooks-Atlanta, AUSTELL, GA, U.S.A.
Paperback. Condizione: Good. No Jacket. Pages can have notes/highlighting. Spine may show signs of wear. ~ ThriftBooks: Read More, Spend Less 1.6. Codice articolo G1590595408I3N00
Quantità: 1 disponibili
Da: Half Price Books Inc., Dallas, TX, U.S.A.
paperback. Condizione: Very Good. Connecting readers with great books since 1972! Used books may not include companion materials, and may have some shelf wear or limited writing. We ship orders daily and Customer Service is our top priority! Codice articolo S_411326704
Quantità: 1 disponibili
Da: Better World Books Ltd, Dunfermline, Regno Unito
Condizione: Very Good. Ships from the UK. Former library book; may include library markings. Used book that is in excellent condition. May show signs of wear or have minor defects. Codice articolo 16155107-20
Quantità: 1 disponibili
Da: Lucky's Textbooks, Dallas, TX, U.S.A.
Condizione: New. Codice articolo ABLIING23Mar2811580082119
Quantità: Più di 20 disponibili
Da: Basi6 International, Irving, TX, U.S.A.
Condizione: Brand New. New. US edition. Expediting shipping for all USA and Europe orders excluding PO Box. Excellent Customer Service. Codice articolo ABEJUNE24-195449
Quantità: 1 disponibili
Da: Romtrade Corp., STERLING HEIGHTS, MI, U.S.A.
Condizione: New. This is a Brand-new US Edition. This Item may be shipped from US or any other country as we have multiple locations worldwide. Codice articolo ABNR-124437
Quantità: 1 disponibili
Da: Books Puddle, New York, NY, U.S.A.
Condizione: Used. pp. 380. Codice articolo 263071963
Quantità: 1 disponibili
Da: The Book Spot, Sioux Falls, MN, U.S.A.
Paperback. Condizione: New. Codice articolo Abebooks539335
Quantità: 1 disponibili
Da: Majestic Books, Hounslow, Regno Unito
Condizione: Used. pp. 380 Illus. Codice articolo 5857284
Quantità: 1 disponibili