Prinsip-Perinsip Perancangan Kelas

dokumen-dokumen yang mirip
Understanding Class Definition. Viska Mutiawani, M.Sc

Understanding Class Definition

Bahasa Pemrograman 2.

Pemrograman Lanjut Class, Instance Variable dan Method PTIIK

Praktikum 4 Konsep Inheritance, Polymorphism, dan Encapsulation

Pemrograman Lanjut. Instance Variable Access Modifier Set dan Get Method

MEMBUAT KELAS SENDIRI. Dewi Sartika, M.Kom

06/03/2018. Tidak memiliki parameter. Melakukan inisialisasi semua variabel private dengan nilai awal.

Kelas dan Objek. Oleh : Agus Priyanto, M.Kom

Membuat dan Menggunakan Class

MODUL PRAKTIKUM PEMROGRAMAN BERORIENTASI OBJEK

Pewarisan Jamak Oleh : Agus Priyanto Priy, anto M.Kom M.K

PERTEMUAN III OBJEK DAN CLASS TUJUAN PRAKTIKUM

BAB 6. Struktur Kontrol

PEMROGRAMAN BERORIENTASI OBJEK

BAB 6. Struktur Kontrol

Pengenalan pemrograman berorientasi obyek

Algoritma Pemrograman [BS204]

Struktur Kontrol. Gambar 1: Flowchart Statement If

BAB 1 PENGENALAN PEMROGRAMAN BERORIENTASI OBJEK

Konsep Dasar Pemrograman Berorientasi Obyek I JAVA. Anugrah Kusuma Seno Adi Putra

Variabel dengan tipe dasar hanya dapat menyimpan sebuah nilai

Pengenalan OOP. Object-Oriented programming (OOP) Menitikberatkan pada konsep object sebagai elemen dasar dari program.

INTERAKSI ANTAR OBJECT

C#, Programming, Object-oriented. Pengenalan C# Erick Pranata. Edisi I

Pemrograman Lanjut. Class : Deeper Look 2

IF PEMROGRAMAN LANJUT TUGAS 1. Oleh : Andri Heryandi, M.T.

KUM 3 IMPLEMENTASI LIST

Pemrograman Lanjut Review Class dan Object PTIIK

PRAKTIKUM 3 KONSTRUKTOR DAN OVERLOADING

Praktikum 6 Class Design (Encapsulation)

E-Book PEMROGRAMAN BERORIENTASI OBJEK. Disusun Oleh: Arfian Hidayat, S.Kom

Bahasa Pemrograman :: Java Class Library

Pemrograman Berorientasi. Class dan Obyek 2

Pemrograman Lanjut Jurusan S1 Teknik Informatika. 9/17/2012 Ratno

Other OOP Basic. Viska Mutiawani, M.Sc

A. TUJUAN PEMBELAJARAN. 1. Menerapkan konsep enkapsulasi pada class 2. Mendeklarasikan suatu constructor

PERTEMUAN 3 OBJEK DAN CLASS

Kurikulum Qt. { Basic OOP } Chapter 6. Class dan Object

Dasar-Dasar OOP di Java

Problem 1. Implementasi Interface. Diberikan definisi interface BentukDuaDimensi berikut:

Pertemuan 8 Pemrograman

Other OOP Basic. Viska Mutiawani, M.Sc. 1 Viska Mutiawani - Informatika FMIPA Unsyiah

Obyektif : KONTROL ALUR PROGRAM

Bahasa Pemrograman 2.

Bahasa Pemrograman :: Polimorfism and Abstraction

Encapsulation (Encapsulasi) Minggu 5 Pemrograman Berorientasi Objek Alfa Faridh Suni

MODUL 2 STRUKTUR KONTROL

BAB Argument Command-Line dan System Properties

