The Principles of Abstraction Encapsulation Information-hiding and Modularity

Post on 10-Apr-2015

672 views 1 download

Transcript of The Principles of Abstraction Encapsulation Information-hiding and Modularity

1Department of Information Engineering

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

2Department of Information Engineering

• 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

3Department of Information Engineering

•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

4Department of Information Engineering

Interface&

Implementation

5Department of Information Engineering

What is software engineering?

How to build large-scale software

by a large team of people quickly

and inexpensively?

6Department of Information Engineering

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

7Department of Information Engineering

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

8Department of Information Engineering

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?

9Department of Information Engineering

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

10Department of Information Engineering

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

11Department of Information Engineering

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

12Department of Information Engineering

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

13Department of Information Engineering

疱丁解牛

14Department of Information Engineering

To divide a system into parts

• A decomposed cow

Head

Body

FrontLeg

ReadLeg

15Department of Information Engineering

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 – 牽一發而動全身

16Department of Information Engineering

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

17Department of Information Engineering

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

Camera

PC

Phone Disk

USB Interface

18Department of Information Engineering

To connect to a new camera

New Camera

PC

Phone Disk

USB Interface

19Department of Information Engineering

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

20Department of Information Engineering

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

Camera(data)

PC

(data)

21Department of Information Engineering

Separation of interface and implementation

• Interface and implementation are two different things

New Camera(data)

PC

(data)

Interface (USB)Implementation

22Department of Information Engineering

What is a USB interface?

• Just a specification on paper

• Abstract concept, not concrete product

• Interface, not implementation

23Department of Information Engineering

What is the use of header file in C?

Interface implementation

.h file

int foo(int x);

.c file

int foo(int x) { . . .}

24Department of Information Engineering

The reason of using header file

• Separation of interface and implementation

Rest of the system

Can only see the header file, so that the implementationcan be changed freely

.h file

.c file

25Department of Information Engineering

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

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

.h file

New .c file

26Department of Information Engineering

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 classRest of the system

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

Interface class

Implementation

27Department of Information Engineering

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

28Department of Information Engineering

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

29Department of Information Engineering

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

30Department of Information Engineering

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)

31Department of Information Engineering

Inheritance

• Example– Interface

•USB specification (just a piece of paper, totally abstract)

– Implementation•MP3 player•Digital camera•Mobile phone

32Department of Information Engineering

Inheritance and implementation

USB

MP3 DigitalCamera

MobilePhone

Rest of theSystem

Interface

Implementation

33Department of Information Engineering

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

34Department of Information Engineering

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

35Department of Information Engineering

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

36Department of Information Engineering

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

• Essential ideas are the same– Divide and conquer– With good interface, one can change a part

without affecting the rest of the system

Box A Box B Box C

37Department of Information Engineering

The most important principle in (software) engineering

Separation of

Interface and

Implementation

38Department of Information Engineering

Interface and implementation

Disk drive

CDROM

Printer

Digital camera

Future products . . .

USB bus

Interface versus implementation

Polymorphism – many different forms

39Department of Information Engineering

• 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

40Department of Information Engineering

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)

41Department of Information Engineering

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

42Department of Information Engineering

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

Twins Morning 娘