Vous êtes sur la page 1sur 42

IEG3080    Software Engineering

• Instructor: Prof. Michael Chang 

• About this course
– Object­oriented 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

Department of Information Engineering 2


•No plagiarism
– Policy of zero tolerance
– Penalties include failing the course and 
receiving demerits.
– http://www.cuhk.edu.hk/policy/academichonesty/ 

• 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

Department of Information Engineering 3


Interface
&
Implementation
Department of Information Engineering 4
What is software engineering?

How to build 
large­scale software 
by a large team of people 
quickly 
and 
inexpensively?

Department of Information Engineering 5


Problems
• Software is expensive
• Delivery is always late
• Software is unreliable
• Software is difficult to maintain
– the code is written by other people
• Software is difficult to adapt to future change
• Difficult to manage a large team

Department of Information Engineering 6


Problems
• Technical problems
– To build complex software reliably, quickly, cheaply, 
easy to maintain, and can be changed flexibly in 
future 
– OO, design patterns, etc

• Management problems
– How to manage software project
– Software methodology

Department of Information Engineering 7


Management solution
• Effective communication

• 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

Department of Information Engineering 9


Technical solution
• How to build complex software reliably, quickly, 
cheaply, easy to maintain, and can be changed flexibly 
in future

• 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

Department of Information Engineering 11


Software is complex
• How to deal with complex system?
– Divide and conquer

• Divide a complex system into sub­systems, 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

Department of Information Engineering 12


疱丁 解牛

Department of Information Engineering 13


To divide a system into parts
• A decomposed cow

   
 Head            Body          

Front Read
Leg Leg

Department of Information Engineering 14


Is software easy to change?
• Good software will be used for many years
• Must be able to extend/modify the software in order to cater 
for future needs

• Is software easy to change?
– Look easy, but in fact difficult !!

• Why?
– Too many dependencies between different modules 
– 牽一發而 動全 身

Department of Information Engineering 15


Solution?
• Reduce dependency between parts
– Can change a component freely without affecting the rest 
of the system
– Interface is the key to flexible and extensible software

• Related concepts
– Loose coupling
– Modularity
– Encapsulation
– Information hiding

Department of Information Engineering 16


Interface – so that parts can be changed independently
• USB interface

   
USB Interface
 Camera            PC          

Phone Disk

Department of Information Engineering 17


To connect to a new camera

   
 New USB Interface
          PC          
 Camera 

Phone Disk

Department of Information Engineering 18


Interface and Implementation
• Interface is a specification, describing how components 
should be joint

• 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

Department of Information Engineering 19


What is an object?
• Object is a software component that you can reuse easily

• 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) 

Department of Information Engineering 20


Separation of interface and implementation
• Interface and implementation are two different things

   
 New
  PC   
 Camera       (data)       
(data) 

Implementation Interface (USB)

Department of Information Engineering 21


What is a USB interface?
• Just a specification on paper

• Abstract concept, not concrete product

• Interface, not implementation

Department of Information Engineering 22


What is the use of header file in C?

Interface                                implementation

.h file .c file

int foo(int x);   int foo(int x) {


. . .
  }

Department of Information Engineering 23


The reason of using header file
• Separation of interface and implementation 

Rest of the system .h file

Can only see the 
header file, so that  .c file
the implementation
can be changed 
freely  

Department of Information Engineering 24


The advantage of the separation
• .c file (the code) can be changed without affecting the 
rest of the system
• .h file (the header) cannot be changed!!

Rest of the system .h file

Can only see the 
header file, so that 
New .c file
the code can be 
changed freely  

Department of Information Engineering 25


The essential idea of OO
• Base class provides the interface (function declaration)
• The subclass provides the function implementation
– The subclass inherits the interface of the base class

Rest of the system Interface class

Can only see the base 
class (interface), so 
Implementation
that the subclass
(implementation) can 
be changed freely  

Department of Information Engineering 26


Inheritance
• Inheritance is a key concept in OOP. What does it mean?

• 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  

Department of Information Engineering 27


Interface inheritance versus implementation inheritance
• Class A (the base class) may have interface and 
implementation
 
• Class B (the sub­class) inherits both the interface and 
implementation in A
– Interface inheritance
– Implementation inheritance

• Interface inheritance is much more important than 
implementation inheritance
– The main purpose of the base class is to provide an 
interface, so that the sub­class (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

Department of Information Engineering 29


Common misconception about OO
• Wrong concept
– The main purpose of inheritance is to inherit the codes 
in the base class 

• 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
 

Department of Information Engineering 31


Inheritance and implementation

Rest of the
System

Interface USB

Implementation MP3 Digital Mobile


Camera Phone

Department of Information Engineering 32


Interface and implementation
• Interface promotes the following related design concepts
– Modularity
• Divide a system into modules
– Encapsulation
• Hide the complexity of module, expose only the 
interface
– Abstraction
• Intellectual simplification, e.g. USB is an abstraction 
of devices (existing or yet to be invented)
• Reduce the amount of learning
– Loose coupling
• change in one module won’t affect others

Department of Information Engineering 33


The key to LARGE SCALE DEVELOPMENT 
• Different parts are manufactured by different companies 
from different places in the world

• Modularity, encapsulation, abstraction, and loose coupling 
are boiled down to one important principle in good 
engineering design
– The Interface

Department of Information Engineering 34


Example of good interface
• World­wide web
– Network level interface – HTTP protocol
– Human level interface – HTML/XML

• Internet
– Interfaces ­  IP

• Airplane, plumbing, car, . . .

• 2nd language (English) is more important than mother 
tongue for business communication

Department of Information Engineering 35


The high input impedance / low output impedance 
principle in circuit design
• B has high input impedance and low output impedance, 
can change B without affecting the rest of the system

Box A  Box B  Box C 

• Essential ideas are the same
– Divide and conquer
– With good interface, one can change a part without 
affecting the rest of the system

Department of Information Engineering 36


The most important principle in (software) engineering

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

Department of Information Engineering 38


• USB is an abstraction of the hardware
– Design the PC system around the USB interface, not 
to a particular device

• The first principle in OOP (in Gamma’s Design Pattern)

programming to an interface, not an implementation

Department of Information Engineering 39


From C, C++ to Java/C#
• Basic principles are the same
– The separation of interface from implementation

• The differences
– On granularity
• C –at sub­system level (coarser scale)
• C++, C# ­ at object level (finer scale)
– C++ is complicated
– C# (and Java) is simpler (no pointer)

Department of Information Engineering 40


Abstract versus concrete
• Interface is abstract, implementation is concrete

• 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

Department of Information Engineering 41


Which one is a more successful design from OO point of 
view?

Twins Morning 娘

Department of Information Engineering 42

Vous aimerez peut-être aussi