Intro to Design Pattern

Download Report

Transcript Intro to Design Pattern

Gita Indah Marthasari 1

    Mahasiswa mengetahui sejarah kemunculan

pattern dan desain pattern

Mahasiswa mampu menjelaskan tujuan

penggunaan desain pattern

Mahasiswa mampu menjelaskan pengelompokan dan perbedaan tiap kelompok desain pattern Mahasiswa mampu menggambarkan class diagram UML Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 2

 The first among a dozen definitions in Webster's Ninth New Collegiate is this: 

A form or model proposed for imitation

 As currently used in the software industry: 

A pattern is a description of a common problem and a likely solution, based on experience with similar

situations. (Mitchell, 2003) Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 3

 Christopher Alexander proposed in the visually pleasing and practical structures for a certain application and/or setting could be 1970s that described by a pattern language .

 For example, Alexander wrote that desirable farmhouses in the Bernese Oberland area of Switzerland used these patterns:  North South Axis  West Facing Entrance Down The Slope  Two Floors  Hay Loft At The Back  Bedrooms In Front Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 4

  Alexander claimed that following these patterns when designing a farmhouse produces a structure that blends with the environment and the community, and has the "Quality Without A Name".

Alexander further claimed that a pattern language permits a great variety of buildings to be designed that meet a particular need.

Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 5

  In 1987 , Kent Beck and Ward Cunningham presented an OOPSLA paper titled Using Pattern Languages for Object-Oriented Programs.

It described a project that applied Alexander's work to the problem of user-interface design guided by these patterns: in which the eventual users of a system designed the interface  Window Per Task  Few Panes Per Window  Standard Panes  Short Menus  Nouns and Verbs Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 6

  In the late 1980s and early 1990s a number of individuals began to look at the problem of identifying and describing patterns used to create software In 1995 the now-classic text Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides was published .

Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 7

   In art , a pattern is the composition or plan of a work of graphic or plastic art. In architecture , a pattern is an architectural design or style.

In psychology , a pattern is a thinking mechanism that is basic to the brain’s operation, helping one to perceive things quickly Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 8

    In dressmaking , a pattern is a pleasing shape that is applied repeatedly. In decorating , a pattern is a design or figure appearing in furniture or an accessory. In manufacturing , a pattern is the shape or style of a manufactured form.

In chess , a pattern is a set of moves that may be applied in an overall strategy Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 9

  For architectural pattern, we see certain physical elements repeated endlessly, combined in an almost endless variety of combinations To find out what it really is that is repeating there we need to look more carefully Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 10

 Analisis dan desain berorientasi objek (ADBO) bergantung pada classes and objects sebagai komponen dasar sistem.

 These classes and objects form

patterns

specific relationship between them.

with  Groups of classes in an object-oriented environment are likely to be useful

repeatedly

Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 11

 Pada pengembangan sistem pemantau suhu :  Sistem yang menggunakan sensor suhu untuk memantau temperatur sebuah hardware.

 Misalnya, model sensor pertama yang digunakan : TempTek, Inc. TS7000 sensors.  Kode untuk mengakses suhu menggunakan sensor TempTek dalam Java :

class TS7000 { native double getTemp(); ...

}

Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 12

 Berikut kode untuk menghitung rata-rata suhu dari sensor :

double sum = 0.0; for (int i = 0; i < sensors.length; i++) sum += sensors[i].getTemp(); double meanTemp = sum / sensors.length;

Variabel

sensors

dideklarasikan sbg array of TS7000 objects. ( TS7000 sensors[ ] = new TS7000[...] ) Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 13

 Berikutnya, andaikan akan dipakai model sensor kedua yang digabungkan dengan model sebelumnya, misalnya dari vendor Thermon.  Kelas Thermon :

class SuperTempReader { //NOTE: temperature is Celsius tenths of a degree // native double current_reading(); ...

}

Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 14

 Kode setelah penambahan tipe sensor :

for (int i = 0; i < sensors.length; i++) { if (sensors[i] instanceof TS7000) sum += ((TS7000)sensors[i]).getTemp(); else // Must be a SuperTemp!

sum += } ((SuperTempReader)sensors[i]).current_re

ading() * 10;

Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 15

 In this case sensors is an array of Objects. The type is tested with instanceof and an appropriate cast and method call is performed.

Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 16

   Persoalan potensial :  Jika ada sensor dari vendor berbeda yang ingin digabungkan.

 Sensor berbeda mungkin memiliki cara penggunaan yang berbeda, misalnya cara menjalankan, konversi nilai output, dll.

Prinsip OO Design : Program should open for extension but close for modification.

Solusi :

ADAPTER PATTERN

Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 17

Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 18

 A Design Pattern is essentially :  A description of a commonly occurring object oriented design problem and how to solve it.

Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 19

Purpose

Creational : pembuatan objek Structural : komposisi kelas dan objek Behavioral : interaksi antara kelas dan objek Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang

Scope

Class : melibatkan pewarisan Object : melibatkan komposisi 20

Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 21

Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 22

   A developer with training in object-oriented design would probably produce a similar solution without much effort.

What's the real contribution of design patterns?

 Simply having a name for a solution provides a real benefit: A developer might say "Let's use an ADAPTER."

rather than

"How about a superclass with a getTemperature method and then subclass that with classes that reference instances of the vendor-supplied classes for...." Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 23

  For a while "pattern" was essentially synonymous with "object oriented design pattern" but the meaning has broadened.

In the literature you can now find "patterns" for:  High-level application architecture  User interfaces  Software testing  Project organization and management  Web sites, etc Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 24

1.

2.

3.

Tuliskan awal mula munculnya desain pattern Tuliskan tujuan penggunaan desain pattern Gambarkan class diagram untuk kelas Inbox dan SentBox berikut : Inbox Sentbox

Atribut

-pesanMasuk -tglMasuk -pesanKeluar -tglKeluar

Method

+bacaPesan() +kirimPesan() +bacaPesan() +terimaPesan() Jurusan Teknik Informatika Fakultas Teknik Univ. Muhammadiyah Malang 25