Design Patterns. Design Patterns 1. Outline. Design Patterns. Bicycles Simulator 10/26/2011. Commonly recurring patterns of OO design

Similar documents
Interface. Design Patterns 2. Why use Interface? Design Principles. Adapter 11/7/2011. Michael Li

Object-Oriented Design

Java Object Oriented Design. CSC207 Fall 2014

What are the characteristics of Object Oriented programming language?

Lecture 2: Java & Javadoc

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Exercise: Singleton 1

Chapter 5 Object-Oriented Programming

Data Structures (list, dictionary, tuples, sets, strings)

Person class. A class can be derived from an existing class by using the form

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

Person class. A class can be derived from an existing class by using the form

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Introduction to Object-Oriented Programming

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Advanced Object Oriented PHP

11/12/12. Objectives DESIGN PATTERNS. Design Pattern. Defined Design Patterns. Applying Design Patterns. Motivating Example

Design Pattern and Software Architecture: IV. Design Pattern

Polymorphism and Interfaces. CGS 3416 Spring 2018

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

An Introduction to Patterns

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

SDC Design patterns GoF

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Subclasses, Superclasses, and Inheritance

Programming II (CS300)

Computer Science 210: Data Structures

Chapter 5. Inheritance

The Essence of Object Oriented Programming with Java and UML. Chapter 2. The Essence of Objects. What Is an Object-Oriented System?

CSE 70 Final Exam Fall 2009

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Building custom components IAT351

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Programming II (CS300)

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

8. Interfaces & Inner Classes

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

Object Orientated Analysis and Design. Benjamin Kenwright

ITI Introduction to Computing II

The Strategy Pattern Design Principle: Design Principle: Design Principle:

Design Patterns. "Gang of Four"* Design Patterns. "Gang of Four" Design Patterns. Design Pattern. CS 247: Software Engineering Principles

Introduction to Design Patterns

CS 247: Software Engineering Principles. Design Patterns

OBJECT ORİENTATİON ENCAPSULATİON

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

Outline of today s class. Design Patterns. Classes and Inheritance. Aside: UML Class Diagrams

Introduction and Review of Java: Part 2

ECE 3574: Dynamic Polymorphism using Inheritance

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Chapter 11 Object and Object- Relational Databases

ITI Introduction to Computing II

Design Patterns Revisited

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Object-Oriented Design

LABORATORY 1 REVISION

6.170 Lecture 15 Design Patterns

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Accessibility. EEC 521: Software Engineering. Classes and Objects. Inheritance. Classes and Objects (OO Analysis)

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

Object-Oriented Design

Universiti Malaysia Perlis

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Introduction to Software Engineering: Object Design I Reuse & Patterns

SOFTWARE PATTERNS. Joseph Bonello

CS-202 Introduction to Object Oriented Programming

Object- Oriented Design with UML and Java Part I: Fundamentals

Self-review Questions

CS200: Advanced OO in Java

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

Inheritance. The Java Platform Class Hierarchy

Topics in Object-Oriented Design Patterns

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

Introduction to OO Concepts

Programmazione. Prof. Marco Bertini

Instance Members and Static Members

The GoF Design Patterns Reference

Object-Oriented Design

Testing Object-Oriented Software. COMP 4004 Fall Notes Adapted from Dr. A. Williams

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

COURSE 2 DESIGN PATTERNS

Introduction to Object- Oriented Programming

Object-Oriented Programming Paradigm

CS342: Software Design. November 21, 2017

CS Week 15 Page 1

Inheritance and Encapsulation. Amit Gupta

4.1 Introduction Programming preliminaries Constructors Destructors An example... 3

Chapter 10 Classes Continued. Fundamentals of Java

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Inheritance (Outsource: )

Principles of Object Oriented Programming. Lecture 4

Transcription:

G52APR Applications Programming Design Patterns 1 Design Patterns What is design patterns? The design patterns are languageindependent strategies for solving common object-oriented design problems. Jiawei Li (Michael) email: jwl@cs.nott.ac.uk http://www.cs.nott.ac.uk/~jwl/g52apr 2 Design Patterns How many design patterns? Many. Why use design patterns? Solutions to complex problems Code reuse To be a good Java developer Outline Commonly recurring patterns of OO design A simple example Adapter pattern Composite pattern Solving complex problems 3 4 Commonly recurring patterns of OO design s Simulator Creational Patterns Structural patterns Behavioural patterns J2EE patterns o Adaptor o Composite o Strategy o Decorator o Observer o Factory method o Iterator 5 Various bicycles Mountain bicycles Road bicycles Child bicycles Various actions Speed up Changing gear 6 1

s Inheritance class Many different bicycles Classic case for inheritance Base-class of, from which specialized classes inherit Road Mountain Child 7 8 class UML Notation Class Name Attribute Attribute Operation Operation Aggregation UML Representation for a Class UML stands for Unified Modeling Language Inheritance Composition 9 10 class Inheritance class Inheritance Road Mountain Child Superclass Subclasses Abstract classes: cannot be instantiated. Abstract methods: without an implementation. 11 // source file name bicycle.java public abstract class bicycle{ private int gear; private boolean front_brake; private boolean rear_brake; public void setgear(int newvalue); public void getmaximumspeed(); public class road_bicycle extends bicycle{ public class mountain_bicycle extends bicycle{ 12 2

