IN 076 Pola Desain Perangkat Lunak Dosen Hapnes

  • Slides: 48
Download presentation
IN 076 Pola Desain Perangkat Lunak Dosen : Hapnes Toba (hapnestoba@gmail. com) Oscar Karnalim

IN 076 Pola Desain Perangkat Lunak Dosen : Hapnes Toba (hapnestoba@gmail. com) Oscar Karnalim (oscar. karnalim@gmail. com) Asisten : Lucky Christiawan Teori : Jumat 13. 00 - 15. 30 Lab ADV 1 Praktikum : Jumat 15. 30 – 17. 30 Lab ADV 1

Referensi Pendukung 1. Eric Freeman & Elisabeth Freeman; Head First Design Patterns; O’Reilly; 2004

Referensi Pendukung 1. Eric Freeman & Elisabeth Freeman; Head First Design Patterns; O’Reilly; 2004 2. Allen Holub; Holub on Patterns: Learning Design Patterns by Looking at Code; Apress; 2004 3. Christopher G. Lasater; Design Patterns; Wordware Publishing Inc. ; 2007 4. Erich Gamma, et. al. ; Design Patterns: Elements of Reusable Object Oriented Software; Addison-Wesley Intl. ; 1994 5. James W. Cooper; Introduction to Design Patterns in C#, IBM TJ Watson Research Center; 2002 6. Jason Mc. Donald; Design Patterns, DZone Refcards; www. dzone. com; 2008 7. Metsker, Steven John, William C. Wake, Design Patterns in Java 2 nd ed. , Addison. Wesley Professional, 2006. 8. Metsker, Steven John, Design Patterns in C#, Addison-Wesley Professional, 2004. 9. Steve Holzner, Ph. D. ; Design Patterns for Dummies; Wiley Publishing, Inc. ; 2006 10. http: //www. dofactory. com 11. etc

Rencana Pertemuan (1) Pertemuan 1 12 Februari 2016 Pengenalan, Review PBO, Strategy (Ch. 1)

Rencana Pertemuan (1) Pertemuan 1 12 Februari 2016 Pengenalan, Review PBO, Strategy (Ch. 1) Pertemuan 2 19 Februari 2016 Observer (Ch. 2), Decorator (Ch. 3) Pertemuan 3 26 Februari 2016 Factory (Ch. 4) Pertemuan 4 4 Maret 2016 Quis 1 Pertemuan 5 11 Maret 2016 Command (Ch. 6) Pertemuan 6 18 Maret 2016 Pertemuan x (Jumat Agung) 25 Maret 2016 Ujian Tengah Semester Template (Ch. 8), Singleton (Ch. 5) Review Materi (1 -6) Quis 2 + Kuis Besar (take home) 28 Maret - 8 April 2016 UTS (Bahan pertemuan 1 -6)

Rencana Pertemuan (2) Pertemuan 7 15 April 2016 Iterator & Composite (Ch. 9), State

Rencana Pertemuan (2) Pertemuan 7 15 April 2016 Iterator & Composite (Ch. 9), State (Ch. 10) Pertemuan 8 22 April 2016 Adaptor & Facade (Ch. 7) Pertemuan 9 29 April 2016 Quis 3 Pertemuan x (Isra Miraj) 6 Mei 2016 Libur Pertemuan 10 13 Mei 2016 Proxy (Ch. 11), Compound (Ch. 12) Pertemuan 11 20 Mei 2016 Patterns in the real world (Ch. 13) Left over patterns (Ch. 14): Bridge, Builder, Chain of Responsibility, Pertemuan 12 27 Mei 2016 Flyweight, Interpreter, Mediator, Momento, Prototype, Visitor; Review Materi (7 -12) Quis 4 + Kuis Besar (take home) Ujian Akhir Semester 30 Mei - 10 Juni 2016 UAS (Bahan pertemuan 7 -12)

