• Tidak ada hasil yang ditemukan

Prinsip-Perinsip Perancangan Kelas

N/A
N/A
Protected

Academic year: 2021

Membagikan "Prinsip-Perinsip Perancangan Kelas"

Copied!
30
0
0

Teks penuh

(1)

Prinsip-Perinsip

Perancangan Kelas

Oleh : Agus Priyanto, M.Kom

Oleh : Agus Priyanto, M.Kom

(2)

Tujuan Perkuliahan

Setelah mempelajari materi ini, diharapkan

mahasiswa mampu:

Mendapatkan pengetahuan tentang

Mendapatkan pengetahuan tentang

perancangan

kelas yang baik

serta

menerjemahkannya dalam

bahasa

(3)

Outline Kuliah

1. Tentang Kelas (review)

2. Petunjuk Perancangan Kelas

3. Accessor, Mutator dan Overloaded Methods

4. Prinsip perancangan kelas

(4)

Definisi Kelas

class ClassName

{

Fields

Constructors

Methods

atribut

Methods

}

perilaku

(5)

Elemen-elemen dasar dalam mendefinisikan

kelas

1. Field

(variabel) : menyimpan data untuk

setiap objek (implementasi dari atribut)

setiap objek (implementasi dari atribut)

2. Constructor

: setup objek di awal

(6)

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

(7)

a. Fungsi Accessor

Fungsi untuk mendapatkan

property

dari

suatu objek.

Mengembalikan

nilai atau value dari suatu

Mengembalikan

nilai atau value dari suatu

(8)

b. Fungsi Mutator

Fungsi untuk mengubah

property

dari suatu

objek.

Mengubah

nilai atau value dari sebuah

atribut.

(9)

3. Selalu definisikan suatu

konstruktor

dan

inisialisasikan

variabel instance secara lengkap

dalam konstruktor sehingga objek akan

dibuat dalam suatu kondisi yang valid

dibuat dalam suatu kondisi yang valid

(10)

4. Deklarasikan konstanta sebagai

public

apabila

nilainya akan diakses oleh klien atau kelas

lain. Jika hanya digunakan secara internal

deklarasikan sebagai

private

(11)

Beberapa Prinsip Perancangan

a. Coupling

b. Encapsulation

c. Cohesion

(12)

Coupling

Keterikatan

antar kelas

Kelas berkomunikasi melalui antar muka yang

telah didefinisikan dengan baik

telah didefinisikan dengan baik

Yang baik :

loose coupling

Perubahan

pada satu kelas tidak memiliki

(13)

Encapsulation

Digunakan untuk

mengurangi

efek coupling

Menegaskan pemisahan antara

what

dan

how

dengan membuat atribut sebagai

private

dan

dengan membuat atribut sebagai

private

dan

menggunakan fungsi

acessor

untuk

(14)

Cohesion

Satu unit kode

seharusnya selalu

bertanggung jawab pada

satu dan hanya satu

tugas

(task)

Cohesion

dapat diterapkan pada

kelas

dan

(15)

Cohesion pada method

Satu method hanya bertanggung jawab pada

satu dan hanya satu

well-defined task

Lihat contoh – cohesive-1.java &

(16)

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();

System.out.println(currentRoom.getLongDescription());

Non

Cohesive

Non-Cohesive

// 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.");

}

(17)

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();

System.out.println(currentRoom.getLongDescription());

// Enter the main command loop. Here we repeatedly read

// 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()

(18)

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();

System.out.println(currentRoom.getLongDescription());

Task tambahan fungsi

play()

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()

(19)

/**

* 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.");

Cohesive

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();

System.out.println(currentRoom.getLongDescription());

}

(20)

Cohesion pada kelas

Setiap kelas harus merepresentasikan

entitas

(21)

Code Duplication

Pertanda rancangan kelas yang tidak baik

Problem

perubahan di satu bagian harus

diikuti dengan bagian lain untuk

diikuti dengan bagian lain untuk

menghindarkan

inkonsistensi

(22)

Code Duplication

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();

System.out.println("You are " + currentRoom.getDescription()); System.out.print("Exits: ");

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; 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(); }

// ... 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.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(); } } }

(23)

Code Duplication

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();

System.out.println("You are " + currentRoom.getDescription()); System.out.print("Exits: ");

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; 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(); }

// ... 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.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(); } } }

duplikasi

(24)

Solusi ???

Solusi ???

(25)

Code Duplication

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();

System.out.println("You are " + currentRoom.getDescription()); System.out.print("Exits: ");

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; 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(); }

// ... 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.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(); } } }

(26)

Code Duplication

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();

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; }

// ... some code omitted...

nextRoom = currentRoom.westExit; if (nextRoom == null) System.out.println("There is no door!"); else { currentRoom = nextRoom; } } }

(27)

Code Duplication

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();

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;

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)

}

// ... some code omitted...

nextRoom = currentRoom.westExit; if (nextRoom == null) System.out.println("There is no door!"); else { currentRoom = nextRoom; } } }

if(currentRoom.westExit != null)

System.out.print("west ");

System.out.println();

}

(28)

Code Duplication

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();

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; printLocationInfo(); }

// ... some code omitted...

nextRoom = currentRoom.westExit; if (nextRoom == null) System.out.println("There is no door!"); else { currentRoom = nextRoom; printLocationInfo(); } } }

(29)
(30)

Referensi

Dokumen terkait

Ekonomi Malaysia dipenuhi dengan pekerja asing tidak mahir atau mempunyai kemahiran yang rendah menyebabkan ia tidak dapat memberi sumbangan bermakna dan berkesan

Untuk mendukung kegiatan program Aksi Perbibitan Kerbau melalui kegiatan uji coba inseminasi buatan kerbau yang dimulai pada tahun 2006 – 2009, bekerjasama dengan Dinas

Puji dan syukur kehadirat Allah Yang Maha Kuasa atas semua rahmat dan karunia yang telah Dia berikan sehingga penulis dapat menyelesaikan tugas akhir ini. Penyelesaian tugas akhir

Penelitian ini bertujuan untuk mengetahui pengaruh harga, lokasi dan pelayanan terhadap kepuasan konsumen Angkringan Kang Harjo dan menganalisis banyaknya faktor

 Komite tidak menyetujui penggunaan Tokoferol (INS. 150d) pada draft regional standard for non-fermented soybean product karena batas maksimum yang diajukan

cara yang ditempuh adalah dengan menggunakan model pembelajaran Numbered Heads Together karena menurut peneliti model pembelajaran Numbered Heads Together merupakan

Hal ini disebabkan oleh karena relasi yang mereka miliki adalah warga sekitar adalah orang Hindu yang sangat menghormati adat dan budaya dari Bali itu sendiri, untuk

Setiap kelompok mengisi deskripsi masing-masing Lembaga Kedaulatan Mahasiswa yang ada di Fakultas MIPA dengan ketentuan 1 lembar Form A untuk setiap lembaga.. Setiap kelompok