360 likes | 385 Vues
Chapter 6 Object-Oriented Software Design and Implementation. Chapter 6 Topics. Software Design Strategies Objects and Classes Revisited Object-Oriented Design The CRC Card Design Process Functional Decomposition Object-Oriented Implementation Packages
E N D
Chapter 6 Object-Oriented Software Design and Implementation
Chapter 6 Topics • Software Design Strategies • Objects and Classes Revisited • Object-Oriented Design • The CRC Card Design Process • Functional Decomposition • Object-Oriented Implementation • Packages • Ethics and Responsibility in the Computing Profession
Software Design Strategies FUNCTIONALOBJECT-ORIENTED DECOMPOSITIONDESIGN The problem is divided into more easily handled subproblems, the solutions of which together create a solution to the overall problem. Produces a hierarchy of tasks. The solution is expressed in terms of objects (self-contained entities composed of data and operations on that data) that interact by sending messages to one another. Produces a hierarchy of objects.
More about OOD • Languages supporting OOD include: Java, Smalltalk, Eiffel, CLOS, and Object-Pascal • A class defines the pattern used when instantiating an object of that type • A class generally contains private data and public operations (called methods)
Object-Oriented Design (OOD) • Focus is on the entities (objects) in a problem • Beginsby identifying the classes of objects in the problem, and choosing appropriate operations on those objects • Programs are collections of objects that communicate with (send messages to) each other • Data plays a leading role; algorithms are used to implement operations on the objects and to enable interaction of objects with each other
OOD good with large software projects • Objects within a program often model real-life objects in the problem to be solved • The OOD concept of inheritance allows the customization of an existing class to meet particular needs. This can reduce the time and effort needed to design, implement, and maintain large systems
Functional Decomposition • A technique for developing a program in which theproblem is divided into more easily handled subproblems,the solutions of which create a solution to the overall problem • In functional decomposition, we work from the abstract (a list of the major solution steps for which some implementation details remain unspecified) to the concrete (algorithmic steps for which the implementation details are fully specified)
Functional Decomposition • Focus is on the sequence of actions (algorithms) required to solve the problem • Begins by breaking the solution into a series of major steps. This process continues until each subproblem cannot be divided further or has an obvious solution • Programs are collections of modules that solve subproblems; a module structure chart (hierarchical solution tree) is often created • Data plays a secondary role in support of actions
Definitions in Functional Decomposition • Concrete step A step for which the implementation details are fully specified • Abstract step A step for which some implementation details remain unspecified • Module A self-contained collection of steps that solves a problem or subprogram; it can contain both concrete and abstract steps
Find Weighted Average Print Weighted Average Module Structure Chart Main Get Data Prepare File for Reading Print Data Print Heading
. . . . . . . . . OBJECT OBJECT FUNCTION FUNCTION FUNCTION Two Design Strategies FUNCTIONALOBJECT-ORIENTED DECOMPOSITION DESIGN OBJECT
Object-Oriented Design Process • Three steps in the process • Identify an initial set of object classes that seem relevant to the problem • nouns often represent objects • verbs often represent actions • Filter the list, eliminating duplicates • Identify the responsibilities for the reduced list of objects
Identify Possible Classes • Brainstorming (by a team) • Identify objects • Propose classes • Write on a blackboard • Keep going around until no one can think of any more objects • No ideas are rejected
Filter List of Possible Classes • Eliminate duplicates • Decide if the classes really do represent objects in the problem • Look for classes that are related • Over looked classes may emerge • For each class that survives the filtering stage, create a CRC card
Blank CRC Card Class Name: Superclass: Subclasses: Responsibilities Collaborations
Definitions • Responsibilities An action that an implementation of an object must be capable of performing • Collaboration An interaction between objects in which one object requests that another object carry out one of its responsibilities
Determine Responsibilities • Initial responsibilities • Know and return the states of the object • Called Knowledge Responsibilities • Action responsibilities Use scenario walk-throughs, a role playing technique, • to explore the actions of a class • to explore the interactions of classes
Address CRC Card Class Name: Superclass: Subclasses: Address Responsibilities Collaborations Create itself None (name, city, state, zip code) Know its name None Know its city None Know its state None Know its zip code None
Name CRC Card Class Name: NameSuperclass: Subclasses: Responsibilities Collaborations Create itself (First, Middle, Last) None Know its first name None Know its middle name None Know its last name None Are two names equal? String Compare two names String
Class Name: NameSuperclass: Subclasses: Responsibilities Collaborations Create itself (First, Middle, Last) None Know its first name None return String Know its middle name None return String Know its last name None return String Are two names equal? String return boolean Compare two names String return int Name CRC Card with Return Types
CRC Card Summary • The use of Classes, Responsibilities and Collaborations (CRC) cards is an informal technique for developing objected-oriented designs • For each class, a CRC card is created listing the class responsibilities and collaborations • Brainstorming, filtering, and scenarios are used to identify and refine the classes and responsibilities needed to solve a problem
Inheritance • Inheritance A mechanism that enables us to define a new class by adapting the definition of an existing class • Example on next slide • Detailed explanation in next Chapter
Address object inheritance hierarchy Object Address HomeAddress CompanyAddress WorkAddress BoxAddress
Categories of Responsibilities • Constructor An operation that creates a new instance of a class • Copy constructor An operation that creates a new instance by copying an existing instance, possibly altering its state in the process • Transformer An operation that changes the state of an object • Observer An operation that allows us to observe the state of an object without changing it • Iterator An operation that allows us to process all the components of an object one at a time
Methods are written to specifications (as on CRC card) • The specifications state the result type, the parameter types, and what task the method is to perform using its parameters • The advantage is that teamwork can occur without knowing what the argument identifiers will actually be
Instance Methods • CRC card responsibilities are implemented as methods in the class • A responsibility that refers to an object should be implemented as an instance method • Constructors, observers, transformers, and iterators are instance methods; they have access to the fields of their associated object, and can receive values through the parameter list
Three Categories of Data • Instance data is the internal representation of a specific object. It records the object’s state. • Class data is accessible to all objects of a class. • Local data is specific to a given call of a method.
Instance Data Instance data is the internal representation of a specific object. public class Name { // Instance variables String first; String middle; String last; . . . }
Class Data • Class data is accessible to all objects of a class. • Fields declared as static belong to the class rather than to a specific instance. public class Name { // Class constant static final String PUNCT = “, ”; . . . }
Local Data • Local data is specific to a given call of a method. • The JVM allocates space for this data when the method is called and deallocates it when the method returns. public int compareTo(Name otherName) { int result; // Local variable . . . return result; }
Package Syntax Compilation Unit packageIdentifier ; ImportDeclaration . . . ClassDeclaration . . .
Package Do’s and Don’t’s • A compilation unit can have only one public class • Many compilation units can be in a package • “No modifier” specifies package access • Any field or method with package access can be accessed by any member of the package
Package Example • package addressBook • Members: • class Address • class Entry • Imported by • class AddressDr • All of the variables have package access
Ethics and Responsibilities • Software piracy The unauthorized copying of software for either personal use or use by others Have you every made unauthorized copies? Has anyone you know done so? Why is it wrong to do so?
More on Ethics • Virus Code that replicates itself, often with the goal of spreading to other computers without authorization, and possibly with the intent of doing harm Has a virus ever attached your machine? Why is creating and/or spreading a virus unethical?