CSCI-383 Lecture 3-4: Abstraction

43
Lecture 3-4: Abstraction CSCI-383 Object-Oriented Programming & Design Ji Ruan 2009/09/18 and 09/21

Transcript of CSCI-383 Lecture 3-4: Abstraction

Page 1: CSCI-383 Lecture 3-4: Abstraction

Lecture 3-4: Abstraction

CSCI-383Object-Oriented Programming & Design

Ji Ruan2009/09/18 and 09/21

Page 2: CSCI-383 Lecture 3-4: Abstraction

Overview

• Abstraction and Information Hiding• Levels of Abstraction• Forms of Abstraction• A Short History of Abstraction

Page 3: CSCI-383 Lecture 3-4: Abstraction

Abstraction

• What is Abstraction?o Definition: Abstraction is the purposeful suppression, or

hiding, of some details of a process or artifact, in order to bring out more clearly other aspects, details, or structure.

o In other word, the elimination of the irrelevant and the amplification of the essential

• Why?o It’s one of the most important techniques to create,

understand and manage complex systems

Page 4: CSCI-383 Lecture 3-4: Abstraction

An Example of Abstraction - Map

Think of maps, and the different levels of details:• A map of the world, contains mountain ranges, large political

boundaries• A map of a continent, contains all political boundaries, large

cities• A map of a country, contains more cities, major roads• A map of a large city, roads, major structures• A map of a portion of a city, buildings, occupants

Each level contains information appropriate to the level of abstraction.• A demo with: Google Maps (maps.google.com)

Page 5: CSCI-383 Lecture 3-4: Abstraction

Information Hiding

• Information hiding is the purposeful omission of details in the development of an abstract representation.

• Information hiding is what allows abstraction to control complexity.

Page 6: CSCI-383 Lecture 3-4: Abstraction

Example of Real-world Abstraction - Car

• For drivers: Need to knowIgnition Steering wheelShifting gearCan ignore (hidden)the particular engine in this car; the way fuel is pumped to the engine where a spark ignites it, it explodes pushing down a piston and driving a crankshaft.

• For mechanics:Need to knowbatteries and enginesCan ignore (hidden)inside the battery there's a complex chemical reaction going on.

• For the battery designers: Need to knowWhat is inside the batteryHow to make it durableCan ignore (hidden)How to use this battery, e.g. the electronics going into the car's radio.

Page 7: CSCI-383 Lecture 3-4: Abstraction

Five Levels of Abstraction

Page 8: CSCI-383 Lecture 3-4: Abstraction

Levels of Abstraction in OO Programs

• At the highest level (level 1) of abstraction we view a program as a community of interacting objects.

• Important characteristics here are the lines of communication between the various agents.

Page 9: CSCI-383 Lecture 3-4: Abstraction

Abstraction in OO Programs -- Packages and Name spaces

The next level (level 2) of abstraction is found in some (but not all) OO languages. A package, Unit or Name Space allows a programmer to surround a collection of objects (a small community in itself) with a layer, and control visibility from outside the module.

Page 10: CSCI-383 Lecture 3-4: Abstraction

Abstraction in OO Languages -- Clients and Servers

The next level of abstraction considers the relationship between two individual objects. Typically one is providing a service, and the other is using the service.

Page 11: CSCI-383 Lecture 3-4: Abstraction

Abstraction in OO languages -- Description of Services

We can examine just the person providing a service, independent of the client. We define the nature of the services that are offered, but not how those services are realized. (like billboard advertisements)

Page 12: CSCI-383 Lecture 3-4: Abstraction

Levels of Abstraction in OO -- Interfaces

Interfaces (level 3) are one way to describe services at this level of abstraction.

interface Stack { public void push (Object val);public Object top () throws EmptyStackException; public void pop () throws EmptyStackException;}

Page 13: CSCI-383 Lecture 3-4: Abstraction

Levels of Abstraction -- An Implementation

Next (level 4) we look at the services provided, but from the implementation side:

public class LinkedList implements Stack ... {public void pop () throws EmptyStackException { ... }...}

Concern here is with the high level approach to providing the designated service.

Page 14: CSCI-383 Lecture 3-4: Abstraction

Levels of Abstraction -- A Method in Isolation

Finally (level 5), we consider the implementation of each method in isolation.

