CSC 480 Software Engineering

Slides:



Advertisements
Similar presentations
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Advertisements

Chapter 8 Designing Classes. Assignment Chapter 9 Review Exercises (Written)  R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13, 8.15, 8.19, 8.20 Due Friday,
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
Chapter 3 (Horstmann’s Book) Guidelines for Class Design Hwajung Lee.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 10 Object-Oriented Modeling.
Software Testing and Quality Assurance
Class Design CSC 171 FALL 2004 LECTURE 11. READING Read Chapter 7 It’s abstract But it should help with project #1.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Information Hiding and Encapsulation
1 Class design guidelines. 2 Encapsulation Classes can be implemented many different ways –each has advantages & disadvantages –improvement/revision always.
1 Object-Oriented Design. 2 Objectives F To become familiar with the process of program development. F To the relationship types: association, aggregation,
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Component-Based Software Engineering Components and Interfaces Paul Krause.
Ranga Rodrigo. Class is central to object oriented programming.
Encapsulation CMSC 202. Types of Programmers Class programmers – Developers of new classes – Goal: Expose the minimum interface necessary to use a new.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
More About Classes Ranga Rodrigo. Information hiding. Copying objects.
Chapter 3 Guidelines for Class Design. Objective of this chapter Have “bottom up point of view” Learn how to write a single class well.  The classes.
CSC 480 Software Engineering Design by Contract. Detail Design Road Map Begin with architectural models  Class model: domain classes  Overall state.
CSC 205 Java Programming II Defining & Implementing Classes.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
OOP with Objective-C Categories, Protocols and Declared Properties.
(c) University of Washington13-1 CSC 143 Java Specifications: Programming by Contract Reading: Ch. 5.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Chapter 2 Principles of Programming and Software Engineering.
Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI
CSCE 240 – Intro to Software Engineering Lecture 3.
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Principles of Programming & Software Engineering
ITEC324 Principle of CS III
Chapter 10 Thinking in Objects
Classes and Objects: Encapsulation
Chapter 10 Thinking in Objects
CMPE 135: Object-Oriented Analysis and Design September 14 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
Chapter 10 Thinking in Objects
Chapter 11 Object-Oriented Design
Topics: jGRASP editor ideosyncrasies assert debugger.
Chapter 3: Using Methods, Classes, and Objects
Classes and Objects 2nd Lecture
Creating and Using Classes
Computer Science II Exam 1 Review.
CSC 205 – Java Programming II
CSC 205 Programming II Lecture 2 Subclassing.
Classes and Objects: Encapsulation
Chapter 9 Thinking in Objects
Defining Your Own Classes Part 1
Classes and Objects Encapsulation
Object initialization: constructors
Chapter 9 Thinking in Objects
Defining Classes and Methods
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Defining Classes and Methods
Defining Classes and Methods
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 2. Problem Solving and Software Engineering
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Information Hiding and Encapsulation Section 4.2
CMSC 202 Encapsulation Version 9/10.
CS 240 – Advanced Programming Concepts
CSG2H3 Object Oriented Programming
Chapter 5 Classes.
Presentation transcript:

CSC 480 Software Engineering Encapsulation

Topics OOD guidelines: an overview Encapsulation Accessors and mutators Side effects Pre- and postcoditions Invariants

Elements of the Object Model Abstraction: model the essential attributes Encapsulation: hide implementation details Modularity: (think package in Java) Hierarchy: ranking/ordering of objects Typing: enforcement of the class Concurrency: distinguishes active object Persistence: transcends time and/or space

Abstraction

Encapsulation

Modularity

Hierarchy: “part-of” relationship

Hierarchy: “is-a” relationship

Typing

Concurrency

Persistence

Class Design Essential guidelines Instance variables are declared as private Public methods are provided to access and manipulate instance variables Accessors Mutators

Mutators Do not provide set methods for every instance variable Bad examples setBalance for a BankAccount class setDate, setMonth, and setYear for Date class Day deadline = new Day(2001, 1, 31); dealine.setMonth(2);//change day to 3/3 deadline.setDate(1);//final result is 3/1

Accessors Accessors may also break encapsulation giving out a reference to a mutable instance variable Solution – return a clone of the instance variable class Employee { private Date hireDate; //… public Date getHireDate() { //error //return hireDate; //the right way return (Date) hireDate.clone(); } }

What May Go Wrong? The mutable object may be modified by a client Sample code Employee bob = ...; Date d = bob.getHireDate(); d.setTime(t);

Separation of Getters & Setters It’s desirable to have Accessors will not change the state of an object nextToken for the StringTokenizer class Mutators will not return a data field removeFirst in the MessageQueue class In reality, you should consider your method design in terms of Completeness Convenience

Side Effects In a method invocation fraction1.add(fraction2); One expect the value in fraction1 be changed If the value in fraction2 changed, it is a side effect Changing a public static field is also a side effect Using System.out.println is not a good solution Define and throw specific exceptions instead

Programming by Contract Contract between client and server objects Formal definitions in terms of Precondition a condition must be fulfilled before the method may be called failure in doing so may cause program to break allow a client to check whether the condition is satisfied Postcondition A condition that holds after the method has completed Invariants A condition that is fulfilled by all objects after completion a method/constructor

Related Debugging Mechanisms Assertion A condition a programmer expects to be true Available since Java version 1.4 Program will terminate if an assertion yields false Exception The exceptions as declared in the throws clause should be deemed as a part of the contract Typical Java style, especially good for something the client have no control