Download presentation
Presentation is loading. Please wait.
Published byDayna Rice Modified over 9 years ago
1
CS2110: SW Development Methods Design of methods (functions) Class design – CRC cards – UML class and sequence diagrams Software Design
3
Old problems Before OO, many languages followed the procedural paradigm –A program was based on functions that worked like static methods in Java –A “main” function started things functional decomposition –Break problem down –Function deals with subproblem –Hierarchical. Levels of abstraction Problems: –Control and coordination centered in main method –Required changes have big impacts –Logic may be distributed –Data changes affect may modules
4
Functions: What we call them: functions, methods, modules Functions are a core construct in OO and non-OO programs Principals of good functions apply in either language –But less of a problem in OO – can you think why?
5
What makes a function good? Your answers:
6
Good qualities in functions: Lots written about this! Clean Code: A Handbook of Agile SW Craftsmanship Robert C. Martin –Read Chapter 3 in UVa digital library version
7
Good Qualities in Functions Small –How small? Do One Thing: Strong Cohesion –Why? –One level of abstraction per function Thus, functions fall into levels of abstraction –Name reflects the one task it does
8
Interface Qualities of Functions What about its interface? –Inputs, return value/arguments –Other sources of data or “output” Side effects Some rules?
9
Good Qualities in Functions (2) Number of arguments: small Flag arguments –What are they? What bad thing do they suggest is happening? Avoid side effects Avoid “output” arguments –Arguments that are changed after the function executes –Java: possible for “mutable” classes
10
Good Qualities in Functions (3) Don’t Repeat Yourself (DRY) Command/Query Separation –Do something. Or answer something. Don’t do both. Returning Errors –Return value? Output argument? Burden on caller? –Exceptions: what advantages?
12
Classes So far we ’ ve just talked about objects We note that many objects are a “ type of ” the same kind of thing – The same “ abstraction ” – E.g. 1, 2, 7 and 10 – whole numbers – E.g. 1, 3.5, 25, pi – numbers – Bob, Sally, Joe – Students But, they are Persons too, aren ’ t they? – cs201, cs202, engr162 – Courses
13
Classes, Type Classes define a set of objects with the same properties (state, behavior) A class definition serves as a “ cookie cutter ” for creating new objects – Instantiation of an object of a certain class – In Java, we do this with new and a constructor is called – Creates an individual object, also called an instance Variables and data objects have a type – What the rules are for that object – An object ’ s class is one form of object-type Also any superclass it extends, and any interface it implements
14
Finding Classes from Specifications Study the textbook, Sect. 2.3.1 Next In-Lab exercises – Several problems for groups to do in lab – Later you ’ ll get sample solutions for all these problems
15
Sample Problem: Library Loans Problem overview: Imagine you were going to write a set of classes that would be part of a software system to support loans in a library. A description might be: The library contains books and journals. It may have several copies of any given book. Books and journals have titles and a call-number. Each copy has a copy-number. For each book we record the author. Some of the books are on "reserve" and can only be checked-out for 3 hours, but all other books can be borrowed as a regular loan (for a three week period). Members of the library can normally borrow up to six items at a time, but staff members can borrow up to ten items. Members of staff can borrow journals, but not regular members. The system must keep track of when books and journals are borrowed and returned. Our system might want find out what items have been borrowed by a given member, and which copies of an item have been checked out and by whom.
16
Class Identification What are possible classes here? Why? How do you recognize classes? What makes a good class name?
17
Sample Problem: Library Loans Problem overview: Imagine you were going to write a set of classes that would be part of a software system to support loans in a library. A description might be: The library contains books and journals. It may have several copies of any given book. Books and journals have titles and a call-number. Each copy has a copy-number. For each book we record the author. Some of the books are on "reserve" and can only be checked-out for 3 hours, but all other books can be borrowed as a regular loan (for a three week period). Members of the library can normally borrow up to six items at a time, but staff members can borrow up to ten items. Members of staff can borrow journals, but not regular members. The system must keep track of when books and journals are borrowed and returned. Our system might want find out what items have been borrowed by a given member, and which copies of an item have been checked out and by whom.
18
Methods and Fields Pick a class: name its methods and fields
19
Class Relationships Do any classes have “ structural links ” to other classes? –“ link ” might mean “ part-of ” relationship. – This is a static relationship – Are such “ links ” one-to-one, one-to-many? Which objects might call methods on objects of another class? – This is a dynamic relationship (while the program is running)
20
Another View on Defining Classes Classes have – Responsibilities In words: what is its role? Data encapsulation and management? Organizer of other objects? Some level of control or task-execution? Methods implement these – Relationships Static connections to other objects Dynamic interactions with other objects
21
The CRC Card Technique Read info on “CRC cards” here: http://www.agilemodeling.com/artifacts/crcM odel.htm http://www.agilemodeling.com/artifacts/crcM odel.htm
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.