Pemrograman Berorientasi Obyek (PBO)

Slides:



Advertisements
Presentasi serupa
Pemrograman Berorientasi Objek
Advertisements

Membuat class sendiri.
Other OOP Basic Viska Mutiawani, M.Sc. Konsep penting Method overloading Encapsulation this keyword final static.
Universitas Muhammadiyah Malang Fakultas Teknik Jurusan Teknik Informatika Semester Genap Nur hayatin, S.ST.
OBJECT ORIENTED PROGRAMMING in JAVA
Workshop SCS: Java Game Programming
BAB III – CLASS (1) DIKTAT PBO Oleh: Adam Mukharil B. S.Kom.
Java array.
Pemrograman Berorientasi Objek
di OBJECT ORIENTED PROGRAMMING [OOP] Oleh H. SUMIJAN, Ir, M.Sc
MINGGU 5 Java Programming (MKB614C)
Variabel, Method & Constructor Oleh : Nur Hayatin, S.ST Teknik Informatika – UMM 2011.
Pemrograman Berorientasi Objek
Class, Inheritance, Encapsulation & Interface
Pemrograman Berorientasi Obyek Oleh Tita Karlita
KELAS INNER, KELAS ABSTRACT, DAN INTERFACE
METHOD Object Oriented Programming with JAVA 2011/2012.
PELATIHAN JAVA FUNDAMENTAL
07 Advanced Class Features
MEMBUAT CLASS SENDIRI. Pada akhir pembahasan, peserta diharapkan mampu untuk :  Membuat class  Mendeklarasikan atribut dan method untuk class  Menggunakan.
Algoritma & Pemrograman 1
Struktur Kontrol Pemilihan
Struktur Data List Linear : Linked List (Single Linkedlist)
Pemrograman Berorientasi Obyek (PBO)
Class and Object Matakuliah : XXXX / Algoritma dan Metode Object Oriented Programming II Pertemuan : 2 Tahun : 2008 Versi : 1/0.
OOP Java Minggu 2b Dasar OOP. Class (1) Deklarasi class : [ *] class { [ *] } Cat : [] = optional, * = repeat 0-N.
Algoritma & Pemrograman 1
03 Elemen Dasar Bahasa Java
Java array.
Kelas A dapat memiliki referensi ke obyek dari kelas- kelas lain sebagai anggota. Kadang-kadang disebut sebagai hubungan “has-a”. Sebagai contoh, sebuah.
Pemrograman Berorientasi Obyek (PBO)
MEMBUAT KELAS SENDIRI Dewi Sartika, M.Kom.
Firman Asharudin | Pemrograman Lanjut
FONDASI PEMROGRAMAN & STRUKTUR DATA #5
Class & Object Disusun Oleh: Reza Budiawan Untuk:
Enkapsulasi.
Outline: Class Objek Konstruktor Destruktor Overloading dan Tipe value
JAVA ARRAY.
Pemrograman Berorientasi Object
Pemprograman Berorientasi Objek
Java array.
Java array.
MODIFIER JAVA.
Enkapsulasi, Accessor Method, Mutator Method
Algoritma & Pemrograman 1
Java array.
Constructor overloading
MEMBUAT CLASS SENDIRI 2.
Membuat Kelas.
Java array.
METODE SUSSI.
PBO Lanjutan Membuat Kelas.
Dasar Bahasa Java.
Matakuliah : M0074/PROGRAMMING II Tahun : 2005 Versi : 1/0
Membuat Class Sendiri.
Array, Class, Obyek, Method danConstructor.
Struktur.
OOP ENKAPSULASI SMKN 2 SINGOSARI Kelas XI RPL.
ISTILAH-ISTILAH PENTING PEMROGRAMAN BERBASIS OBYEK
Java array.
Pemrograman Berorientasi Obyek
Array, Class, Obyek, Method danConstructor.
Struktur Kontrol Pemilihan
Java array.
Pemrograman Berorientasi Objek
Pemrograman Berorientasi Objek
Struktur Kontrol Pemilihan Struktur kontrol pemilihan adalah pernyataan dari Java yang mengijinkan user untukmemilih dan mengeksekusi blok kode spesifik.
FONDASI PEMROGRAMAN & STRUKTUR DATA #5 - 1
FONDASI PEMROGRAMAN & STRUKTUR DATA #4 - 1
Bahasa Pemrograman (Pemrograman Visual)
Transcript presentasi:

