IN 076 Pola Desain Perangkat Lunak Dosen: Hapnes Toba Oscar Karnalim

Slides:



Advertisements
Similar presentations
Winter 2007ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Advertisements

+ Informatics 122 Software Design II Lecture 7 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
D ESIGN P ATTERNS Introduction. C OURSE D ESCRIPTION Traditionally, OO designers have developed their own private "catalogs" of solutions to recurring.
Informatics 122 Software Design II
Strategy Pattern1 Design Patterns 1.Strategy Pattern How to design for flexibility?
Patterns Reusable solutions to common object-oriented programming problems When given a programming problem, re-use an existing solution. Gang of Four.
05/26/2004www.indyjug.net1 Indy Java User’s Group June Knowledge Services, Inc.
Design Patterns CS is not simply about programming
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
(c) 2010 University of California, Irvine – André van der Hoek1June 29, 2015 – 08:55:05 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Design Patterns Alan Shalloway, James Trott, Design Patterns Explained, Addison-Wesley, Gamma, Helm, Johnson, Vlissides, Design Patterns, Elements.
Design Patterns.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
CSSE 374: Introduction to Gang of Four Design Patterns
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
1 Unit 5 Design Patterns: Design by Abstraction. 2 What Are Design Patterns?  Design patterns are: Schematic descriptions of design solutions to recurring.
January 12, Introduction to Design Patterns Tim Burke References: –Gamma, Erich, et. al. (AKA, The Gang of Four). Design Patterns: Elements of Reusable.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
DAAD project “Joint Course on OOP using Java” On Object Oriented modeling in Java (Why & How) Ana Madevska Bogdanova Institute of informatics Faculty of.
DESIGN PATTERNS CSC532 Adv. Topics in Software Engineering Shirin A. Lakhani.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.
ECE450S – Software Engineering II
2007ACS-3913 Ron McFadyen1 Class Diagram See Schaum’s UML Outline, especially chapters 4, 5, 6, 7.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Design Pattern Catalog - Page L3-1 PS95&96-MEF-L10-1 Dr. M.E. Fayad Creationa.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
CS 210 Introduction to Design Patterns August 29, 2006.
Pengantar Temu Balik Informasi Dosen: Hapnes Toba Ruang: R. Dosen Lt. 8 GWM.
CS 210 Review October 3, 2006.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
It started with a simple … A highly successful duck pond simulation game called SimUDuck The game can show a large variety of duck swimming and making.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
CS 210 Introduction to OO Design August 24, 2006
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
7 April 2004CSci 210 Spring Design Patterns 2 CSci 210.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
SE 461 Software Patterns Welcome to Design Patterns.
Intro to Design Pattern
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
object oriented Principles of software design
Strategy Design Pattern
Informatics 122 Software Design II
Informatics 122 Software Design II
Software Design Lecture : 27.
Presentation transcript:

IN 076 Pola Desain Perangkat Lunak Dosen: Hapnes Toba Oscar Karnalim Asisten: Lucky Christiawan Teori: Jumat Lab ADV 1 Praktikum: Jumat – Lab ADV 1

Referensi Pendukung 1.Eric Freeman & Elisabeth Freeman; Head First Design Patterns; O’Reilly; Allen Holub; Holub on Patterns: Learning Design Patterns by Looking at Code; Apress; Christopher G. Lasater; Design Patterns; Wordware Publishing Inc.; Erich Gamma, et.al.; Design Patterns: Elements of Reusable Object Oriented Software; Addison-Wesley Intl.; James W. Cooper; Introduction to Design Patterns in C#, IBM TJ Watson Research Center; Jason McDonald; Design Patterns, DZone Refcards; Metsker, Steven John, William C. Wake, Design Patterns in Java 2nd ed., Addison- Wesley Professional, Metsker, Steven John, Design Patterns in C#, Addison-Wesley Professional, Steve Holzner, PhD.; Design Patterns for Dummies; Wiley Publishing, Inc.; etc

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 Template (Ch. 8), Singleton (Ch. 5) Review Materi (1-6) Pertemuan x (Jumat Agung) 25 Maret 2016 Quis 2 + Kuis Besar (take home) Ujian Tengah Semester 28 Maret - 8 April 2016 UTS (Bahan pertemuan 1-6)

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 Mei 2016 Proxy (Ch. 11), Compound (Ch. 12) Pertemuan Mei 2016 Patterns in the real world (Ch. 13) Pertemuan Mei 2016 Left over patterns (Ch. 14): Bridge, Builder, Chain of Responsibility, 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 (2x): 15% Rata-rata keaktifan kelas: 15% Kuis besar: 20% Soal UTS : 50% UAS (30% dari total) – jika hadir Rata-rata kuis (2x): 15% Rata-rata keaktifan kelas: 15% Kuis besar: 20% Soal UTS : 50% KAT (40%) – tugas-tugas per pertemuan Sesi praktikum

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 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?