public class LinkedList implements Stack ... {...public void pop () throws EmptyStackException {if (isEmpty())throw new EmptyStackException();removeFirst(); // delete first element of list}...}

Every level is important, and often you move quickly back and forth between levels.

Page 15: CSCI-383 Lecture 3-4: Abstraction

Summary of abstraction levels

• Level 1, community of interacting objects• Level 2, a package or Name Space to surround a collection of

objects • Level 3, interfaces to describe services (the client side)• Level 4, concrete implementation of the abstract behavior (the

server side)• Level 5, every single method

Page 16: CSCI-383 Lecture 3-4: Abstraction

Finding the Right Level of Abstraction

• A critical problem in early stages of development is to determine what details are appropriate at each level of abstraction, and (often more importantly) what details should be omitted.

• One does not want to ignore or throw away important information

• But one does not want to manage too much information, or have the amount of information hide critical details.

Page 17: CSCI-383 Lecture 3-4: Abstraction

Forms of Abstraction

Page 18: CSCI-383 Lecture 3-4: Abstraction

Forms of Abstraction

• Specialization (Is-A)o Car :: Wheeled vehicle :: Means of transportation

• Division into parts (Has-A)o Car { engine; transmission; body; wheels}

• Multiple viewso CarDriverView { Ignite(); SteerWheel(); ShiftGear() }o CarMechanicsView { CheckBattery(); CheckWheels();

CheckEngine() }

Page 19: CSCI-383 Lecture 3-4: Abstraction

Forms of Abstraction

 

Page 20: CSCI-383 Lecture 3-4: Abstraction

Is-a Abstraction

• Is-a abstraction takes a complex system, and views it as an instance of a more general abstraction.

• Characterized by sentences that have the words "is-a'' o A car is a wheeled vehicle, which is-a means of

transportation o A bicycle is-a wheeled vehicle o A pack horse is-a means of transportation

• Allows us to categorize artifacts and information and make it applicable to many different situations.

Page 21: CSCI-383 Lecture 3-4: Abstraction

Has-a Abstraction

• Division into parts takes a complex system, and divides into into component parts, which can then be considered in isolation.

• Characterized by sentences that have the words "has-a'' o A car has-a engine, and has-a transmission o A bicycle has-a wheel o A window has-a menu bar

• Allows us to drop down a level of complexity when we consider the component in isolation.

Page 22: CSCI-383 Lecture 3-4: Abstraction

Encapsulation and Interchangeability

• An important aspect of division into parts is to clearly characterize the connection, or interface, between two components.

• Allows for considering multiple different implementations of the same interface.

• For example, a car can have several different types of engine and one transmission.

Page 23: CSCI-383 Lecture 3-4: Abstraction

The Service View

• Another way to think of an interface is as a way of describing the service that an object provides.

• The interface is a contract for the service--if the interface is upheld, then the service will be provided as described.

Page 24: CSCI-383 Lecture 3-4: Abstraction

• Square and Triangle inherited method set_colour()

• Square and Triangle implement different method draw()

Inheritance and Polymorphism

Page 25: CSCI-383 Lecture 3-4: Abstraction

Other Types of Abstraction: Composition

• While is-a and has-a are two important types of abstraction, there are others.

• Composition is one example; a form of has-a; characterized by the following o Primitive forms o Rules for combining old forms to create new forms o The idea that new forms can also be subject to further

combination

Page 26: CSCI-383 Lecture 3-4: Abstraction

Basic alphabet {a, b, c}

• Rule 1: any single element of the alphabet is a regular expression.o a is a regular expression

• Rule 2: composition of two regular expressions is a regular expression.o ab is a regular expression

• Rule 3: alternation (|) of two regular expressions is a regular expression.o aba|abc|abd is a regular expression

• Rule 4: use parentheses for grouping, the result is a regular expression.o ab(a|c|d), meaning aba|abc|abd, is a regular expression

• Rule 5: use * symbol represent “zero or more repetitions”, the result is a regular expression.o (((a|b)*c)|dd)a is a regular expression.

Regular expressions

Page 27: CSCI-383 Lecture 3-4: Abstraction

Patterns

• Patterns are another attempt to document and reuse abstractions.

• Patterns are description of proven and useful relationships between objects; which can help guide the solution of new problems.

Page 28: CSCI-383 Lecture 3-4: Abstraction

Patterns: example • The Great Firewall of China, China's Internet Censorship System,

blocks many sites, e.g Youtube, Wikipedia, Twitter etc.)• The GFW even blocks my personal blog: jiblog.jiruan.net. Here is a

screenshot when I tried to visit my blog in a netbar in China (2007).• “ 找不到服务器,无法显示网页”

• “The Connection Has Been Reset”(Article | wikipeida)

 

Page 29: CSCI-383 Lecture 3-4: Abstraction

Patterns: example • Proxy Model 

   

• An application: crack the GFW using proxy

Page 30: CSCI-383 Lecture 3-4: Abstraction

A Short History of Abstraction Mechanisms

Page 31: CSCI-383 Lecture 3-4: Abstraction

An Overview of History

Another way to better understand OOP is to put it in context with the history of abstraction in computer science.• Assembly languages• Procedures• Modules• Abstract Data Type (ADT)• The Service View• Objects• The future....

Page 32: CSCI-383 Lecture 3-4: Abstraction

Assembly Languages

Assembly languages and linkers were perhaps the first tools used to abstract features of the bare machine.• Addresses could be represented symbolically, not as a

number.• Symbolic names for operations.• Linking of names and locations performed automatically

Page 33: CSCI-383 Lecture 3-4: Abstraction

Procedures and Functions

• Libraries of procedures and functions (such as mathematical or input/output libraries) provided the first hints of information hiding.

• They permit the programmer to think about operations in high level terms, concentrating on what is being done, not how it is being performed.

• But they are not an entirely effective mechanism of information hiding.

Page 34: CSCI-383 Lecture 3-4: Abstraction

Failure of Procedures in Information Hiding

A Simple Stacks:    int datastack[100];

int datatop = 0;void init() // initialize the stack{ datatop = 0;}void push(int val) // push a value on to the stack{ if (datatop < 100)datastack [datatop++] = val;}int top() // get the top of the stack{ if (datatop > 0)return datastack [datatop - 1];return 0;}int pop() // pop element from the stack{ if (datatop > 0)return datastack [--datatop];return 0;}

Where can you hide the implementation?

Page 35: CSCI-383 Lecture 3-4: Abstraction

Modules

• Modules basically provide collections of procedures and data with import and export statements

• Solves the problem of encapsulation -- but what if your programming task requires two or more stacks? (No instantiation)

Page 36: CSCI-383 Lecture 3-4: Abstraction

Parnas's Principles

David Parnas described two principles for the proper use of modules: • One must provide the intended user of a module with all the

information needed to use the module correctly, and with nothing more.

• One must provide the implementer of a module with all the information needed to complete the module, and nothing more.

Page 37: CSCI-383 Lecture 3-4: Abstraction

Abstract Data Types (ADTs)

• An Abstract Data Type is a programmer-defined data type that can be manipulated in a manner similar to system-provided data types o Must have the ability to instantiate many different copies of the

data type. o Data type can be manipulated using provided operations, without

knowledge of internal representation. • But ADTs were important not because they were data

structures, but because they provided an easily characterized service to the rest of an application.

Page 38: CSCI-383 Lecture 3-4: Abstraction

Summery

Looking at the history, we can separate it into three periods of time:

TheFunction CenteredView

Assembly languageFunctions and Procedures

Major concern is characterizing the functionality of an application

TheData CenteredView

ModulesAbstract Data Types

Major concern is characterizing the data types used in an application

TheService CenteredView

Object-OrientedProgramming

Major concern is characterizing the services provided by objects in the application

Page 39: CSCI-383 Lecture 3-4: Abstraction

Objects - ADT's with Message Passing

Characterists of Objects• Encapsulation -- similar to modules • Instantiation -- similar to ADT's • Messages -- dynamic binding of procedure names to behavior • Classes -- a way of organization that permits sharing and

reuse • Polymorphism -- A new form of software reuse using

dynamic binding

Page 40: CSCI-383 Lecture 3-4: Abstraction

What Does the Future Hold?

• Prediction is hard, particularly about the future.• However, one you have accepted the idea of an application

formed from interacting agents, there is no reason why those components must exist on the same computer (distributed computing) or be written in the same language (components).

• So some of the trends we see today in software are natural results of the OOP mind set.

• Web services, Cloud computing, Agent-Based Computing

Page 41: CSCI-383 Lecture 3-4: Abstraction

Basic Idea of Cloud ComputingCloud computing is a paradigm of computing in which dynamically scalable and often virtualized resources are provided as a service over the Internet. Users need not have knowledge of, expertise in, or control over the technology infrastructure in the "cloud" that supports them.

Page 42: CSCI-383 Lecture 3-4: Abstraction

• Textbook Chapter 2: Exercises 1,2,3.• Deadline: Wed, September 30, 2009.• Either by email or giving it to me directly in

the class.

Assignment 2:

Page 43: CSCI-383 Lecture 3-4: Abstraction

About these slides

These slides are created by Dr. Ji Ruan on top of the lecture notes by Prof. Wendy MacCaull and Dr. Hao Wang from their previous years' teaching on this course at StFX University. 

The core materials are based on book: An introduction to Object-Oriented Programming, 3rd edition, by Timothy Budd.

The slides are licensed under Creative Commons Attribution-Share Alike 3.0

You are free to Share and Remix this work, under the following conditions: • Attribution• Share Alike