Sekarang, kita akan mencoba untuk menganalisa program Java pertama : public class Hello {

PEMAHAMAN DASAR DASAR JAVA

Everybody in this country should learn how to program a computer because it teaches you how to think. Steve Jobs

2 TIPE DATA DAN VARIABEL

Perulangan, Percabangan, dan Studi Kasus

BAHASA PEMROGRAMAN JAVA

BAHASA PEMROGRAMAN JAVA PUTU INDAH CIPTAYANI JURUSAN SISTEM INFORMASI STMIK AMIKOM YOGYAKARTA

ABSTRACTION, ENCAPSULATION, INHERITANCE & POLYMORPHISM

Pemrograman Berorientasi Object. IMade Suartana, S.Kom, M.Kom

SEKOLAH TINGGI MANAJEMEN INFORMATIKA & KOMPUTER JAKARTA STI&K SATUAN ACARA PERKULIAHAN

Array Pendeklarasian Array

Tipe if : If tanpa else (if) If dengan else (if-else) Nested if

Tipe data primitif adalah tipe data dasar yang dikenali oleh Java dan bukan merupakan class. Tipe data primitif ini dapat dikenali dengan ciri

Modul Praktikum Bahasa Pemrograman 1

UNIVERSITAS GUNADARMA

Modul Praktikum Bahasa Pemrograman 1

PRAKTIKUM 3 KONSTRUKTOR DAN OVERLOADING

24/09/2017 PERULANGAN

Enkapsulasi. Praktikum 7

BAB II VARIABEL DAN TIPE DATA

Pemrograman Lanjut. Class, dan Instance Variable. Aryo Pinandito, ST, M.MT Team Teaching Pemrograman Lanjut

Bahasa Pemrograman :: Inheritance

JAVA BASIC PROGRAMMING Joobshet

Modul Praktikum 4 Pemograman Berorientasi Objek

Class & Object 2. Imam Fahrur Rozi

Dasar-Dasar OOP di Java

Modul Praktikum 3 Pemograman Berorientasi Objek

Minggu III STRUKTUR PEMILIHAN (KONTROL PROGRAM)

Algoritma Pemrograman 2B (Pemrograman C++)

PERTEMUAN 2 PEMOGRAMAN BERORIENTASI OBJEK L/O/G/O

Pengenalan pemrograman berorientasi obyek. Politeknik Elektronika Negeri Surabaya. Topik

Praktikum 7. Dasar-dasar OOP di Java

PEMROGRAMAN JAVA. Yoannita, S.Kom. Class & Method sederhana Konsep Pemrograman Berorientasi Objek. Company LOGO

ENKAPSULASI Data Hiding

Praktikum 3 Konsep Class, Attribute dan Method

Kita akan membuat class yang mengandung informasi dari Siswa dan operasi operasi yang dibutuhkan pada record siswa.

OBJECT ORIENTED PROGRAMMING DENGAN PHP. Janitra Panji

KARAKTERISTIK PEMROGRAMAN BERORIENTASI OBJEK (OOP) Pemrograman berorientasi Objek mempunyai karakterisitik sebagai berikut:

Class & Object 2 ~Praktikum~ Imam Fahrur Rozi

CLASS PUTU INDAH CIPTAYANI JURUSAN SISTEM INFORMASI STMIK AMIKOM JOGJAKARTA

OVERLOADING, CONSTRUCTOR DAN DESTRUCTOR. Dewi Sartika, M.Kom

Praktikum. PBO (Kelas K) Oleh : MOHAMMAD SHOLIKIN

I. Tujuan Instruksional Umum Komunikasi antar objek pada kelas yang berbeda Memahami berbagai macam modifier dan karakteristiknya II.

Pemrograman Lanjut Tipe Data dan Constructor PTIIK

CLASS & OBJECT DALAM JAVA (IS1313)

GARIS-GARIS BESAR PROGRAM PERKULIAHAN (GBPP)

Konsep Dasar Pemrograman Berorientasi Objek

PERCOBAAN 6 EXCEPTION

Pemrograman Berorientasi. Abstract & Interface

Array 1 Dimensi pada Java

PENYELEKSIAN /PERCABANGAN & PERULANGAN Part II. 5 th week Estu Sinduningrum ST,MT

Transkripsi:

Prinsip-Perinsip Perancangan Kelas Oleh : Agus Priyanto, M.Kom

Tujuan Perkuliahan Setelah mempelajari materi ini, diharapkan mahasiswa mampu: Mendapatkan pengetahuan tentang perancangan kelas yang baik serta menerjemahkannya dalam bahasa pemrograman

Outline Kuliah 1. Tentang Kelas (review) 2. Petunjuk Perancangan Kelas 3. Accessor, Mutator dan Overloaded Methods 4. Prinsip perancangan kelas

Definisi Kelas atribut perilaku class ClassName Fields Constructors Methods

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

Petunjuk pendefinisian kelas 1. Deklarasikan variabel (data member) sebagai private untuk menjamin integritas kelas Perwujudan enkapsulasi 2. Deklarasikan fungsi dengan public untuk menyediakan akses kepada klien kelas. Fungsi public menentukan perilaku objek dari kelas.

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

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

3. Selalu definisikan suatu konstruktor dan inisialisasikan variabel instance secara lengkap dalam konstruktor sehingga objek akan dibuat dalam suatu kondisi yang valid

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

Beberapa Prinsip Perancangan a. Coupling b. Encapsulation c. Cohesion d. Code Duplication

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

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.

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

Cohesion pada method Satu method hanya bertanggung jawab pada satu dan hanya satu well-defined task Lihat contoh cohesive-1.java & noncohesive-1.java

Non- Cohesivee public void play() 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.");

public void play() 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()

public void play() 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. Task tambahan fungsi play() 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()

/** * 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("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()); Cohesive

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

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 public class Game //... some code omitted... /** * Print out the opening message for the player. */ private void printwelcome() System.out.println("Welcome to the World of Zuul!"); 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")) System.out.println("Zuul is a new, incredibly boring adventure game."); nextroom = currentroom.eastexit; System.out.println("Type 'help' if you need help."); if(direction.equals("south")) nextroom = currentroom.southexit; System.out.println("You are " + currentroom.getdescription()); if(direction.equals("west")) 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 "); //... some code omitted... nextroom = currentroom.westexit; if (nextroom == null) System.out.println("There is no door!"); else currentroom = nextroom; System.out.println("You are " + currentroom.getd 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 public class Game //... some code omitted... /** * Print out the opening message for the player. */ private void printwelcome() System.out.println("Welcome to the World of Zuul!"); game."); System.out.println("Zuul is a new, incredibly boring adventure 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 "); //... some code omitted... 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.getd 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 ");

