More Patterns CS 124. More Basic Patterns Patterns you’ve already seen (without knowing it) Observer / Listener Wrapper Composite Decorator / Filter Patterns.

Slides:



Advertisements
Similar presentations
JDBC Session 4 Tonight: Design Patterns 1.Introduction To Design Patterns 2.The Factory Pattern 3.The Facade Pattern Thursday & Next Tuesday: Data Access.
Advertisements

C15: Design Patterns Gamma,Helm,Johnson,Vlissides (GOF)
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Object-Oriented Analysis and Design
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Ownership Types for Object Encapsulation Authors:Chandrasekhar Boyapati Barbara Liskov Liuba Shrira Presented by: Charles Lin Course: CMSC 631.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
Chapter 26 Applying Gang of Four Design Patterns 1CS6359 Fall 2012 John Cole.
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
More OOP Design Patterns
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Object Oriented Software Development
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
CSSE 374: Introduction to Gang of Four Design Patterns
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
CSC 313 – Advanced Programming Topics. Design Pattern Intent  Each design pattern is a tool  Like all tools, have reason for being.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
CSCI-383 Object-Oriented Programming & Design Lecture 13.
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.
CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Gang of Four Patterns 23 total 15 useful How are they different from GRASP Patterns?
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
M1G Introduction to Programming 2 5. Completing the program.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
More Design Patterns From: Shalloway & Trott, Design Patterns Explained, 2 nd ed.
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Introduction to Object-Oriented Programming Lesson 2.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
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.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
Week 5, Day 3: Decorator Decorators Muddiest Point Tomorrow: Quiz on lab reading: web.msoe.edu/hasker/se2811/labs/5/ SE-2811 Slide design:
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
GoF Patterns (GoF) popo.
MPCS – Advanced java Programming
Common Design Patterns
Introduction to Design Patterns
Behavioral Design Patterns
object oriented Principles of software design
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Advanced Programming Behnam Hatami Fall 2017.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Interfaces.
Slide design: Dr. Mark L. Hornick
GoF Patterns Ch. 26.
Presentation transcript:

More Patterns CS 124

More Basic Patterns Patterns you’ve already seen (without knowing it) Observer / Listener Wrapper Composite Decorator / Filter Patterns you might be using already, or might want to use Strategy Facade Command

Observer / Listener Idea: Allow “Listeners” to be added to an object When “something happens” to that object, a certain method is called on each Listener Benefits: Event-driven programming  program in terms of reactions to events (good for interactive programs) Allows you to place event-handling logic in a separate class Allows you to have multiple listeners – no limit to what you can do when an “event” happens Example: in AWT/Swing, you handle events by adding Listeners In Java, there is the also the Observable class and Observer interface In real life, Publish-Subscribe or “Push” services e.g., you get an if there is an interesting news topic

> Listener event1Method() event2Method() … +addListener(Listener) ListenerImpl When a relevant event in the EventGenerator occurs, it will invoke the appropriate event method on all the listeners -listeners:List EventGenerator added

Wrapper Idea: Hide an object in another object Interact only with the outer object Benefits: Allows you to control access to original object Users can’t call the method of the original object directly You can only allow certain methods You can change the method calls (similar to Adapter) You can add / modify functionality Hide detail of original object Allows you to change implementation later Example: InventoryList contains a Vector, but you don’t interact with the Vector directly. InventoryList provides the getProduct() method, which internally calls the Vector, but it casts the result to Product, so the user gets a product type and doesn’t need to do cast himself

Immutability / ReadOnly Wrapper Immutable Address +ImmutableAddress (Address) +getAddress() : String Address +getAddress() : String +setAddress(String) Using a Wrapper to hide the setXXX() methods renders the enclosed object unmodifiable/immutable

Decorator Idea: “Chain” objects together Objects can add functionality to other objects (also hide) Chain functionality (Similar to Wrapper, but allows infinite chain of wrapping.) Benefits: Can be used to add or enhance functionality e.g., PrintStream adds print and println methods to simple Stream objects, ObjectInputStream adds readObject method. e.g., BufferedInputStream performs buffering for you Decorators can decorate different classes e.g., ObjectInputStream can be used with FileInputStream, the InputStream of Socket, ByteArrayInputStream, etc. Example: in Files, BufferedReader, PrintStream, ObjectInputStream, etc. are Decorators that can be applied to basic Readers / InputStreams

Hmm … they look the same Adapter, Wrapper, Decorator have very similar implementations generally they are about putting an object in another object with minor variations however, they vary mainly in their intent why they are putting the object in another object in the first place it is the intent that determines which pattern you are actually using not the implementation

Open-Close Principle A principle that is followed by many design patterns Open to Extension Closed to Modification The basic idea is that code should be open for adding new behaviour using composition or inheritance but closed to code modification prevents adding new bugs to old working code This can be seen with the Adapter and Decorator behaviour is added by using an instance within the class

Composite Idea: Container contains Components Container is-a Component itself Benefits: A container can contain another container, ad infinitum Easier to manage complexity you only need to see and handle the top-level containers Containers take care of handling their components Examples: in AWT/Swing, you can add Panels to panels. This allows infinite nesting of GUI elements In your paint-a-shape programs, you can make a Shape class that contains other Shapes

Command Idea: Encapsulate things-you-can-do into Command objects Fields specify details Kind of command can be a field (int or String) Or, you can use polymorphism Write an “Executor” class that can take individual Command objects and perform them accordingly use a switch or if-else chain if command type is int or String if you’re using polymorphism, you can call execute() method on Command, or use Visitor pattern (more advanced) Benefits: Allows you to treat commands/operations as objects  place them on queues, send them over the network, undo/redo queues/stacks Examples: In your games, you can send Move objects over the network to tell the server (or the other player) what kind of move the player wants to make

Strategy Idea: Put changeable logic in a separate class Pass an instance of that class into the function or object that uses the changeable logic Benefits: “Template” code for the main algorithm remains the same and doesn’t need to be recompiled Change logic by simply giving a different Strategy object Examples: Arrays.sort( Object[] array, Comparator c ) static utility method Comparator is an interface that has compare( Object a, Object b ) which returns whether a > b, a equals b, or a < b. To use, implement the Comparator interface, and define compare method appropriately. Then, pass instance of your new class as parameter in Arrays.sort You can use inner classes to define the class on-the-spot

Facade Idea: Create a class that acts as a “front” to another class or system Provide methods that you or your users need to use Translate these to method calls using the other class or system Benefits: Simplify programming interface for you and users of your code Enable you to use other “messier” code internally, without making it visible to programmers Allows you to swap back-end code in the future Examples: For your network programs, instead of writing directly to the OutputStream, create a class Communicator that has methods for sending just the kinds of data you need. e.g., sendGameMove( GameMove gm ) or receiveGameMove() Then, you can hide whether you are using Strings, or ObjectOutputStream, etc. Also, now only one class can write data to the stream. Thus, if you get an error, you know where to look

Combinations Usually Design Patterns are used in combination e.g. Factory can be used to create different Command object instances based on criteria e.g. this can be useful for Networking where the incoming data is in the form of raw bytes and you want to convert these to some form of Command object  If the byte stream starts with 0 create Command0 with the rest of the data as parameters  If the byte stream starts with 1 create Command1 with the rest of the data as parameters

Conclusion Patterns give the developer many possible ways of solving a problem gives you the benefit of tapping the experience of others Patterns however are not a magic bullet You can still shoot yourself in the foot if you choose the wrong pattern for your application It is important to weigh the pros and cons of different patterns take into account what the future holds for your application choose the pattern that can address both the above