adapter - lecturer.ukdw.ac.idlecturer.ukdw.ac.id/anton/download/pbk11.pdf · usb to e-sata usb to...

38
PBK 5 Adapter Patterns

Upload: truonganh

Post on 27-Aug-2019

222 views

Category:

Documents


1 download

TRANSCRIPT

PBK 5

Adapter Patterns

What is adapter?

Usb to serial Apple mini divi to VGAEuropean - US power adapter

Usb to serial Apple mini divi to VGA

Usb to e-sata USB to RJ11European - US power adapter

Adapter Pattern

“Converts the interface of a class into another interface the clients expect”

Adapter lets classes work together that

couldn’t, otherwise because of incompatible

Interfaces

A.K.A WRAPPER

Pernah butuh konverter stop kontak listrik?

How to implement vendor class to your Existing System?

Interface tidak cocok!

Adapter dapatmengimplements sistem kita sistem kita dan berkomunikasi dengan sistem vendor

No change New code No change

Can you think a solution that doesn’t require YOU to WRITE ANY additional code to integrate the NEW vendor classes?

How about making the vendor SUPPLY the adapter class?adapter class?

Adapter Pattern Motivation

• Sometimes a toolkit or class library can not be used because its interface isincompatible with the interface required by an application

• We can not change the library interface, since we may not have its source code

• Even if we did have the source code, we probably should not change the library for each domain-specific application

Kalkun menjadi Bebek?

• Bisa tidak kalkun menjadi bebek?– Pakai Adapter!

Coba ingat lagi Bebek!

Kalkun ?

Implementasi Adapter

Harus terbang 5x!Karena jangkauan terbang Kalkun pendek

Adapter Tester

Output

Adapter Pattern Explained

Adapter Pattern Explained

Coba buat!

• Adapter untuk mengubah Bebek ke Kalkun !– Nama class BebekAdapter

Jawab

Object Adapter Pattern

Object Adapter Pattern dapat diperluas

Class adapter pattern

C++

C++

Real Adapter (pada Java )

• Old Enumerator– Berupa Interface

– Untuk segala Collection: Vector, Stack, HashTableHashTable

• New Iterator– Berupa Interface

– Untuk Collection juga namun memiliki kemampuan menghapus (remove)

Bagaimana cara Enum jadi Interator?

Desain

TestDrive

Contoh Two Way Adapter

public class Square {public void insert(String str) {

System.out.println("Square insert(): " + str);}

}}

public class Round {public void insertIntoHole(String msg) {

System.out.println("Round insertIntoHole(): " + msg);}

}

Pertanyaan

• What if we want to have an adapter that acts as a Square or a Round? • Such an adapter is called a two -way adapter .

• One way to implement two-way adapters • One way to implement two-way adapters is to use multiple inheritance , but we can't do this in Java

• But we can have our adapter class implement two different Java interfaces!

Interface

public interface IRound {

public void insertIntoHole(String msg);

}

public interface ISquare {

public void insert(String str);

}

Ubah Round dan Square

public class Round implements IRound {public void insertIntoHole(String msg) {

System.out.println("Round insertIntoHole(): " + msg);}

}}

public class Square implements ISquare {public void insert(String str) {

System.out.println("Square insert(): " + str);}

}

The Adapter

public class theAdapter implements ISquare, IRound {private Round round;private Square square;public theAdapter(Round r) {this.round = r;}public theAdapter(Round r) {this.round = r;}public theAdapter(Square s) {this.square = s;}public void insert(String str)

{round.insertIntoHole(str);}public void insertIntoHole(String

msg){square.insert(msg);}}

Clientpublic class Test {

public static void main(String args[]) {Round round = new Round();Square square = new Square();

// Create a two-way adapter and do an insert with it.// Create a two-way adapter and do an insert with it.theAdapter roundToSquare = new theAdapter(round, square);roundToSquare.insert("Inserting square...");roundToSquare.insertIntoHole("Inserting round...");

System.console().readLine();}

}

Output

NEXT

• State Pattern dan Decorator Pattern