Data Management

profilekrish21
DMA605DataManagement-Week7.pdf

Class and Method Design Dr. Ardeshir Badr

Objectives

• Become familiar with how to create classes, add attributes and methods

• Become familiar with how to instantiate objects from classes

• Become familiar with how to send messages to an object

• Be able to identify the reuse of predefined classes and libraries

• Class and method design is the most important part of the design workflow

• During design, instructions and guidelines are created for developers of the classes telling them how to code classes

• Low level detailed design at this stage is critical in spite of reusable class and libraries available, because:

• Using CASE tools, a lot of code can be generated from detailed design

• Even pre-exiting classes and components need to be understood and organized

• It is common for the developers to write code and produce classes from detailed design

• We should not jump into coding without careful class design first

Classes and Method Design

Levels of abstraction in Object Oriented Systems

System

Package

Class/Object

Variable/ Attribute

Method

Library

Higher

Lower

Classes and Objects, Methods and Messages

Patient

-name -address -birthdate -phone -insurance carrier

+make_appointment() +calculate_last visit() +change_status() +provide_medical_history() +create()

James Frank : Patient

Andy Jones : Patient

Class

Objects

A Class is the general template to define and create instances, or objects

An Object is an instantiation of a class

Attributes describe information about an object, for example, patient’s name, birth date, address, and phone number

Attributes can also represent relationships between objects, for example, a department attribute in an employee object that refers to the value of a department object that captures in which department the employee object works

The behavior in each object specifies what the object can do, for example, an appointment object can schedule a new appointment, delete an appointment, and locate the next available time/date. Behaviors are implemented as methods

Messages are information sent to objects to trigger methods, which is essentially calling of the function or procedure of one object by another object

Attributes

Methods

Encapsulation and Information Hiding

• Encapsulation is the combination of data and process into a single entity

• Information hiding suggests that only the needed information to use a software module be published to the user of that module. For example, the internal mechanism of a function does not need to appear to a user of that function as long as the function occurs

• The fact that we can use an object by calling its methods makes reusability possible

• The only information that an object needs to know about another object is the set of methods and what messages need to be sent to trigger them

Inheritance Person

Doctor Patient

General Practitioner Specialist

Superclass for Doctor and

Patient

Superclass for General

Practitioner and Specialist

Abstract class

Abstract class

Concrete class

Concrete class

Concrete class

Classes are arranged in a hierarchy of superclasses or general, and subclasses or specific classes

Subclasses inherit the appropriate attributes and methods from the superclasses above them

Concrete classes are the ones that have instances

Classes that produce no instance and are used as templates for other more specific classes are called abstract classes

Inheritance

Doctor

-medical_school_specialty

+update_medical_specialty()

Patient

-insurance_carrier

+update_insurance_carrier()

Patient

-name -address -birthdate -phone -insurance_carrier

+update_birth_date() +update_insurance_carrier()

Doctor

-name -address -birthdate -phone -medical_school_specialty

+update_birth_date() +update_medical_specialty()

Person

-name -address -birthdate -phone

+update_birth_date(}

Polymorphism and Dynamic Binding

aCircle

aTriangle

In Polymorphism the same message can be interpreted by different classes of objects differently. For example, if an artist sent the message Draw_yourself to a Circle object and a Triangle object, the results will be different even if the message is the same

Dynamic Binding makes polymorphism possible. Dynamic binding or late binding is a technique that delays typing the object until run time

In Static binding the object type is determined at compile time

anArtist

Polymorphism example in Python

class Bear(object): def sound(self):

print ('Groarrr')

class Dog(object): def sound(self):

print ('Woof woof!')

def makeSound(animalType): animalType.sound()

bearObj = Bear() dogObj = Dog()

makeSound(bearObj) makeSound(dogObj)

class Circle(object): def draw_yourself(self):

print ('Drawing Circle')

class Triangle(object): def draw_yourself(self):

print ('Drawing a Triangle')

def draw_a_shape(shape): shape.draw_yourself()

circle = Circle() triangle = Triangle()

draw_a_shape(circle) draw_a_shape(triangle)

11

Python

✓ For Windows users:

✓ go to: https://www.python.org/

✓ Click on Downloads and under Downloads for Windows select Python 3.6.3

✓ Save python-3.6.3.exe

✓ Run python-3.6.3.exe (as administrator if possible)

✓ Once installed, go to Start and then select Python from the list of applications

✓ Under Python, select Python command line

✓ For Mac users:

✓ go to: https://www.python.org/

✓ Click on Downloads and select Mac OS X

✓ Select Python 36.3

Visual Studio Community Version

• https://www.visualstudio.com/downloads/

• Visual Studio Community 2017

• Click on Free Download

• Save the file vs_community__1034607155.1487127638.exe

• Double click on vs_community__1034607155.1487127638.exe

• In Workloads window, Select .NET Desktop Development to be installed and click on Install

• After installation completed click on Launch

• Create a new project by File -> New Project

• Under Visual C# select Windows Classic Desktop, and select Console App

