oop_2

Upload: pamela-lucinda

Post on 20-Jul-2015

40 views

Category:

Documents


0 download

TRANSCRIPT

OBJECT ORIENTED DESIGN

Object Orientation

object-orientation is introduced as a new programming concept which should help you in developing high quality software. Object-orientation is also introduced as a concept which makes developing of projects easier.

short survey of programming techniques

Unstructured programming, Procedural programming, Modular programming and Object-oriented programming.

Unstructured Programming

Usually, people start learning programming by writing small and simple programs consisting only of one main program. ``main program'' stands for a sequence of commands or statements which modify data which is global throughout the whole program

Procedural Programming

You are able to combine returning sequences of statements into one single place. A procedure call is used to invoke the procedure. After the sequence is processed, flow of control proceeds right after the position where the call was made

Procedural Programming (2)

With introducing parameters as well as procedures of procedures ( subprocedures) programs can now be written more structured and error free. For example, if a procedure is correct, every time it is used it produces correct results. Consequently, in cases of errors you can narrow your search to those places which are not proven to be correct.

Procedural Programming (3)

Now a program can be viewed as a sequence of procedure calls. The main program is responsible to pass data to the individual calls, the data is processed by the procedures and, once the program has finished, the resulting data is presented.

Modular Programming

With modular programming procedures of a common functionality are grouped together into separate modules. A program therefore no longer consists of only one single part. It is now devided into several smaller parts which interact through procedure calls and which form the whole program

Modular Programming (2)

Each module can have its own data. This allows each module to manage an internal state which is modified by calls to procedures of this module. However, there is only one state per module and each module exists at most once in the whole program.

OBJECT ORIENTED CONCEPTLearning objective :

Understand what we mean by a software object.

Understand how objects can communicate, using messages, to accomplish some task. Understand what happens when an object is no longer needed

Understand what we mean by a class.

Konsep Object Dalam paradigma berbasis object, object adalahrepresentasi sebuah entitas yang memiliki makna tertentu yang menjadi perhatian si pemandang object memiliki abstraksi tertentu bagi si pemandang Contoh: dalam sistem akademik di kampus, beberapa kandidat object adalah: mahasiswa, dosen, kelas, mata kuliah, kurikulum, praktikum, laboratorium, perpustakaan, KRS, KHS,

Object dapat menerima pesan (message), mengolahdata, dan mengirimkan pesan ke object lain membentuk interaksi antar object Object bersifat independen: tiap object dapat dipandang sebagai sebuah entitas mandiri yang memiliki peran atau tanggung jawab tertentu

OBJECTA thing, an entity, a noun, something you can pick up or kick, anything you can imagine, that has its own identity

Attributes

All objects have attributes.Manufacturer Model Number Color Price

Behaviour

All objects have behaviourA Car can move from a place to another

Also said that Object :

An abstraction of something in a problem domain, reflecting the capabilities of the system to keep information about it, interact with it or both.Object A person Identity Hussain Perves Behaviour Speak, walk, read States Studying, resting qualified Pressed, dirty, worn

A shirt

My favourite Shrink, stain, rip button-down white denims Sale no 0015, 15/06/02 This bottle of ketchup

A sale

Earn loyalty points Invoiced cancelled Spill in transit Unsold, opened, empty

A bottle of ketchup

Konsep-Konsep dalam OOP: Kelas (Class) Mendefinisikan karakteristik abstrak darisebuah entitas Property (statis) fields, atribut Behaviour (dinamika) fitur, methods Contoh: abstraksi sepeda memiliki property punya roda, punya rantai, dsb., dan behaviour bisa berjalan, bisa berbelok, dsb.

Sebagai abstraksi dari sebuah entitas, definisikelas harus mencerminkan karakteristik sebenarnya dari entitas tsb.

Kelas Kelas merupakan cetakan (template) untukinstance (wujud nyata) entitas-entitas yang direpresentasikannya Sebuah kelas dapat melahirkan lebih dari satu instance

satu template banyak instance (wujud nyata)

ObjectSebuah instance (perwujudan nyata) dari suatu kelas tertentu

public class SegiEmpat { int panjang; int lebar; string warna; public SegiEmpat(string w, int p, int l) { panjang = p; lebar = l; warna = w; } public setWarna (string w) { warna = w; } } SegiEmpat sPink = new SegiEmpat(pink, 20,10); SegiEmpat sPutih = new SegiEmpat(putih, 15,10); SegiEmpat sOrange = new SegiEmpat(orange, 30,15);

Definisi Kelas dan Objectpublic class SegiEmpat { int panjang; int lebar; string warna; public SegiEmpat(string w, int p, int l) { panjang = p; lebar = l; warna = w; } public setWarna (string w) { warna = w; } }

field, menunjukkan atribut/property constructor, untuk menciptakan object (instance) baru dengan property tertentu methods atau member functions, mendeskripsikan behaviour atau aktivitas yang bisa dijalankan

SegiEmpat sPink = new SegiEmpat(pink, 20,10);

memanggil constructor untuk membentuk object baru

