Lecture 10: Encapsulation
Download
Report
Transcript Lecture 10: Encapsulation
Something to discuss
Feedbacks on Midterm Exam
Final exam and term project
Final exam requires solid knowledge/skills in Java
Be more serious for the class!
Term project teams
Project topics
Understand lecture AT CLASS
Practice at home (do homework) and at lab sessions.
Self study for term project
Game
Algorithm animation
Project proposal Next Week !
XuanTung Hoang
1
Programming Fundamentals I
Java Programming
Spring 2007
Lecture No. 10
XuanTung Hoang
[email protected]
Encapsulation
1.
2.
3.
4.
5.
Overview
Encapsulation
Information Hiding
Composition
Singleton
What’s next after midterm … ?
We have learned almost every basics of Java
We can write java programs solve almost every
computing problem.
We will learn HOW TO ORGANIZE your
program in a clever way.
For understanding
For reusability
XuanTung Hoang
4
Specifically, what will we learn?
Writing Java program …
Creating class:
If you follow (or use) those rules/tricks/patterns:
Rules
Tricks
And patterns
You will write very good Java code!!!
You will be able to USE classes provided by others
effectively
Using class:
Java Collection Framework
Java I/O
Java GUI
Java Multithreading ….
XuanTung Hoang
5
Rule 1: Encapsulation
Group related information together
Group related information into a class
Use instance variables
Provide related actions to process those variables
XuanTung Hoang
6
Rule 1.1: Information Hiding
1. Don’t allow instance variables to be changed arbitrarily
2. Only expose necessary methods
Instance variable should be private
Standardizing:
the way to change instance variables (with set methods)
the way to represent instance variables
with get methods
with predicate methods
Expose necessary methods with public modifier
XuanTung Hoang
7
Trick 1.2: Composition
You can combine classes to define another class
Bottom up design !
Example:
Define six classes
Input, Output, Memory, CPU, ALU, Storage
Make Computer class that contains the above 6 classes
XuanTung Hoang
8
Case Study: Time Class (1)
private instance
variables
Validate parameter
values before setting
instance variables
format strings, check
JavaAPI manual for
details
XuanTung Hoang
9
Case Study: Time Class (2)
Create a Time1 object
Call toUniversalString
method
Call toString method
Call setTime method
Call setTime method
with invalid values
XuanTung Hoang
10
Notes from the Case Study …
hour, minute, second are private. They
cannot be accessed/modified from other classes
Access/modify via set and get methods (e.g. setTime method)
XuanTung Hoang
11
Making hour, minute, second public is allowed, but it isn’t a
good idea …
Hour, minute, second should store valid values. If they can be
set freely, their values are very likely
invalid.
If hour, minute, second are private with appropriate set metho
ds we can make sure that their values are always valid
appropriate set methods: Integrate validity checking in set
method
Other classes (clients) just call the set method without
worries about invalidity.
Complex detailed implementation
related to private fields are hidden
(information hiding)?
XuanTung Hoang
12
set, get, and predicate methods
Data/information hiding is a property of class instance
Typical classes has set, get, and predicate methods
set methods (mutators): validate and set the new values to instance
variables
get methods (accessors): obtain values of instance variables and control
the format of the returned values
E.g.: toUniversalString() can be considered as an
get method
Predicate methods: test whether a certain condition on the object is true
or false
E.g.: we can add to class Time1 method is21Century()
that tests whether the date is in the 21st century or not.
Great feature of OOP
XuanTung Hoang
13
Data abstraction and ADTs (1)
Thanks to information hiding, clients care only
what functionalities a class offers and don’t
concern with how those functionalities are
implemented. This concept is called data
abstraction
Data is not just the information like in common
sense, data is the information and operations that
can be performed on the information Abstract
Data Types (ADTs)
ADTs = Data representation + Operations
In ADTs, data representation can be hidden
XuanTung Hoang
14
Data abstraction and ADTs (2)
ADT Is a term that can refer to any type in current
programming languages
Java:
ADTs can be primitive types. E.g.: int (data representa-tion is int
eger number; operations are arithmetic operations)
ADTs can be classes. E.g. Time1 class, Employee class
Read section 8.15, text book
XuanTung Hoang
15
Pattern 1.3: Singleton
Singleton = Universal public Instance
In an application, if you have ONLY ONE instance
of a class, use Singleton pattern.
XuanTung Hoang
16
Singleton pattern
In a singleton class:
1 private static instance variable of singleton class
(sInstance)
Initialize it to null
1 public static method to:
create object of Singleton class ONLY ONE
Guarantee ONLY ONE object creation by checking sInstance
and return the singleton instance
XuanTung Hoang
17
Singleton Pattern
XuanTung Hoang
18
Examples of Singleton
Universal Random number generator
Universal Sequence number generator
In CardGame project
XuanTung Hoang
19