Pemrograman Berorientasi Obyek (PBO) Pertemuan 5 : Prinsip Perancangan Kelas Disusun oleh : Silvester Dian Handy Permana, S.T., M.T.I. Fakultas Telematika, Universitas Trilogi

Tujuan Kuliah Memberikan wawasan tentang perancangan kelas yang baik serta menerjemahkannya dalam bahasa pemrograman

Outline Kuliah Tentang Kelas -- review Petunjuk Perancangan Kelas Accessor, Mutator dan Overloaded Methods Prinsip perancangan kelas

Definisi Kelas Elemen-elemen dasar dalam mendefinisikan kelas Field (variabel) : menyimpan data untuk setiap objek (implementasi dari atribut) Constructor : setup objek di awal Method : implementasi perilaku objek

Definisi Kelas class ClassName { Fields Constructors Methods }

Petunjuk pendefinisian kelas Deklarasikan variabel (data member) sebagai private untuk menjamin integritas kelas. Perwujudan enkapsulasi

Petunjuk pendefinisian kelas Deklarasikan fungsi dengan public untuk menyediakan akses kepada klien kelas. Fungsi public menentukan perilaku objek dari kelas.

Fungsi Accessor Fungsi untuk mendapatkan property dari suatu objek. Mengembalikan nilai atau value dari suatu atribut.

Fungsi Mutator Fungsi untuk mengubah property dari suatu objek. Mengubah nilai atau value dari sebuah atribut.

Petunjuk pendefinisian kelas Selalu definisikan suatu konstruktor dan inisialisasikan variabel instance secara lengkap dalam konstruktor sehingga objek akan dibuat dalam suatu kondisi yang valid

Petunjuk pendefinisian kelas Deklarasikan konstanta sebagai public apabila nilainya akan diakses oleh klien atau kelas lain. Jika hanya digunakan secara internal deklarasikan sebagai private

Beberapa Prinsip Perancangan Coupling Encapsulation Cohesion Code Duplication

Beberapa Prinsip Perancangan Coupling Keterikatan antar kelas Kelas berkomunikasi melalui antar muka yang telah didefinisikan dengan baik Yang baik : loose coupling Perubahan pada satu kelas tidak memiliki pengaruh yang besar pada kelas lain

Beberapa Prinsip Perancangan Encapsulation Digunakan untuk mengurangi efek coupling. menegaskan pemisahan antara what dan how dengan membuat atribut sebagai private dan menggunakan fungsi acessor untuk mengakses atribut tersebut.

Beberapa Prinsip Perancangan Cohesion Satu unit kode seharusnya selalu bertanggung jawab pada satu dan hanya satu tugas (task) cohesion dapat diterapkan pada kelas dan method

Beberapa Prinsip Perancangan Cohesion pada method Satu method hanya bertanggung jawab pada satu dan hanya satu well-defined task. Lihat contoh – cohesive-1.java & non-cohesive-1.java