Enkapsulasi Enkapsulasi: lokalisasi fitur-fitur sebuah object (fields danmethods) dalam definisi object tersebut Enkapsulasi menyembunyikan property dan behaviour object dari pihak luar (object yang lain) object lain melihat object ini sebagai black box saja Enkapsulasi memisahkan antara bagian publik (yang bisa dilihat oleh pihak luar (object lain) dan bagian privat (internal object itu sendiri) dengan tegas fitur ini memberi keleluasaan/independensi untuk bekerja dengan aspek internal tanpa harus bergantung pada aspek publik/eksternal berguna untuk menangani berbagai persoalan interoperabilitas

Enkapsulasipublic class SegiEmpat { int panjang; int lebar; string warna; public SegiEmpat(string w, int p, int l) { panjang = p; lebar = l; warna = w; } public setWarna (string w) { warna = w; } }

Pihak luar (object lain) melihat sPink sebagai sebuah kotak hitam yang tidak terlihat isinya, kecuali fields dan methods yang memang dideklarasikan secara publik

SegiEmpat sPink = new SegiEmpat(pink, 20,10);

sPinkObject lain tidak bisa melihat apa yang dilakukan sPink secara internal

setWarna(string w)

Inheritance (Pewarisan Sifat) Dalam dunia OO, beberapa object memiliki sifat/ciri yang miripdengan yang lain sebuah kelas object dapat didefinisikan dari kelas yang lain Kemiripan ini membentuk hubungan sifat yang bersifat hirarkis pesawat penyergap adalah sebuah pesawat terbang yang dilengkapi dengan persenjataan dan dapat terbang melebihi kecepatan suara pesawat penyergap memiliki ciri yang sama dengan pesawat terbang pesawat penyergap mewarisi sifat pesawat terbang pesawat penyergap memiliki ciri yang lebih khusus dibandingkan pesawat terbang pesawat penyergap adalah subclass dari pesawat terbang Dalam OOP, hubungan inheritance (pewarisan sifat) ini diimplementasikan melalui definisi kelas: sebuah kelas dapat didefinisikan dari kelas yang lain

Inheritancebentuk segitiga persegi panjang

poligon

ellips

bujur sangkar

lingkaran

Inheritance Dalam pohon hirarki inheritance, sebuah subclassmewujudkan abstraksi yang lebih spesifik dari superclassnya: subclass = superclass + (fitur-fitur baru) Subclass dapat menambahkan fields dan methods baru Subclass dapat mengambil alih (override) method milik superclass dan mengubah implementasi method tersebut

Manfaat inheritance: Pemanfaatan kembali (reuse) kelas Menyediakan model yang bersifat generik (abstract class); implementasi spesifiknya bisa dilakukan kemudian (mungkin oleh pemrogram yang berbeda)

Polymorphism Poly: banyak; morphism: bentuk sebuah fitur(generik) bisa diimplementasikan dengan berbagai cara Polymorphism diimplementasikan dengan mekanisme inheritance dan overridinghewan bersuara

anjing menggonggong

kucing mengeong

kuda meringkik

OO dan Interoperabilitas Konsep abstraksi dan enkapsulasi membuat batasyang tegas antara bagian yang bersifat publik dan bagian yang bersifat privat Abstraksi menjelaskan hal-hal yang terlihat dari luar (bersifat publik) Enkapsulasi menyembunyikan detil-detil yang tidak perlu terlihat dari luar (bersifat privat) Independensi antara aspek publik dan privat dieksploitasi untuk menangani isu interoperabilitas: implementasi layanan dapat dilakukan tanpa harus tergantung pada siapa yang mengaksesnya

Pemisahan Publik Privatpublik privat

pesan nasi goreng

Nasi goreng Mie goreng Mie bakso

memasak nasi goreng

Pemilik warung (server) dapat memasak nasi goreng (implementasi layanan) dengan berbagai cara tanpa harus tergantung pada siapa yang memesannya (client).

Interoperabilitas di Level AplikasiAplikasi #1 Aplikasi #2

Bahasa A Sistem operasi B Database C Network P Hardware M

Bahasa X Sistem operasi Y Database Z Network Q Hardware N

Solusi Berbasis OOPublik Privat

server(object implementation)

clientX interface Y Z implementasi

Object Orientation

A technique to modelling of a real world system in software based on object The object is the core concept An object is a software model of a real world entity or concept.

Software Object Oriented

Object in Real world migrate into code Objects => stand-alone module, with their own knowledge and behaviour Other words : with their own data and processes. Object is a bundle of variables and related methods

Model

A representation of a problem domain or a proposed solution that allow us to talk or reason about the real thing.

Classes

An object represents a particular instance of a class A class encapsulates characteristics common to a group of objects. In UML, classes are drawn as boxes on a class diagram. So that we can easily tell the difference between classes and objects, class names (on class diagrams) are not underlined, while object names (on object diagrams) are. Classes and objects are rarely mixed on the same diagram: it turns out that we can do most of our modeling in terms of classes, reserving object diagrams for illustration and verification purposes.

Depicting Object

A way to describe an object into a diagram UML object diagramAnObject attribute1 attribute2 operation1() operation2() operation3() Object Name

Object attributes (knowledge) Object Operations (behaviour)

Example : Coffee Machine Object

What operations a coffee machine needs?

Display drinks Select drinks Accept money Dispense drinks

What coffee machine needs to know in order to performs these operations?

Available drinks Drinks prices Drinks recipes

Coffee Machine ObjectaCoffeeMachine drinkPrices drinkRecipes availableDrinks displayDrinks() selectDrinks() dispenseDrinks() acceptMoney()

How OO Program works?

An object-oriented program works by creating objects, connecting them together and getting them to collaborate by sending messages to each other. But who gets the ball rolling? Who creates the first object and who sends the first message? To solve this problem, all objectoriented programs have an entry point. For example, Java expects to find an operation called main on an object that the user names when they launch the program. All the instructions in the main operation are executed, one after the other, and the program stops when main has finished.

object-oriented program in action

Every instruction in main can create an object, connect objects together or send a message to an object. When an object sends a message, the object that receives the message executes an operation. This operation can also create an object, connect objects together or send a message to an object. Thus, you should appreciate that this mechanism allows us to do anything we might want to do.