• Give an appropriate name and select/create a suitable folder to save your project

IDE

C#

• https://docs.microsoft.com/en-us/dotnet/csharp/programming- guide/

• The link above provides information to C# language features

Programming language

• A good design is one that balances trade-offs to minimize the total cost of the system over its entire life time

• Criteria to evaluate a good design: • Coupling: How interdependent or interrelated classes, objects, and methods are

• Cohesion: How single-minded a class, object, or method is within a system

• Connascence: Generalizes the idea of cohesion and coupling and combines them with the argument for encapsulation

Design Criteria

• The higher the interdependency, the more likely changes in part of a design can cause changes to be needed in other parts of a design

• Coupling type: • Interaction coupling: Coupling among methods and objects through message passing.

Interaction coupling should be minimized. • Demeter law: An object should send message only to one of the following: Itself, an object that

is contained in an attribute of that object, an object that is passed as a parameter to the method, an object that is created by the method, or an object that is stored in a global variable

• Inheritance coupling: How tightly coupled the classes are in the inheritance hierarchy. A high level of inheritance coupling, for example subclasses calling super classes methods, should be avoided as much as possible • Ensure inheritance is only used to support generalization/specialization semantics and the

principle of substitutability

Coupling

• A class or object should represent only one thing, and a method should solve only a single task

• Types of cohesion: • Method cohesion: A method should focus on doing only one thing. From good to bad,

theses are method cohesion type: • Functional, Sequential, Communicational, Procedural, Temporal, Logical, Coincidental

• Class cohesion: Is the level of cohesion among the attributes and methods of a class. A class should represent only one thing and attributes and methods contained in that class should be required for the class to represent the thing • Ideal class cohesion: Should have multiple methods that are visible, and each visible method

contains only a single function, all methods reference only attributes or other methods defined within the class, an no control coupling between the visible methods

• From good to bad class cohesion types: Ideal, Mixed-Role, Mixed-Domain, Mixed-Instance

• Generalization/specialization cohesion: How are the classes in the inheritance hierarchy related. A kind of relationship should exist in the class hierarchy semantically

Cohesion

• A software quality metric to allow reasoning about the complexity caused by dependency relationship in Object Oriented Design

• Two class or methods are so intertwined that a change in one would necessitate a change in other

• We should minimize Connascence by eliminating unnecessary Connascence: • Minimize Connascence across class and method boundaries • Maximize Connascence within any encapsulation boundary

• Type of Connascence: • Name: A method refers to the name an attribute and if the attribute name changes, the method has

to change • Class: A class has an attribute A, and if the attribute type changes the attribute declaration has to

change • Convention: A class has an attribute in which a range of values has a semantic meaning. If the range

changes then every method that uses that attribute has to be modified • Algorithm: Two different methods rely on an algorithm to execute correctly and if the algorithm

changes the methods has to change • Position: The order of code in a method or the order of parameters to a method is important and if

is not correct it could affect the method

Connascence

Object Design Activities

Add Specifications

Identify Opportunities for

reuse

Restructure the Design

Optimize the Design

Map Problem Domain Classes to Implementation

Language

1. Add any missing attributes, methods, classes

2. Finalize the visibility of each attribute and method in classes (language dependent)

3. Decide on the signature of methods: name of method, parameters and type, type of value method returns

4. Add constraints, e.g., attributes of an object can have values in certain range and error handling

1. Use of design patterns, e.g., forwarder-receiver pattern

2. Frameworks which is a set of implemented classes that can be used as basis for implementing an application

3. Libraries has a set of implemented classes that were designed for reuse, e.g., data management layer library

4. Component is a set of self-contained, encapsulated piece of software that can be plugged into a system

1. Factor 2. Normalize: All

associations relationships must be converted into attributes in the classes

3. Ensure all inheritance relationships are only generalization/spe cialization (a-kind- of)

1. Review the access paths between objects and make sure the paths are not too long

2. Review each attribute of each class and move attributes to the calling class if only read and update methods are used

3. Review the number of messages sent by each method. If the number is high the method should be optimized by using index for example

1. Map the current design to the capabilities of the programming languages, for example, C++, JAVA, C#, Python

Sure Products class design

Draw a class diagram showing all classes, attributes, operations, and relationships for the following case:

• A company has a number of employees. The attributes of Employee include employeeID (primary key), name, address, and birth date. The company has also several projects. Attributes of Project include projectName and startDate. Each employee may be assigned to one or more projects, or may not be assigned to a project. A project must have at least one employee assigned, and it may have any number of employees assigned. An employee’s billing rate may vary by project, and the company wishes to record the applicable billing rate for each employee when assigned to a particular project. At the end of each month, the company mails a check to each employee who has worked on a project during that month. The check amount is based on the billing rate and the hours logged for each project assigned to the employee.

Activity

• Go through the slides of this class plus the notes you have taken

• Go through the exercises, discussions, and examples

• Read the slides for next class (will be available one day prior to next class)

• Continue working on your project

• Continue working on your assignments

• Work on your presentation

Before next class