The Principles of Abstraction Encapsulation Information-Hiding and Modularity
The Principles of Abstraction Encapsulation Information-Hiding and Modularity
• Instructor: Prof. Michael Chang
• About this course
– Objectoriented programming
– C#
– Design Patterns
– Software Methodologies
• Reference text
– Design patterns by Gamma et al (Addison Wesley)
• The Bible in OO, must read it if you are interested in OO.
Examples written in C++, but not hard to follow
– www.wikipedia.org
Department of Information Engineering 1
• 4 Assignment –10%
• 1 Project – 30%
• Final Exam (open notes, one A4 paper) – 60%
• Homepage: http://course.ie.cuhk.edu.hk/~ieg3080
• How to download Visual Studio .NET
– Get the form from
www.ie.cuhk.edu.hk/fileadmin/download/student/document/msdnaa.pdf
– Ignore the CD part, don’t need my signature, sign it and
return it to tutors
• Please sign the ‘No plagiarism’ declaration form and attach
it together with your assignment/project
• The form can be downloaded from section 9 in above site
How to build
largescale software
by a large team of people
quickly
and
inexpensively?
• Management problems
– How to manage software project
– Software methodology
• How to manage a team
– People has different skills
– Turnover
– Communicate in a large team
– Quality of the software
• How to manage the customers
– How to collect the requirements
– Are we building the system that the customers want?
Department of Information Engineering 8
Management solution
• Software methodologies
– Best practices in software development
• Traditional methods
• Rational Unified Process
• Extreme Programming (XP)
• CASE tools
– UML schematic diagram for software
– NUnit – for regression testing of software
• Solution
– Reusable software
• Key to reusable software
– Many solutions, but the most important concept is
the interface
– In particular, the separation of interface with the
implementation
Department of Information Engineering 10
Levels of reuse
• Source code reuse
– But vendors don’t give the source code away
• Binary code reuse
– But how to modify existing binary code?
• Interface reuse
– Key to flexible software
• Design reuse
– Design patterns, solutions to common programming
problems
• Divide a complex system into subsystems, and a sub
system into objects
– Each component can be developed independently
from each other
• How to divide a system into parts
– Key issue the interface between parts
Head Body
Front Read
Leg Leg
• Is software easy to change?
– Look easy, but in fact difficult !!
• Why?
– Too many dependencies between different modules
– 牽一發而 動全 身
• Related concepts
– Loose coupling
– Modularity
– Encapsulation
– Information hiding
USB Interface
Camera PC
Phone Disk
New USB Interface
PC
Camera
Phone Disk
• Implementation is the real stuff (the code)
• Interface is MORE IMPORTANT than implementation
– Why?
– Interface, once fixed, is hard to change
• e.g. USB
– Implementation can be easily changed
• e.g. mobile phone, camera, printer
• Each object has its own localized functions and data
– Object = function + data
• Why do we want localized functions and data?
– So that we can change a component easily
New
PC
Camera (data)
(data)
New
PC
Camera (data)
(data)
Implementation Interface (USB)
• Abstract concept, not concrete product
• Interface, not implementation
Interface implementation
.h file .c file
Rest of the system .h file
Can only see the
header file, so that .c file
the implementation
can be changed
freely
Rest of the system .h file
Can only see the
header file, so that
New .c file
the code can be
changed freely
Rest of the system Interface class
Can only see the base
class (interface), so
Implementation
that the subclass
(implementation) can
be changed freely
• If class B inherits class A, then class B has everything in A
– Class = interface + implementation
– An easy way to reuse other people’s interface and
implementation
• UML notation A
B
• Interface inheritance is much more important than
implementation inheritance
– The main purpose of the base class is to provide an
interface, so that the subclass (implementation) can be
changed freely in future
Department of Information Engineering 28
Reusable software
• Inheritance reuse
– By inheritance
• Implementation reuse
– By delegation
• Reuse other people’s code by using other people’s
object to do the work for you
• Inheritance and delegation are the two key concepts in OO
• This concept is wrong
– It confuses the concept of interface inheritance with
implementation inheritance
• Implementation can be reused by delegation rather than by
inheritance
– Just creates an object and use it (like getting a code
library and use it)
Department of Information Engineering 30
Inheritance
• Example
– Interface
• USB specification (just a piece of paper, totally
abstract)
– Implementation
• MP3 player
• Digital camera
• Mobile phone
Rest of the
System
Interface USB
• Modularity, encapsulation, abstraction, and loose coupling
are boiled down to one important principle in good
engineering design
– The Interface
• Internet
– Interfaces IP
• Airplane, plumbing, car, . . .
• 2nd language (English) is more important than mother
tongue for business communication
• Essential ideas are the same
– Divide and conquer
– With good interface, one can change a part without
affecting the rest of the system
Separation
of
Interface
and
Implementation
Department of Information Engineering 37
Interface and implementation
Interface versus implementation
Disk drive
CDROM
USB bus Printer
Digital camera
Future products . . .
Polymorphism – many different forms
• The first principle in OOP (in Gamma’s Design Pattern)
programming to an interface, not an implementation
• The differences
– On granularity
• C –at subsystem level (coarser scale)
• C++, C# at object level (finer scale)
– C++ is complicated
– C# (and Java) is simpler (no pointer)
• Top designers design the interface, they don’t code !!
– Implementation is done by programmers working
independently all over the world
• OO is about an abstract style of programming, focus on
the interface
Twins Morning 娘