9 - class & method design model enhancement design to code proposal presentation

21
9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

Upload: baldric-burke

Post on 18-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

9 - Class & Method DesignModel EnhancementDesign to CodeProposal Presentation

Page 2: 9 - Class & Method Design Model Enhancement Design to Code Proposal 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

Page 3: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

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

Page 4: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

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

Page 5: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

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

Page 6: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

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

Page 7: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

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

Page 8: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

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)

Page 9: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

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:

Page 10: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

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.)

Page 11: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

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

Page 12: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

class Car {private String carColor;private double carPrice = 0.0;

public String getCarColor(String model) {return carColor;

}public double getCarPrice(String model) {

return carPrice;}}

Page 13: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

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;}}

Page 14: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

public class Customer {private String name;private String address;private String contactNumber;

}public class Car {

private String modelNumber;private Customer owner;

}

Page 15: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

public class Customer {private String name;private String address;private String contactNumber;private Car car;

}public class Car {

private String modelNumber;private Customer owner;

}

Page 16: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

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"));}}

Page 17: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

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;}

}

Page 18: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

public class PaymentSystem {}

public class Order {public void processPayment(PaymentSystem ps){

}}

dependecy

Page 19: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

public class Student {}

public class School {private Student student;

}

Page 20: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

public class Employee {}

public class Company {private Employee[] employee;

}

Page 21: 9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation

Proposal Presentation