CPS 108, Spring 2004Application Programming Interface, APIProgram Design and ImplementationAdministriviawordlines.cpp, understanding codeQuestions about wordlines.cppClasses: Review/OverviewC++ (and Java) class constructionDesign CriteriaHow to codeXP and RefactoringModules, design, coding, refactor, XPDesign Heuristics: class/program/functionC++ idioms/general conceptsStandard LibrariesEric RaymondSoftware Design1.1CPS 108, Spring 2004Software Design and ImplementationObject oriented programming and design•Language independent concepts including design patterns, e.g., Model-View-Controller, iterator, factory, bridge, …•Design independent concepts, e.g., coupling, cohesion, testing, refactoring, profiling, …What’s in the course?C++ and Java, team projects, mastery exams•team projects can be more and less than the sum of their partshigh-level abstractions, low-level details•patterns, heuristics, and idiomsSoftware Design1.2Application Programming Interface, APIStandard C++ headers, STL headersCurseswxWindowsJava APIjava.util, javax.swing, java.net, javax.crypto, …Eclipse SWTGoogle APIGracenote APISoftware Design1.3Program Design and ImplementationLanguage independent principles of design and programmingdesign heuristics•coupling, cohesion, small functions, small interfaces ...design patterns•factories, adapter, MVC, decorator, iterator, …Language specific:Idioms•smart pointers, vectors/arrays, overloaded operators ...idiosyncracies, idiocies•must define virtual destructor, stream zoo in Java, ...Software Design1.4Administriviacheck website and bulletin board regularlyhttp://www.cs.duke.edu/courses/cps108/current/See links to bulletin board and other stufGrading (see web pages)group projects: small, medium, largemastery programs (solo or semi-solo endeavors)readings and summariestestsEvaluating team projects, role of TA, UTA, consultantsface-to-face evaluation, early feedbackCompiling, tools, environments, Linux, Windows, Macg++ 3.3, Java 2 aka 1.4, JRE, …Software Design1.5wordlines.cpp, understanding code typedef map<string, set<int> > linemap; while (getline(input,line)) { linecount++; istringstream iline(line); while (iline >> w) { linemap::iterator it = info.find(w); if (it == info.end()) { set<int> si; si.insert(linecount); info.insert(make_pair(w,si)); } else { it->second.insert(linecount); } } }Software Design1.6Questions about wordlines.cppConceptually, what's a map and what's a set?In terms of implementation how do they work?What's an iterator (abstractly and concretely)What are streams? ifstream, cin/cout, istring/ostring, …What's a templated class?Other questions?Software Design1.7Classes: Review/OverviewA class encapsulates state and behaviorBehavior first when designing a classInformation hiding: who knows state/behavior?State is private/protected; some behavior is publicPrivate/protected helper functionsA class is called an object factory, creates lots of instancesClasses communicate and collaborateParameters: send and receiveContainment: has a reference toInheritance: is-aSoftware Design1.8C++ (and Java) class constructionC++ uses .h and .cpp, Java uses .javaDocumentation diferent (javadoc vs. ???)Default ctor, copy constructor, destructor, assignment operatortvector, string, DateCopy constructor needed to avoid shallow copyIn C++ destructors needed to free resources/self, Java?Clone makes copy in Java (rare), share is default Private, protected, public, (package)Private default in C++, package default in JavaPer method declaration in Java, class sections in C++Software Design1.9Design Criteria Good design comes from experience, experience comes from bad designFred Brooks (or Henry Petroski)Design with goals:ease of useportabilityease of re-useefficiencyfirst to market?????Software Design1.10How to codeCoding/Implementation goals:Make it runMake it rightMake it fastMake it smallspiral design (or RAD or !waterfall or ...)what’s the design methodology?specificationdesignimplementationSoftware Design1.11XP and Refactoring(See books by Kent Beck (XP) and Martin Fowler (refactoring))eXtreme Programming (XP) is an agile design processCommunication: unit tests, pair programming, estimationSimplicity: what is the simplest approach that works?Feedback: system and clients; programs and storiesCourage: throw code away, dare to be great/diferentRefactoringChange internal structure without changing observable behaviorDon’t worry (too much) about upfront designSimplicity over flexibility (see XP)Software Design1.12Modules, design, coding, refactor, XPMake it run, make it right, make it fast, make it smallDo the simplest thing that can possibly work (XP)Design so that refactoring is possibleDon’t lose sight of where you’re going, keep change in mind, but not as the driving force [it will evolve]Refactor: functionality doesn’t change, code doesShould mean that new tests aren’t written, just re-runDepends on modularity of code, testing in piecesWhat’s a module in C++Could be a class, a file, a directory, a library, a namespaceWe should, at least, use classes, files, directoriesSoftware Design1.13Design Heuristics: class/program/function(see text by Arthur Riel)Couplingclasses/modules are independent of each othergoal: minimal, loose couplingdo classes collaborate and/or communicate?Cohesionclasses/modules capture one abstraction/modelkeep things as simple as possible, but no simplergoal: strong cohesion (avoid kitchen sink)The open/closed principleclasses/programs: open to extensibility, closed to modificationSoftware Design1.14C++ idioms/general conceptsGenericityTemplates, STL, containers, algorithmsCopy/Assignment/MemoryDeep copy model, memory management “required”Low-level structuresC-style arrays and strings compared to STL, TapestryconstGood for clients, bad for designers/coders?From C to C++ to Javafunction pointers, function objects, inheritanceSoftware Design1.15Standard LibrariesIn C++ there is the Standard Library, formerly known as the Standard Template Library or STLEmphasizes generic programming (using templates)Write a sorting routine, the
View Full Document