Inheritance summary A class inherits fields and methods from all its superclasses, whether direct or indirect. A subclass can override methods that it inherits, or it can hide fields or methods that it inherits. An abstract class can only be subclassed; it cannot be instantiated. An abstract class can contain abstract methods. You can prevent a class from being subclassed by using the final keyword in the class's declaration. Similarly, you can prevent a method from being overridden by declaring it as a final method. s Simulator v2 More features of bicycles Foldable With motor 13 14 s Simulator v2 s Simulator v2 public abstract class bicycle{ public void setgear(int newvalue); public void setfold(boolean newstate ); public void setmotor(double newspeed); Some features are not appropriate for all subclasses of. If those features are implemented in the methods of superclass, subclasses inherit inappropriate behaviour. Need to override the methods. Is Inheritance the right model here? 15 16 Inheritance Disabling operations on derived classes Maintenance problem Every new bicycle subclass will require you to consider overriding setfold()/setmotor() Danger of not overriding methods on new bicycles Interface Does the behaviour belong in the baseclass in the first place? Put it in an interface Interfaces are a collection of abstract methods that an Object implements Object can support multiple interfaces (no multiple inheritance!) 17 18 3

Object Interface Client can ask an object if it supports an interface. If it does it can use the interface, if not try something else. Interfaces are declarations, they contain no code to implement methods. Usage of interfaces interface foldable { void setfold(boolean newstate); interface with_motor { void setmotor(double newspeed); class mybike1 extends bicycle implements foldable { public void setfold(boolean newstate){ class mybike2 extends bicycle implements foldable, with_motor { 19 20 class hierarchy Duplicate Code Interfaces have no code Each bicycle subclass must have its own implementation for each interface. Any change to an implementation must be made to every sub-class that supports it 21 22 Adapter The intent of Adapter is to provide the interface that a client expects while using a existing class. A new subclass of : _without_pedals interface no_pedal { void setnopedal(); class mybike1 extends bicycle implements no_pedal { 23 ExistingClass UsefulMethod() Adapter NewClass RequiredMethod() <<interface>> RequiredMethod() When a developer of client code thoughtfully defines the client s needs, you may be able to fulfill the interface by adapting existing code. 24 4

Object Adapter Object Adapter example RequiredClass RequiredMethod() ExistingClass UsefulMethod() NewClass RequiredMethod() ExistingClass is instantiated in NewClass so that UsefulMethod can be used. It can be dangerous if you don t override all the methods that might be called. 25 26 Adapter summary The Adapter pattern lets you use an existing class to meet new requirements. Class adapter: create new class that implements new interface and subclasses an existing class. Object adapter: create a new client subclass that uses an instance of the existing class. Change Change is the one constant in software development Don t make rigid designs Changing something should be easy and require the least amount of effort 27 28 Design Principle Identify the aspects of your application that vary and separate them from what stays the same Design Principle Some parts of the application are varying with new requirements, others don t Encapsulate the bits that change into a new object so that it can be changed easily This is at the heart of every design pattern 29 30 5

Changing s Thought we had a good design for our bicycle simulator But changing features may require major changes How do we make it a good design? (re)design class seems to work Apart from methods e.g. setfold() Separate these methods off into new classes or interfaces Foldable behaviour With Moter behaviour Without Pedal behaviour... 31 32 Behaviour Keep things flexible Instances of are assigned behaviours Can we change a behaviour dynamically? To make a foldable bicycle non-foldable? Mutators to change s behaviour at runtime Design Principle Program to an interface, not an implementation Mutators: setter() methods 33 34 Program to an interface Behaviour Interfaces Interface A{ method() ; Class B implement A { method()...; // program 1: Class X { private A a1; public void usea (A b1){ a1 = b1; a1.method(); // program 2: Class X { private B b1; public void useb(){ b1.method(); FoldableBehaviour and WithMotorBehaviour Gives us flexibility Don t inherit any implementation s aren t tied to an implementation 35 36 6

Foldable setfold() { /* Fold the bike */ <<interface>> foldable setfold() Non-foldable setfold() { /* Do nothing */ Integrating Behaviours Interface type don t want to bind this to an implementation Add instance variables for the behaviours FoldableBehaviour WithMotorBehaviour Add methods to call the behaviour 37 38 public abstract class { protected FoldableBehaviour fordable; protected WithMotorBehaviour with_motor; public class mybike extends { public FoldableBehaviour getfordable { return this.fordable; public void setfordable(foldablebehaviour state) { fordable = state.setfold(); ; What is happening? When mybike is created: Creates objects for correct behaviours Sets variables to instantiate existing classes (interfaces) When setfoldable() called, setfold() is called on the newly created object Equivalent for setmotor() 39 40 Strategy Pattern Example of the Strategy Pattern 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 Exercise interface Monster { void menace(); interface DangerousMonster extends Monster { void destroy(); interface Lethal { void kill(); class Zombie implements DangerousMonster { public void menace() {// some code here public void destroy() {// some code here interface Vampire extends DangerousMonster, Lethal { void drinkblood(); class VeryBadVampire implements Vampire { public void menace() {// some code here public void destroy() {// some code here public void kill() {// some code here public void drinkblood() {// some code here ** You need create three new classes: Monster1: is a zombie and has kill() method Monster2: has the methods of kill(), destroy(), and a new method fly() Monster3: is a VeryBadVampire but does not have kill() method 41 42 7

Reading ErichGamma, RichardHelm, RalphJohnson, and JohnVlissides (the Gang Of Four), Design Patterns: Elements of Reusable Object-Oriented Software, Addison Wesley Professional, 1994 Steven J. Metsker and William C. Wake, Design Patterns in Java, Addison Wesley, 2006 http://www.javacamp.org/designpattern/ 43 8