Penilaian • UTS (30% dari total) – jika hadir • • Rata-rata kuis (2

Penilaian • UTS (30% dari total) – jika hadir • • Rata-rata kuis (2 x) Rata-rata keaktifan kelas Kuis besar Soal UTS : 15% : 20% : 50% • UAS (30% dari total) – jika hadir • • Rata-rata kuis (2 x) Rata-rata keaktifan kelas Kuis besar Soal UTS : 15% : 20% : 50% : 15% • KAT (40%) – tugas-tugas pertemuan • Sesi praktikum

Organisasi Kelas • Asisten • Mengawasi absensi, pengumpulan tugas dan praktikum • Dosen •

Organisasi Kelas • Asisten • Mengawasi absensi, pengumpulan tugas dan praktikum • Dosen • Pemberian materi dan tugas • Menilai tugas harian, kuis, praktikum dan ujian • Situasi kelas • Keterlambatan max. 15 menit boleh masuk, tidak absen • No ringtones Tidak ada absensi susulan • Tugas di-upload sesuai pemberitahuan di kelas

Course Objectives Design patterns are programming best practices. Learning them makes one an effective

Course Objectives Design patterns are programming best practices. Learning them makes one an effective and efficient developer of software solutions. Starting with introductory background on object-oriented principles, this course seeks to provide an overview of some of the most commonly and widely used design patterns. The course is hands on and involves developing solutions that use these patterns.

Review of Object-Oriented Principles What does OO Design mean?

Review of Object-Oriented Principles What does OO Design mean?

Three pillars of OO Design behavioral OO Design Pattern creational structural Encapsulation Inheritance Polymorphism

Three pillars of OO Design behavioral OO Design Pattern creational structural Encapsulation Inheritance Polymorphism Best practices

Group Discussion (Collaborative Learning) For each concept (Group 1 – 3) 1. Define the

Group Discussion (Collaborative Learning) For each concept (Group 1 – 3) 1. Define the concept 2. Find an UML Class Diagram as an example 3. Find the appropriate OOP code for the example OO Design Encapsulation Inheritance Polymorphism Group 4 1. What is the difference between a class and an object? 2. Give example, what is an abstract class? 3. Give example, what is an interface?

Looking behind the object Encapsulation

Looking behind the object Encapsulation

Encapsulation • Hide the data – make data elements private • Provide access to

Encapsulation • Hide the data – make data elements private • Provide access to data using getters and setters – make these public

What is the big deal here? • Setters can ensure that data fields don’t

What is the big deal here? • Setters can ensure that data fields don’t get set with inappropriate value • You can change the implementation without impacting people who are using your object

Encapsulation Example • Using Java 5. 0 • Using Eclipse 3. 1 • Using

Encapsulation Example • Using Java 5. 0 • Using Eclipse 3. 1 • Using a “Medication” class object getters e get. R osag out D t e e g Nam e get. F e set am t. N rm se Fo get orm Name Dosage Route Form set. D osag e te Rou setters Medication private String: Name private int: Dosage private String: Route private String: Form public String get. Name() public int get. Dosage() public String get. Route() public String get. Form() public void set. Name() public void set. Dosage() public void set. Route() public void get. Form()

Inheritance Dealing with Object Hierarchies

Inheritance Dealing with Object Hierarchies

Inheritance • Facilitates code re-use • Organizes objects in a IS-A hierarchy Person age

Inheritance • Facilitates code re-use • Organizes objects in a IS-A hierarchy Person age name Student major graduationyear Employee type

Polymorphism Many faces of an Object

Polymorphism Many faces of an Object

Polymorphism • Ability to communicate to objects that don’t even exist when initial design

Polymorphism • Ability to communicate to objects that don’t even exist when initial design was created! • Use methods defined in parent class on child class Object 1 print object display object pdf object Word object print object display object new object print object display object

A simple example* Does it make sense to instantiate these classes? animal type eats

A simple example* Does it make sense to instantiate these classes? animal type eats sound herbivore carnivore omnivore type eats sound elephant lion bear type eats sound *Adapted from Head First Java, O’Reilly Press

Abstract Classes • When it does not make sense to instantiate a particular class,

Abstract Classes • When it does not make sense to instantiate a particular class, but it makes sense to define them for the purpose of organization, use an “Abstract” class. • Abstract class cannot be instantiated – they can only be “extended” • Abstract classes can have abstract methods as well. . These methods have to be implemented in the concrete classes.

Role of Interfaces How does one deal with multiple inheritance?

Role of Interfaces How does one deal with multiple inheritance?

Multiple Inheritance animal pet eats sound Rollover? cat dog hippo eats sound

Multiple Inheritance animal pet eats sound Rollover? cat dog hippo eats sound

Problem with multiple inheritance* Digirecord burn() CDBurner DVDBurner burn() Combo. Burner burn() *Adapted from

Problem with multiple inheritance* Digirecord burn() CDBurner DVDBurner burn() Combo. Burner burn() *Adapted from Head First Java, O’Reilly Press

Java approach • Use Interfaces • Classes can “extend” classes and they can “implement”

Java approach • Use Interfaces • Classes can “extend” classes and they can “implement” interfaces.

Multiple Inheritance – make Pet class an interface animal pet eats sound Rollover? cat

Multiple Inheritance – make Pet class an interface animal pet eats sound Rollover? cat dog hippo eats sound

Introduction to Design Patterns Chapter 1 Strategy Pattern

Introduction to Design Patterns Chapter 1 Strategy Pattern

Goals for this week • Learn how to exploit and gain experience of others

Goals for this week • Learn how to exploit and gain experience of others • Examine the benefits of design pattern • Learn one specific pattern: Strategy pattern

Simple Simulation of Duck behavior Duck quack() swim() display() // other duck methods Mallard.

Simple Simulation of Duck behavior Duck quack() swim() display() // other duck methods Mallard. Duck display() // looks like mallard Redhead. Duck display() // looks like redhead Other duck types

What if we want to simulate flying ducks? Duck quack() swim() display() fly() //

What if we want to simulate flying ducks? Duck quack() swim() display() fly() // other duck methods Mallard. Duck display() // looks like mallard Redhead. Duck display() // looks like redhead Other duck types

Tradeoffs in use of inheritance and maintenance Duck quack() swim() display() fly() // other

Tradeoffs in use of inheritance and maintenance Duck quack() swim() display() fly() // other duck methods Mallard. Duck display() // looks like mallard Redhead. Duck display() // looks like redhead One could override the fly method to the appropriate thing – just as the quack method below. Rubber. Duck quack() //overridden to squeak display() // looks like rubberduck fly() // override to do nothing

Example complicated: add a wooden decoy ducks to the mix Decoy. Duck quack(){ //

Example complicated: add a wooden decoy ducks to the mix Decoy. Duck quack(){ // override to do nothing } display() // display decoy duck fly (){ //override to do nothing } Inheritance is not always the right answer. Every new class that inherits unwanted behavior needs to be overridden. How about using interfaces instead?

Duck simulation recast using interfaces. Interfaces Flyable Quackable fly() quack() Mallard. Duck display() fly()

Duck simulation recast using interfaces. Interfaces Flyable Quackable fly() quack() Mallard. Duck display() fly() quack() Redhead. Duck display() fly() quack() Duck swim() display() // other duck methods Rubber. Duck display() quack() Decoy. Duck display()

Pros and cons • Not all inherited methods make sense for all subclasses –

Pros and cons • Not all inherited methods make sense for all subclasses – hence inheritance is not the right answer • But by defining interfaces, every class that needs to support that interface needs to implement that functionality… destroys code reuse! • So if you want to change the behavior defined by interfaces, every class that implements that behavior may potentially be impacted And….

Design Principle Identify the aspects of your application that vary and separate them from

Design Principle Identify the aspects of your application that vary and separate them from what stays the same. OR Take the parts that vary and encapsulate them, so that later you can alter or extend the parts that vary without affecting those that don’t.

In the Duck simulation context… Parts that stay the same Parts that vary Duck

In the Duck simulation context… Parts that stay the same Parts that vary Duck Behaviors

Design Principle • Program to an interface, not to an implementation. • Really means

Design Principle • Program to an interface, not to an implementation. • Really means program to a super type.

Program to an interface • Programming to an implementation Dog d = new Dog();

Program to an interface • Programming to an implementation Dog d = new Dog(); d. bark(); • Programming to an interface Animal animal = new Dog(); animal. makesound();

Duck simulation recast using the new approach <<interface>> Fly. Behavior Duck fly() Fly. Behavior:

Duck simulation recast using the new approach <<interface>> Fly. Behavior Duck fly() Fly. Behavior: fly. Behavior Quack. Behavior: quack. Behavior Fly. With. Wings fly() // implements duck flying perform. Quack() perform. Fly() set. Fly. Behavior() set. Quack. Behavior() swim() display() Mallard. Duck display() Fly. No. Way fly() // do nothing – Can’t fly <<interface>> Quack. Behavior quack() Red. Head. Duck Rubber. Duck Decoy. Duck display() Quack quack() // implements duck quacking Squeak quack() // implements squeak Mutequack() // do nothing

Rationale for design patterns • Shared pattern vocabularies are powerful • Patterns allow you

Rationale for design patterns • Shared pattern vocabularies are powerful • Patterns allow you to say more with less • Reusing tried and tested methods • Focus is on developing flexible, maintainable programs

Design Principle • Favor composition over inheritance • HAS-A (behavior) can be better than

Design Principle • Favor composition over inheritance • HAS-A (behavior) can be better than IS-A • Allows changing behavior at run time

The strategy pattern • The Strategy Pattern defines a family of algorithms, encapsulates each

The strategy pattern • The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. • Strategy lets the algorithm vary independently from clients that use it.

Character Weapon. Behavior weapon; fight(); Axe. Behavior Knife. Behavior Bow. And. Arrow. Behavior use.

Character Weapon. Behavior weapon; fight(); Axe. Behavior Knife. Behavior Bow. And. Arrow. Behavior use. Weapon() //implements cutting with // a knife use. Weapon() //implements fight with // bow and arrows <<interface>> Weapon. Behavior use. Weapon() //implements fight with // an axe Queen fight() use. Weapon() Spear. Behavior King Knight Bishop fight() use. Weapon() //implements fight with // a spear set. Weapon(Weapon. Behavior w){ this. weapon = w; }

Abstract Character Weapon. Behavior weapon; fight(); Knife. Behavior use. Weapon() //implements cutting with //

Abstract Character Weapon. Behavior weapon; fight(); Knife. Behavior use. Weapon() //implements cutting with // a knife Axe. Behavior use. Weapon() //implements fight with // an axe <<interface>> Weapon. Behavior Bow. And. Arrow. Behavior Queen use. Weapon() //implements fight with // bow and arrows fight() use. Weapon() Spear. Behavior King Knight Bishop fight() set. Weapon(Weapon. Behavior w){ this. weapon = w; } use. Weapon() //implements fight with // a spear

Abstract Behavior Interface Character <<interface>> Weapon. Behavior weapon; use. Weapon() fight(); Bow. And. Arrow.

Abstract Behavior Interface Character <<interface>> Weapon. Behavior weapon; use. Weapon() fight(); Bow. And. Arrow. Behavior Axe. Behavior use. Weapon() //implements fight with // an axe Knife. Behavior use. Weapon() //implements cutting with // a knife Queen fight() use. Weapon() //implements fight with // bow and arrows Spear. Behavior King Knight Bishop fight() set. Weapon(Weapon. Behavior w){ this. weapon = w; } use. Weapon() //implements fight with // a spear

Abstract Behavior Interface Character <<interface>> Weapon. Behavior weapon; use. Weapon() fight(); King Knight fight()

Abstract Behavior Interface Character <<interface>> Weapon. Behavior weapon; use. Weapon() fight(); King Knight fight() Bishop Queen fight() Axe. Behavior use. Weapon() //implements fight with // an axe Knife. Behavior use. Weapon() //implements cutting with // a knife set. Weapon(Weapon. Behavior w){ this. weapon = w; } Bow. And. Arrow. Behavior use. Weapon() //implements fight with // bow and arrows Spear. Behavior use. Weapon() //implements fight with // a spear

Behavior Interface Abstract <<interface>> Weapon. Behavior Character Weapon. Behavior weapon; fight(); set. Weapon(Weapon. Behavior

Behavior Interface Abstract <<interface>> Weapon. Behavior Character Weapon. Behavior weapon; fight(); set. Weapon(Weapon. Behavior w){ this. weapon = w; } King Knight fight() Bishop Queen fight() use. Weapon() Axe. Behavior use. Weapon() //implements fight with // an axe Knife. Behavior use. Weapon() //implements cutting with // a knife Bow. And. Arrow. Behavior use. Weapon() //implements fight with // bow and arrows Spear. Behavior use. Weapon() //implements fight with // a spear

Challenge: next week in class discussion • Implement the weapon class from previous slide

Challenge: next week in class discussion • Implement the weapon class from previous slide (individual) • Upload via cls. maranatha. edu • Due Fri, 19 Feb 2016, 12: 00