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
NonNon Cohesive 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 // 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(); 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 // 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()
Task tambahan fungsi public void play() { 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 // commands and execute them until the game is over. boolean finished = false; while (! finished) { Command command = parser.getCommand(); finished = processCommand(command); Task utama fungsi } play() System.out.println("Thank you for playing. Good bye."); }
/** * 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
} /** 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()); }
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
private void goRoom(Command command) public class Game { { if(!command.hasSecondWord()) { // ... some code omitted... System.out.println("Go where?"); /** return; * Print out the opening message for the player. } */ String direction = command.getSecondWord(); private void printWelcome() Room nextRoom = null; { if(direction.equals("north")) System.out.println(); nextRoom = currentRoom.northExit; System.out.println("Welcome to the World of Zuul!"); 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")) System.out.println(); nextRoom = currentRoom.southExit; System.out.println("You are " + currentRoom.getDescription()); if(direction.equals("west")) System.out.print("Exits: "); nextRoom = currentRoom.westExit; if(currentRoom.northExit != null) if (nextRoom == null) System.out.print("north "); System.out.println("There is no door!"); if(currentRoom.eastExit != null) else { System.out.print("east "); currentRoom = nextRoom; if(currentRoom.southExit != null) System.out.println("You are " + currentRoom.getDescription System.out.print("south "); System.out.print("Exits: "); if(currentRoom.westExit != null) if(currentRoom.northExit != null) System.out.print("west "); System.out.print("north "); System.out.println(); if(currentRoom.eastExit != null) } System.out.print("east "); if(currentRoom.southExit != null) // ... some code omitted... System.out.print("south "); if(currentRoom.westExit != null) System.out.print("west "); System.out.println(); } } }
Code Duplication
private void goRoom(Command command) public class Game { { if(!command.hasSecondWord()) { // ... some code omitted... System.out.println("Go where?"); /** return; * Print out the opening message for the player. } */ String direction = command.getSecondWord(); private void printWelcome() Room nextRoom = null; { if(direction.equals("north")) System.out.println(); nextRoom = currentRoom.northExit; System.out.println("Welcome to the World of Zuul!"); if(direction.equals("east")) System.out.println("Zuul is a new, incredibly boring adventure nextRoom = currentRoom.eastExit; game."); if(direction.equals("south")) System.out.println("Type 'help' if you need help."); nextRoom = currentRoom.southExit; System.out.println(); if(direction.equals("west")) System.out.println("You are " + currentRoom.getDescription()); nextRoom = currentRoom.westExit; System.out.print("Exits: "); if (nextRoom == null) if(currentRoom.northExit != null) System.out.println("There is no door!"); System.out.print("north "); else { if(currentRoom.eastExit != null) currentRoom = nextRoom; System.out.print("east "); System.out.println("You are " + currentRoom.getDescription if(currentRoom.southExit != null) System.out.print("Exits: "); System.out.print("south "); if(currentRoom.northExit != null) if(currentRoom.westExit != null) System.out.print("north "); System.out.print("west "); if(currentRoom.eastExit != null) System.out.println(); System.out.print("east "); } if(currentRoom.southExit != null) duplikasi System.out.print("south "); // ... some code omitted... if(currentRoom.westExit != null) System.out.print("west "); System.out.println(); } } }
Solusi ???
Code Duplication
private void goRoom(Command command) public class Game { { if(!command.hasSecondWord()) { // ... some code omitted... System.out.println("Go where?"); /** return; * Print out the opening message for the player. } */ String direction = command.getSecondWord(); private void printWelcome() Room nextRoom = null; { if(direction.equals("north")) System.out.println(); nextRoom = currentRoom.northExit; System.out.println("Welcome to the World of Zuul!"); if(direction.equals("east")) System.out.println("Zuul is a new, incredibly boring adventure nextRoom = currentRoom.eastExit; game."); if(direction.equals("south")) System.out.println("Type 'help' if you need help."); nextRoom = currentRoom.southExit; System.out.println(); if(direction.equals("west")) System.out.println("You are " + currentRoom.getDescription()); nextRoom = currentRoom.westExit; System.out.print("Exits: "); if (nextRoom == null) if(currentRoom.northExit != null) System.out.println("There is no door!"); System.out.print("north "); else { if(currentRoom.eastExit != null) currentRoom = nextRoom; System.out.print("east "); System.out.println("You are " + currentRoom.getDescription if(currentRoom.southExit != null) System.out.print("Exits: "); System.out.print("south "); if(currentRoom.northExit != null) if(currentRoom.westExit != null) System.out.print("north "); System.out.print("west "); if(currentRoom.eastExit != null) System.out.println(); System.out.print("east "); } if(currentRoom.southExit != null) System.out.print("south "); // ... some code omitted... if(currentRoom.westExit != null) System.out.print("west "); System.out.println(); } } }
Code Duplication
private void goRoom(Command command) public class Game { { if(!command.hasSecondWord()) { // ... some code omitted... System.out.println("Go where?"); /** return; * Print out the opening message for the player. } */ String direction = command.getSecondWord(); private void printWelcome() Room nextRoom = null; { if(direction.equals("north")) System.out.println(); nextRoom = currentRoom.northExit; System.out.println("Welcome to the World of Zuul!"); if(direction.equals("east")) System.out.println("Zuul is a new, incredibly boring adventure nextRoom = currentRoom.eastExit; game."); if(direction.equals("south")) System.out.println("Type 'help' if you need help."); nextRoom = currentRoom.southExit; System.out.println(); 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 private void printLocationInfo() private void goRoom(Command command)
public class Game { { { if(!command.hasSecondWord()) { System.out.println("You areSystem.out.println("Go " + currentRoom.getDescription()); // ... some code omitted... where?"); /** System.out.print("Exits: "); return; * Print out the opening message for the player. } if(currentRoom.northExit != null) = command.getSecondWord(); */ String direction private void printWelcome() Room nextRoom = "); null; System.out.print("north { if(direction.equals("north")) if(currentRoom.eastExit != null) System.out.println(); nextRoom = currentRoom.northExit; System.out.println("Welcome to the World of Zuul!"); if(direction.equals("east")) System.out.print("east "); System.out.println("Zuul is a new, incredibly boring adventure nextRoom = currentRoom.eastExit; game."); if(currentRoom.southExit != null) if(direction.equals("south")) System.out.println("Type 'help' if you need help."); nextRoom "); = currentRoom.southExit; System.out.println(); System.out.print("south if(direction.equals("west")) if(currentRoom.westExit != nextRoom null) = currentRoom.westExit; if (nextRoom == null) System.out.print("west "); System.out.println("There is no door!"); System.out.println();else { currentRoom = nextRoom; }
} // ... some code omitted...
} } }
Code Duplication private void goRoom(Command command)
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();
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...
} } }