Encapsulation and Data Abstraction Testing Applications in Smalltalk.

Slides:



Advertisements
Similar presentations
Chapter 3: Modularization
Advertisements

Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
Software Engineering and Design Principles Chapter 1.
Chapter 6: Using Design Patterns
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz Summer Semester 2006.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
COMS S1007 Object-Oriented Programming and Design in Java August 7, 2007.
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
13-Jul-15 Refactoring II. Books Design Patterns is the classic book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Basically a catalog.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
UML and Object Oriented Concepts
Abstract data types What does ‘ abstract ’ mean? From Latin: to ‘ pull out ’— the essentials –To defer or hide the details –Abstraction emphasizes essentials.
Ranga Rodrigo. Class is central to object oriented programming.
CENTURY 21 ACCOUNTING © 2009 South-Western, Cengage Learning LESSON 12-1 Preparing Payroll Time Cards.
CENTURY 21 ACCOUNTING © 2009 South-Western, Cengage Learning LESSON 12-1 Preparing Payroll Time Cards.
Preparing Payroll Records Preparing Payroll Time Cards Salary – money paid for employee services Pay period – period covered by a salary payment.
1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang.
Unit 03: Financial Literacy Vocabulary. Available Balance The amount available in an account for a person, business, or organization to spend. How much.
Why Analysis Process Refer to earlier chapters Models what the system will do makes it easier for understanding no environment considered (hence, system.
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Design-Making Projects Work (Chapter7) n Large Projects u Design often distinct from analysis or coding u Project takes weeks, months or years to create.
Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Week 12 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns 1.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
Refactoring Improving the structure of existing code Refactoring1.
CSE 219 Computer Science III Program Design Principles.
Chapter 25 Formal Methods Formal methods Specify program using math Develop program using math Prove program matches specification using.
Relational Databases and Transaction Design Lecture 27.
1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling.
LESSON 12-1 Preparing Payroll Time Cards
CENTURY 21 ACCOUNTING © 2009 South-Western, Cengage Learning LESSON 12-2 Determining Payroll Tax Withholding  Payroll taxes  Employee’s withholding allowance.
Object-oriented Programming and Design 1 Polymorphism Poly => many Morph => shape Variables take on many shapes, or many classes of objects.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Refactoring1 Improving the structure of existing code.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Object Oriented Software Development
Object-oriented programming and design 1 Object Identity = vs. == copying objects Value Object ValueWithHistory.
ITEC 3220A Using and Designing Database Systems Instructor: Prof Z. Yang Course Website: 3220a.htm
Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
CENTURY 21 ACCOUNTING © 2009 South-Western, Cengage Learning LESSON 12-1 Preparing Payroll Time Cards  Paying employees  Analyzing a payroll time card.
JUnit Don Braffitt Updated: 10-Jun-2011.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Object-Oriented Principles Applications to Programming.
CS 350 – Software Design Expanding Our Horizons – Chapter 8 The traditional view of objects is that they are data with methods. Sometimes objects could.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Refactoring1 Improving the structure of existing code.
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
CENTURY 21 ACCOUNTING © 2009 South-Western, Cengage Learning LESSON 12-3 Preparing Payroll Records  Payroll register  Employee earnings records.
CSCE 240 – Intro to Software Engineering Lecture 3.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
XuanTung Hoang 1 Something to discuss Feedbacks on Midterm Exam Final exam and term project  Final exam requires solid knowledge/skills in Java  Be more.
Chapter 5:Design Patterns
Introduction to Testing, SUnit and Error Handling
Slides by Steve Armstrong LeTourneau University Longview, TX
Improving the structure of existing code
ITEC 3220A Using and Designing Database Systems
Interfaces.
Applying Use Cases (Chapters 25,26)
Applying Use Cases (Chapters 25,26)
Refactoring.
Presentation transcript:

Encapsulation and Data Abstraction Testing Applications in Smalltalk

Object-oriented Programming and Design 2 Encapsulation and Data Abstraction l An object should hide design decisions from its users l what is stored / what is computed l classes used l Can’t hide everything l Interface of object l Interface of parameters of object

Object-oriented Programming and Design 3 Encapsulation l Class contains variables and all the code that accesses the variables. Code should go with data. l Overuse of accessing methods is a sign of poor encapsulation l Code that uses a lot of accessing methods of an object should be moved to that object

Object-oriented Programming and Design 4 Encapsulation l (aPoint x squared + aPoint y squared) sqrt l aPoint r Some patterns (State, Strategy) break encapsulation. These are exceptions to the general rule that code should go with data.

Object-oriented Programming and Design 5 Inside/outside l Class describes the implementation of an object l Users care about specifications of object l Interface (Java, C++) l Pre and post-conditions (Eiffel) l Tests, examples, English description (all languages)

Types of arguments/results l Smalltalk does not check types at compile time l Programmer needs to know types l Method names tell: l Type of returned object l Role of arguments addEmployee: Object-oriented Programming and Design 6

Side effects l Names indicate whether method has side effect l Nouns, property – no side effect l Command – changes state of object add:, remove: Object-oriented Programming and Design 7

8 Tests l See if feature works l Make sure changes don’t break anything. l Document features. l Measure of completeness.

Object-oriented Programming and Design 9 Test-driven development l Write test for new feature first, then implement the feature. l Stop when tests work. l Use the simplest design that makes all the tests work l Refactor to eliminate duplication

Object-oriented Programming and Design 10 SUnit l The testing framework. l Each test is written in a subclass of TestCase. l Test is a method of the form “testXXX” l Each subclass is a group of related tests.

Object-oriented Programming and Design 11 TestRunner

Object-oriented Programming and Design 12 Important TestCase methods l assert: aBoolean l deny: aBoolean l should: aBlock l should: aBlock raise: anException l shouldnt: aBlock l shouldnt: aBlock raise: anException

Object-oriented Programming and Design 13 Test first design l How should feature work? l Give example l Write test case l Make test work l Write another test case l Make test work l...

Object-oriented Programming and Design 14 Problems with test first l What about testing GUIs? l What about testing real-time systems? l Tests show the presence of flaws, not their absence. l What about design?

Object-oriented Programming and Design 15 But you can’t test everything! l Testing increases your confidence. l Testing involves tradeoffs - better testing is more expensive. l Testing is never perfect; you can’t afford perfection. l Zero testing is always wrong; you must decide how much is enough.

Object-oriented Programming and Design 16 You can’t test everything l You can’t specify everything. l Automated tests with Sunit are easy and cheap most of the time. l Hard to test hardware. l Hard to test concurrent systems. l Most parts of a system are easy to test; write automated tests for them.

Object-oriented Programming and Design 17 Finding abstractions l Do one thing l Eliminate duplication l Keep rate of change similar l Decrease coupling, increase cohesion l Minimize interfaces l Minimize size of abstractions l Minimize number of abstractions

Object-oriented Programming and Design 18 Do one thing l Method should do what its name says l Class should be what its name says l Break complex classes/methods into simpler ones

Object-oriented Programming and Design 19 Eliminate duplication l (Ranks indexOf: rank) < (Ranks indexOf: argument rank) l self rankIndex < argument rankIndex

Object-oriented Programming and Design 20 Keep rate of change similar l Separate initial conditions from an algorithm’s temporary variables l Separate tax tables from employee data from time cards l Use “timer” to separate quickly changing information from slowly changing

Object-oriented Programming and Design 21 Decrease coupling, increase cohesion l Coupling is dependencc between components l Cohesion is dependence within a component

Object-oriented Programming and Design 22 Minimize interfaces l Use the smallest interface you can l Use Number instead of Float l Avoid embedding classes in names l add: instead of addNumber: l Don’t check the class of an object

Object-oriented Programming and Design 23 Mimize size of abstractions l Methods should be small l median size is 3 lines l 10 lines is starting to smell l Classes should be small l 7 variables is starting to smell l 40 methods is starting to smell

Object-oriented Programming and Design 24 Minimize number of abstractions l Can only learn 4-5 new things at a time. 3 is better l A class hierarchy 6-7 levels deep is hard to learn l Break large system into subsystems, so people only have to learn part of the system at a time

Object-oriented Programming and Design 25 A Payroll System PayrollSystem keeps track of employees. Employee knows salary, how much has been earned, how much has been payed, how much taxes have been withheld. Employee should be able to tell us about past transactions, not just current balances.

Object-oriented Programming and Design 26 Object Model for Payroll Employee EmployeeTransaction PayrollSystem post: date Paycheck Timecard SalaryChange salary pay, taxes hoursWorked vacation A class with a method #post: A payroll system has a set of employees. An employee has a set of transactions (and a transaction is for an employee) Timecard, paycheck and salarychange are kinds of employee transactions : postTo: abstract class abstract method * *

Object-oriented Programming and Design 27 Employee Object subclass: #Employee instanceVariableNames: 'name transactions salary earned paid withholding accruedVacation taxRule' classVariableNames: 'HeadTaxRate MarriedSeparateTaxRate MarriedTaxRate SingleTaxRate' poolDictionaries: '' category: 'CS598rej-Payroll HW2'

Object-oriented Programming and Design 28 Employee Comment (View/Comment in RB) I represent an employee in a payroll system. Thus, I keep track of the number of hours worked, the wages paid, the vacation accrued, the taxes withheld, and so on. My values change only when transactions are posted to me. Transactions are subclasses of EmployeeTransaction.

Object-oriented Programming and Design 29 (continued) Variables name transactions salary... taxRule

Object-oriented Programming and Design 30 Hiding classes l Employee knows class Timecard l Users of Employee do not l Employee creates Timecards for users

Object-oriented Programming and Design 31 Using Employee | ralph | ralph := Employee new name: 'Ralph Johnson'. ralph changeSalaryFor: (Date newDay: 5 year: 1996) to: 20. ralph postTimeCardFor: (Date newDay: 10 year: 1996) hoursWorked: 40 vacation: 0.

Object-oriented Programming and Design 32 Testing Employee EmployeeTest is a subclass of TestCase testOvertime | ralph | ralph := Employee new named: 'Ralph Johnson'. ralph changeSalaryFor: day1 to: 20. payroll addEmployee: ralph. self postTimeCards: aLittleOvertime to: ralph.. self assert: ralph earned =

Object-oriented Programming and Design 33 Initializing an Employee named: aString name := aString. transactions := OrderedCollection new. salary := taxRule := SingleTaxRate

Object-oriented Programming and Design 34 Complete Creation Method Employee has the class method named: aString ^self new named: aString

Object-oriented Programming and Design 35 Timecard Timecard is a subclass of EmployeeTransaction with instance variables 'hoursWorked' and 'hoursVacation '

Object-oriented Programming and Design 36 Initializing a Timecard date: aDate employee: anEmployee worked: hoursW vacation: hoursV date := aDate. employee := anEmployee. hoursWorked := hoursW. hoursVacation := hoursV

Object-oriented Programming and Design 37 Complete Creation Method Timecard has class method date: aDate employee: anEmployee worked: hoursW vacation: hoursV ^self new date: aDate employee: anEmployee worked: hoursW vacation: hoursV

Object-oriented Programming and Design 38 Using a Timecard Employee has method: postTimeCardFor: aDate hoursWorked: hoursW vacation: hoursV self postTransaction: (Timecard date: aDate employee: self worked: hoursW vacation: hoursV)

Object-oriented Programming and Design 39 Initialization Patterns There should be one or more methods that initialize object. They should be in protocol "initialize-release". There should be one or more class methods that create initialized objects. They should be in protocol "instance creation".

Object-oriented Programming and Design 40 Processing a Transaction In Employee: postTransaction: aTransaction aTransaction postTo: self. transactions add: aTransaction

Object-oriented Programming and Design 41 Processing a Timecard postTo: anEmployee | money overtime nonovertime | overtime := (hoursWorked - 40) max: 0. nonovertime := hoursWorked min: 40. money := (overtime * nonovertime) * anEmployee salary. anEmployee incrementEarned: money.

Object-oriented Programming and Design 42 Processing a Timecard salary ^salary incrementEarned: anAmount earned := earned + anAmount

Object-oriented Programming and Design 43 Employee Action Protocol All changes to an Employee should be recorded as transactions. postTimeCardFor: aDate hoursWorked: hoursW vacation: hoursV self postTransaction:

Object-oriented Programming and Design 44 Assignment l Look up assignment on speedy.cs.uiuc.edu:2500 l Create a page for yourself on the “people in the class” page l Choose a time to meet with me to discuss the assignment