Three pillars of OO Design Encapsulation InheritancePolymorphism OO Design Design Pattern Best practices

Group Discussion (Collaborative Learning) Encapsulation InheritancePolymorphism OO Design 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 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

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 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 a “Medication” class object private String: Name private int: Dosage private String: Route private String: Form Medication public String getName() public int getDosage() public String getRoute() public String getForm() public void setName() public void setDosage() public void setRoute() public void getForm() getters setters Name Dosage Route Form getName getDosage getRoute getForm setName setDosage setRoute setForm

Inheritance Dealing with Object Hierarchies

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 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 print object display object Word object print object display object new object print object display object

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

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?

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

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

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

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

Introduction to Design Patterns Chapter 1 Strategy Pattern

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 MallardDuck display() // looks like mallard RedheadDuck display() // looks like redhead Other duck types

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

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

Example complicated: add a wooden decoy ducks to the mix DecoyDuck 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. Duck swim() display() // other duck methods MallardDuck display() fly() quack() Quackable quack() Flyable fly() RedheadDuck display() fly() quack() RubberDuck display() quack() DecoyDuck display() Interfaces

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 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… Duck Behaviors Parts that vary Parts that stay the same

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(); d.bark(); Programming to an interface Animal animal = new Dog(); animal.makesound();

Duck simulation recast using the new approach MallardDuck display() RedHeadDuck display() RubberDuck display() DecoyDuck display() Duck FlyBehavior: flyBehavior QuackBehavior: quackBehavior performQuack() performFly() setFlyBehavior() setQuackBehavior() swim() display() > FlyBehavior fly() FlyWithWings fly() // implements duck flying FlyNoWay fly() // do nothing – Can’t fly > QuackBehavior quack() Quack quack() // implements duck quacking Squeak quack() // implements squeak Mutequack quack() // do nothing

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 IS-A Allows changing behavior at run time

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 WeaponBehavior weapon; fight(); KnifeBehavior useWeapon() //implements cutting with // a knife BowAndArrowBehavior useWeapon() //implements fight with // bow and arrows AxeBehavior useWeapon() //implements fight with // an axe > WeaponBehavior useWeapon() Queen fight() King fight() Knight fight() Bishop fight() SpearBehavior useWeapon() //implements fight with // a spear setWeapon(WeaponBehavior w){ this.weapon = w; }

Character WeaponBehavior weapon; fight(); KnifeBehavior useWeapon() //implements cutting with // a knife BowAndArrowBehavior useWeapon() //implements fight with // bow and arrows AxeBehavior useWeapon() //implements fight with // an axe > WeaponBehavior useWeapon() Queen fight() King fight() Knight fight() Bishop fight() SpearBehavior useWeapon() //implements fight with // a spear setWeapon(WeaponBehavior w){ this.weapon = w; } Abstract

Character WeaponBehavior weapon; fight(); KnifeBehavior useWeapon() //implements cutting with // a knife BowAndArrowBehavior useWeapon() //implements fight with // bow and arrows AxeBehavior useWeapon() //implements fight with // an axe > WeaponBehavior useWeapon() Queen fight() King fight() Knight fight() Bishop fight() SpearBehavior useWeapon() //implements fight with // a spear setWeapon(WeaponBehavior w){ this.weapon = w; } Abstract Behavior Interface

Character WeaponBehavior weapon; fight(); KnifeBehavior useWeapon() //implements cutting with // a knife BowAndArrowBehavior useWeapon() //implements fight with // bow and arrows AxeBehavior useWeapon() //implements fight with // an axe > WeaponBehavior useWeapon() Queen fight() King fight() Knight fight() Bishop fight() SpearBehavior useWeapon() //implements fight with // a spear setWeapon(WeaponBehavior w){ this.weapon = w; } Abstract Behavior Interface

KnifeBehavior useWeapon() //implements cutting with // a knife BowAndArrowBehavior useWeapon() //implements fight with // bow and arrows AxeBehavior useWeapon() //implements fight with // an axe > WeaponBehavior useWeapon() Queen fight() King fight() Knight fight() Bishop fight() SpearBehavior useWeapon() //implements fight with // a spear Abstract Behavior Interface Character WeaponBehavior weapon; fight(); setWeapon(WeaponBehavior w){ this.weapon = w; }

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