Upload
baldric-burke
View
214
Download
0
Tags:
Embed Size (px)
Citation preview
9 - Class & Method DesignModel EnhancementDesign to CodeProposal Presentation
Class & Method Design
• Criteria, activities, & techniques used to design classes & methods
• Part of Design Phase where work is actually accomplished
• Refinement of UML models to create the most efficient system possible that still meets the users’ requirements
• The steps just before the actual coding of the system
Review of Terms
• Object – basic building block of a system; instances of classes
• Class – defines both data & processes that each object contains
• Attributes – describe data about the object
• Methods – specify processes that objects can perform
• Message – procedural call from one object to another
More Terms
• Inheritance – superior classes pass attributes & methods to subordinate classes
• Encapsulation – combining processes & data into a single object
• Information Hiding – only information required to use an object should be available outside of an object
• Polymorphism – same message is interpreted differently by different objects
• Dynamic binding – interpretation of message by an object takes place at run time
New Terms
• Cohesion – how specific a module is within a system (i.e., a class or object should represent only one thing)
• Coupling – level of interdependence among modules (classes, objects, & methods) in a system• The higher the degree of coupling, the more likely that
changes in part of a system will require changes in other parts
• Connascence – two modules are so intertwined that a change in one requires a change in the other
• Ideally, a system should have high Cohesion and low (i.e., loose) Coupling
Opportunities for Reuse
• Class Library• Set of implemented classes that are designed for
reuse• Pre-built “template” classes with Attributes and
Methods already defined• Can be utilized using Inheritance• Support Foundation Layer
• Component• Self-contained piece of software that can be
“plugged” into a system• Simplify development of objects on Problem Domain &
Human-Computer Interaction Layers
Optimizing the Design
1. Shorten/simplify paths between objects
2. Move attributes to provide more direct access
3. Reduce the number of messages (Fan-out) sent by an object
4. Reorder execution of statements
5. Cache (temporarily save) computational results
Constraints & Contracts
• Contract • Formalizes the interactions between client and
server objects• Document the message passing between objects• Contain information necessary for programmer to
understand method
• Contracts may contain:• Pre-conditions – constraints that must be met for
a method to execute• Post-conditions – constraints that must be met
after the method executes (or method is undone)
Contract Example
Method Name: Class Name: ID:
Clients (Consumers):
Associated Use Cases:
Description of Responsibilities:
Arguments Received:
Type of Value Returned:
Pre-Conditions:
Post-Conditions:
Method Specification• Method Specifications – written documents that
include explicit instructions on how to write the code to implement a method
• No standard syntax for Method Specification
• Written in Structured English – short sentences
• 4 Components:1. General Information about Method2. Events – items that trigger method3. Message Passing – what is being passed, and
returned4. Algorithm Specification (calculations, formula, etc.)
From class diagram to code• Attributes are generally implemented as instance
variables. Choose an appropriate class, but normally it will be a class from the standard Java library such as String.
• Generalizations are implemented using the extends keyword, and interfaces are implemented using the implements keyword.
• Associations are normally implemented using instance variables. • Divide each two-way association into two one-way associations
so that each associated class has an instance variable representing the other end of the association.
• One-way association where the multiplicity at the other end is ‘one’ or ‘optional’, you declare a variable whose type is that class
• To implement a one-way association where the multiplicity at the other end is ‘many’, use a collection class such as ArrayList.
• To implement an association where the multiplicity at the other end has a small, fixed upper bound, use a regular array
class Car {private String carColor;private double carPrice = 0.0;
public String getCarColor(String model) {return carColor;
}public double getCarPrice(String model) {
return carPrice;}}
public class Employee {private static String department = "R&D";private int empId;
private Employee(int employeeId) {this.empId = employeeId;
}public static String getEmployee(int emplId) {
if (emplId == 1){
return "idiotechie";}else{
return "Employee not found";
}}public static String getDepartment() {
return department;}}
public class Customer {private String name;private String address;private String contactNumber;
}public class Car {
private String modelNumber;private Customer owner;
}
public class Customer {private String name;private String address;private String contactNumber;private Car car;
}public class Car {
private String modelNumber;private Customer owner;
}
public class Car {private String brand;
public Car(String brands){this.brand = brands;
}public Car() {}public String getBrand() {
return brand;}public void setBrand(String brand) {
this.brand = brand;}}
public class Customer {private Car[] vehicles;ArrayList<Car> carList =
new ArrayList<Car>();public Customer(){
vehicles = new Car[2];vehicles[0] = new
Car("Audi");vehicles[1] = new
Car("Mercedes");carList.add(new
Car("BMW"));carList.add(new
Car("Chevy"));}}
public class Car {private String model;public void printPrice() {}public String getModel() {
return model;}public void setModel(String model) {
this.model = model;}
} public class hatchback extends Car {
private String model;public void printPrice() {
System.out.println("Hatchback Price");
}public String getModel() {
return model;}public void setModel(String model) {
this.model = model;}
}
public class PaymentSystem {}
public class Order {public void processPayment(PaymentSystem ps){
}}
dependecy
public class Student {}
public class School {private Student student;
}
public class Employee {}
public class Company {private Employee[] employee;
}
Proposal Presentation