Solusi???

Code Duplication public class Game //... some code omitted... /** * Print out the opening message for the player. */ private void printwelcome() System.out.println("Welcome to the World of Zuul!"); game."); System.out.println("Zuul is a new, incredibly boring adventure 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 "); //... some code omitted... 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.getd 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 public class Game //... some code omitted... /** * Print out the opening message for the player. */ private void printwelcome() 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."); 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; //... some code omitted...

Code Duplication public class Game //... some code omitted... /** * Print out the opening message for the player. */ private void printwelcome() System.out.println("Welcome to the World of Zuul!"); System.out.println("Zuul is a new, incredibly boring adventure game."); private void printlocationinfo() System.out.println("Type 'help' if you need help."); private void goroom(command command) if(!command.hassecondword()) System.out.println("Go where?"); return; String direction = command.getsecondword(); System.out.print("north Room nextroom = "); null; if(direction.equals("north")) nextroom = currentroom.northexit; System.out.print("east if(direction.equals("east")) "); nextroom = currentroom.eastexit; if(direction.equals("south")) System.out.print("south nextroom "); = currentroom.southexit; if(direction.equals("west")) nextroom = currentroom.westexit; System.out.println("You are " + currentroom.getdescription()); System.out.print("Exits: "); if(currentroom.northexit!= null) if(currentroom.eastexit!= null) if(currentroom.southexit!= null) if(currentroom.westexit!= null) System.out.print("west if (nextroom == null) "); System.out.println("There is no door!"); else currentroom = nextroom; //... some code omitted...

Code Duplication public class Game //... some code omitted... /** * Print out the opening message for the player. */ private void printwelcome() 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(); 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(); //... some code omitted...