Non-Cohesive /** * Main play routine. Loops until end of play. */ public void play() { System.out.println(); System.out.println("Welcome to The World of Zuul!"); System.out.println("Zuul is a new, incredibly boring adventure game."); System.out.println("Type 'help' if you need help."); System.out.println(currentRoom.getLongDescription()); // Enter the main command loop. Here we repeatedly read // commands and execute them until the game is over. boolean finished = false; while (! finished) { Command command = parser.getCommand(); finished = processCommand(command); } System.out.println("Thank you for playing. Good bye.");

Non-Cohesive Task utama fungsi play() /** * Main play routine. Loops until end of play. */ public void play() { System.out.println(); System.out.println("Welcome to The World of Zuul!"); System.out.println("Zuul is a new, incredibly boring adventure game."); System.out.println("Type 'help' if you need help."); System.out.println(currentRoom.getLongDescription()); // Enter the main command loop. Here we repeatedly read // commands and execute them until the game is over. boolean finished = false; while (! finished) { Command command = parser.getCommand(); finished = processCommand(command); } System.out.println("Thank you for playing. Good bye."); Task utama fungsi play()

Non-Cohesive Task tambahan fungsi play() Task utama fungsi play() /** * Main play routine. Loops until end of play. */ public void play() { System.out.println(); System.out.println("Welcome to The World of Zuul!"); System.out.println("Zuul is a new, incredibly boring adventure game."); System.out.println("Type 'help' if you need help."); System.out.println(currentRoom.getLongDescription()); // Enter the main command loop. Here we repeatedly read // commands and execute them until the game is over. boolean finished = false; while (! finished) { Command command = parser.getCommand(); finished = processCommand(command); } System.out.println("Thank you for playing. Good bye."); Task utama fungsi play()

Cohesive /** * Main play routine. Loops until end of play. */ public void play() { printWelcome(); // Enter the main command loop. Here we repeatedly read // commands and execute them until the game is over. boolean finished = false; while (! finished) { Command command = parser.getCommand(); finished = processCommand(command); } System.out.println("Thank you for playing. Good bye."); * Print out the opening message for the player. private void printWelcome() System.out.println(); System.out.println("Welcome to The World of Zuul!"); System.out.println("Zuul is a new, incredibly boring adventure game."); System.out.println("Type 'help' if you need help."); System.out.println(currentRoom.getLongDescription());

Beberapa Prinsip Perancangan Cohesion pada kelas Setiap kelas harus merepresentasikan entitas tunggal, well-defined dalam domain problem.

Beberapa Prinsip Perancangan Code Duplication Pertanda rancangan kelas yang tidak baik Problem : perubahan di satu bagian harus diikuti dengan bagian lain untuk menghindarkan inkonsistensi. Akibat : pemeliharaan menjadi mahal

Code Duplication private void goRoom(Command command) { if(!command.hasSecondWord()) { System.out.println("Go where?"); return; } String direction = command.getSecondWord(); Room nextRoom = null; if(direction.equals("north")) nextRoom = currentRoom.northExit; if(direction.equals("east")) nextRoom = currentRoom.eastExit; if(direction.equals("south")) nextRoom = currentRoom.southExit; if(direction.equals("west")) nextRoom = currentRoom.westExit; if (nextRoom == null) System.out.println("There is no door!"); else { currentRoom = nextRoom; System.out.println("You are " + currentRoom.getDescription()); System.out.print("Exits: "); if(currentRoom.northExit != null) System.out.print("north "); if(currentRoom.eastExit != null) System.out.print("east "); if(currentRoom.southExit != null) System.out.print("south "); if(currentRoom.westExit != null) System.out.print("west "); System.out.println(); public class Game { // ... some code omitted... /** * Print out the opening message for the player. */ private void printWelcome() System.out.println(); System.out.println("Welcome to the World of Zuul!"); System.out.println("Zuul is a new, incredibly boring adventure game."); System.out.println("Type 'help' if you need help."); System.out.println("You are " + currentRoom.getDescription()); System.out.print("Exits: "); if(currentRoom.northExit != null) System.out.print("north "); if(currentRoom.eastExit != null) System.out.print("east "); if(currentRoom.southExit != null) System.out.print("south "); if(currentRoom.westExit != null) System.out.print("west "); }

Code Duplication duplikasi private void goRoom(Command command) { if(!command.hasSecondWord()) { System.out.println("Go where?"); return; } String direction = command.getSecondWord(); Room nextRoom = null; if(direction.equals("north")) nextRoom = currentRoom.northExit; if(direction.equals("east")) nextRoom = currentRoom.eastExit; if(direction.equals("south")) nextRoom = currentRoom.southExit; if(direction.equals("west")) nextRoom = currentRoom.westExit; if (nextRoom == null) System.out.println("There is no door!"); else { currentRoom = nextRoom; System.out.println("You are " + currentRoom.getDescription()); System.out.print("Exits: "); if(currentRoom.northExit != null) System.out.print("north "); if(currentRoom.eastExit != null) System.out.print("east "); if(currentRoom.southExit != null) System.out.print("south "); if(currentRoom.westExit != null) System.out.print("west "); System.out.println(); public class Game { // ... some code omitted... /** * Print out the opening message for the player. */ private void printWelcome() System.out.println(); System.out.println("Welcome to the World of Zuul!"); System.out.println("Zuul is a new, incredibly boring adventure game."); System.out.println("Type 'help' if you need help."); System.out.println("You are " + currentRoom.getDescription()); System.out.print("Exits: "); if(currentRoom.northExit != null) System.out.print("north "); if(currentRoom.eastExit != null) System.out.print("east "); if(currentRoom.southExit != null) System.out.print("south "); if(currentRoom.westExit != null) System.out.print("west "); } duplikasi

Code Duplication SOLUSI ???

Code Duplication private void goRoom(Command command) { if(!command.hasSecondWord()) { System.out.println("Go where?"); return; } String direction = command.getSecondWord(); Room nextRoom = null; if(direction.equals("north")) nextRoom = currentRoom.northExit; if(direction.equals("east")) nextRoom = currentRoom.eastExit; if(direction.equals("south")) nextRoom = currentRoom.southExit; if(direction.equals("west")) nextRoom = currentRoom.westExit; if (nextRoom == null) System.out.println("There is no door!"); else { currentRoom = nextRoom; System.out.println("You are " + currentRoom.getDescription()); System.out.print("Exits: "); if(currentRoom.northExit != null) System.out.print("north "); if(currentRoom.eastExit != null) System.out.print("east "); if(currentRoom.southExit != null) System.out.print("south "); if(currentRoom.westExit != null) System.out.print("west "); System.out.println(); public class Game { // ... some code omitted... /** * Print out the opening message for the player. */ private void printWelcome() System.out.println(); System.out.println("Welcome to the World of Zuul!"); System.out.println("Zuul is a new, incredibly boring adventure game."); System.out.println("Type 'help' if you need help."); System.out.println("You are " + currentRoom.getDescription()); System.out.print("Exits: "); if(currentRoom.northExit != null) System.out.print("north "); if(currentRoom.eastExit != null) System.out.print("east "); if(currentRoom.southExit != null) System.out.print("south "); if(currentRoom.westExit != null) System.out.print("west "); }

Code Duplication private void goRoom(Command command) { if(!command.hasSecondWord()) { System.out.println("Go where?"); return; } String direction = command.getSecondWord(); Room nextRoom = null; if(direction.equals("north")) nextRoom = currentRoom.northExit; if(direction.equals("east")) nextRoom = currentRoom.eastExit; if(direction.equals("south")) nextRoom = currentRoom.southExit; if(direction.equals("west")) nextRoom = currentRoom.westExit; if (nextRoom == null) System.out.println("There is no door!"); else { currentRoom = nextRoom; public class Game { // ... some code omitted... /** * Print out the opening message for the player. */ private void printWelcome() System.out.println(); System.out.println("Welcome to the World of Zuul!"); System.out.println("Zuul is a new, incredibly boring adventure game."); System.out.println("Type 'help' if you need help."); }

Code Duplication private void printLocationInfo() { System.out.println("You are " + currentRoom.getDescription()); System.out.print("Exits: "); if(currentRoom.northExit != null) System.out.print("north "); if(currentRoom.eastExit != null) System.out.print("east "); if(currentRoom.southExit != null) System.out.print("south "); if(currentRoom.westExit != null) System.out.print("west "); System.out.println(); } private void goRoom(Command command) { if(!command.hasSecondWord()) { System.out.println("Go where?"); return; } String direction = command.getSecondWord(); Room nextRoom = null; if(direction.equals("north")) nextRoom = currentRoom.northExit; if(direction.equals("east")) nextRoom = currentRoom.eastExit; if(direction.equals("south")) nextRoom = currentRoom.southExit; if(direction.equals("west")) nextRoom = currentRoom.westExit; if (nextRoom == null) System.out.println("There is no door!"); else { currentRoom = nextRoom; public class Game { // ... some code omitted... /** * Print out the opening message for the player. */ private void printWelcome() System.out.println(); System.out.println("Welcome to the World of Zuul!"); System.out.println("Zuul is a new, incredibly boring adventure game."); System.out.println("Type 'help' if you need help."); }

Code Duplication private void goRoom(Command command) { if(!command.hasSecondWord()) { System.out.println("Go where?"); return; } String direction = command.getSecondWord(); Room nextRoom = null; if(direction.equals("north")) nextRoom = currentRoom.northExit; if(direction.equals("east")) nextRoom = currentRoom.eastExit; if(direction.equals("south")) nextRoom = currentRoom.southExit; if(direction.equals("west")) nextRoom = currentRoom.westExit; if (nextRoom == null) System.out.println("There is no door!"); else { currentRoom = nextRoom; printLocationInfo(); public class Game { // ... some code omitted... /** * Print out the opening message for the player. */ private void printWelcome() System.out.println(); System.out.println("Welcome to the World of Zuul!"); System.out.println("Zuul is a new, incredibly boring adventure game."); System.out.println("Type 'help' if you need help."); printLocationInfo(); }