Making the Concrete Abstract Adapter, Factory, and More 1.

Slides:



Advertisements
Similar presentations
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Advertisements

The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Design Patterns CS is not simply about programming
ADAPTER PATTERN Ali Zonoozi Design patterns course Advisor: Dr. Noorhoseini Winter 2010.
Prototype Pattern Intent:
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
More OOP Design Patterns
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
CS 4240: Introduction to Design Patterns Readings: Chap. 5 of Design Patterns Explained Also, Chap. 6 and 7 Also, on-line info on Composite pattern (and.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design Patterns.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
Creational Patterns (1) CS350, SE310, Fall, 2010.
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.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
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 Façade, Singleton, and Factory Methods Team Good Vibrations (1)
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Week 2, Day 2: The Factory Method Pattern Other good design principles Cohesion vs. Coupling Implementing the Strategy Pattern Changing strategies (behaviors)
By Shishir Kumar Contact:
Structural Design Patterns
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IV Structural Patterns.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Chapter 7: The Adapter Pattern. Object Oriented Adapters Suppose that you have existing software. You have outsourced some of your work and there is a.
FacadeDesign Pattern Provide a unified interface to a set of interfaces in a subsystem. Defines a high level interface that makes the subsystem easier.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Design Reuse Earlier we have covered the re-usable Architectural Styles as design patterns for High-Level Design. At mid-level and low-level, design patterns.
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.
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.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
F-1 © 2007 T. Horton CS 4240 Principles of SW Design More design principles LSP, OCP, DIP, … And another pattern Decorator.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
Decorator Pattern ¢ Design: Which classes must be included?  Story As a user I want a weekend planner so that I can have more fun  Scenario Given.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
Façade and the DP Wrap-Up 1. Which pattern does this class diagram from the Factory chapter call out for? A.Strategy B.Decorator C.Adapter D.Factory 2.
Design Patterns: MORE Examples
Abstract Factory Pattern
Design Patterns Spring 2017.
Chapter 10 Design Patterns.
MPCS – Advanced java Programming
object oriented Principles of software design
Abstract Factory Pattern
Factory Method, Abstract Factory, and More
Adapter, Fascade, and More
Intent (Thanks to Jim Fawcett for the slides)
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
08/15/09 Design Patterns James Brucker.
Object Oriented Design Patterns - Structural Patterns
Structural Patterns: Adapter and Bridge
Presentation transcript:

Making the Concrete Abstract Adapter, Factory, and More 1

Factory Pattern: Motivation  Correctly making objects is complex  Especially making collection of related objects  Parts of a car  Look-and-feel of a window: canvas, scrollbar, etc.  The correct making of objects is not easily centralized in one place  Often do it all over code wherever object is needed  Violates SRP and DRY  DP angle is that “new” names and makes a concrete class ; should be referring to abstract  concrete class has to be named, but we can at least hide that from most of system  encapsulating class will violate OCP, but no others will 2

Factory is the answer  Actually a collection of patterns  Simply factory (idiom)  Factory Method  Abstract Factory (not today) 3 Concrete class; Mystery how to make one

2 ¢ Factory for Dating Events 4 static “Factory” methods keep Event details local …

What’s wrong with 2 ¢ Factory? A. Doesn’t hide construction details adequately B. Should pass an enum argument rather than defining multiple methods: Event.makeEvent(SEEMOVIE) C. Violates the Open/Closed principle D.“make” methods shouldn’t be static E.Name prefix should be “create”, not “make” 5

Simple Factory (idiom) 6 How to make Events not necessarily part of Event

Comparison – increasingly abstract class MyDatingClass { … Event event = new Event( 2, “OrderFlowers” ); // magic constants // concrete Event class (majorly violates OCP) Event event = Event.makeOrderFlowers(); // abstract class object (passed into constructor) Event event = factory.createEvent(FLOWERS); 7

Class Diagram for Simple Factory 8

What’s wrong with Simple Factory? A.create method has to know about all the concrete classes it new’s B.Client class composes concrete class C.Factory is a concrete class 9

Simple Factory is Good, but not OCP  SimpleEventFactory should implement EventFactory interface  Clients of SEF should then compose EventFactory: class DateClass { private EventFactory factory; public DateClass( EventFactory factory) { this.factory = factory; } …  Someone can implement a new class with EventFactory interface, and DateClass can use it with no modification  Means that when EventFactory is extended (new subclass), DateClass is extended with new EventFactory options.  That’s open for extension ! 10

Factory Method Style: Date as Factory  Subclasses of Date are the factories!  In this example, no parallel Date/Event class hierarchy  Event hierarchy is “flat” – class hierarchy is implicit in data of Event  What changes is time to first “flowers” event, what the flowers are, etc. 11 ____Date____ createEvent() ____Youth____ createEvent() ____Senior___ createEvent() …

Factory Method Style: Separate Factory  Separate Factory like SEF but in hierarchy with abstract super  Super centralizes Event selector  Subs implement making 12

Factory Motivation for Pizza Example: Franchising 13

Factory Method Class Diagram 14 “composes” them all because their constructors are referenced

Object Relationships 15

Adapter Pattern Again? Composition and Delegation, again? 16

Adapter: Motivation 17 Real-world examples: Database connectors Chat systems Adapter: Uses Composition and Delegation Adapter (wrapper) is not subclass of wrapped object (because we’re adapting) Naming: InputStreamToReaderAdapter

Adapter: Example 18

Adapter: Class Diagram  All the classic DP elements:  Client is “open” because it composes interface  Adapter implements interface, delegates to concrete class 19

Adapter: Exercise  Java’s StringTokenizer class breaks a string into words using a specific algorithm  Oddly, it implements the Enumeration interface instead of Iterator interface  Need to pass the StringTokenizer to a class that requires (takes) an Iterator  Write an adapter from Enumeration to Iterator  (Could do this for StringTokenizer, but that’s less reusable!)  interface Enumeration { boolean hasMoreElements(); E nextElement(); }  interface Iterator { boolean hasNext(); E next(); } 20

Adapter Exercise (solution) 21 USE: Iterator iter = new EnumerationIterator (myStringTokenizer); while(iter.hasNext()) { String str = iter.next(); System.out.print(str + " "); }

Which pattern does this class diagram call out for? A.Strategy B.Decorator C.Adapter D.Factory 22

Façade It’s all a…oh, nevermind. 23

Façade – variant of adapter  Not for matching incompatible interfaces, but for simplifying use  Hide unneeded operations  Combine low-level operations into convenient one  Combine several objects into one object  Android’s AnimationUtils is an example 24

Façade - Example  Composes three classes  Groups low-level operations into high-level  Façade doesn’t have to be interface (but if it does, it will be adapter, too)  Note: if low- level classes declared as interfaces, then becomes Strategy, too 25

Which pattern? Suppose we extended myCity to provide a graph of friend proximity over the whole day. We’d start with a simple display, but think we’ll eventually adopt a powerful library to support a glanceable, informative display. Which pattern should be applied? A.Strategy B.Decorator C.Adapter D.Façade E.Factory 26

Discussion  Adapter : graphing library probably incompatible with initial service  Façade : to hide complexities of the powerful libraries  Decorator : to add border, extra info, around graph view  Strategy : to give users choice of graph  Qualities of strategy pattern in Adapter also facilitate testing  Factory : making variants of display for different modes/views  Do all? Agile says wait-and-see (refactor), as DPs take time and add complexity  Still, should compose abstract types ( interfaces and abstract super classes ) in initial design  almost free  will save writing new testcases  sets the stage for quick use of DPs later 27

Limitations of Factory Method? A.YouthDate class depends on concrete Event class(es) – even if we subclass Event to YouthEvent B.Doesn’t cope with how the various parts (Events) of a Date compose – two dinners and no movie? C.Date classes know too much about events (violating SRP) – know details of making, like constants 28

Abstract Factory: Motivation  Need multiple cooperating methods  Sounds like a class!  Interface (or abstract superclass) and subclasses  Typically, each method is a factory method  Typically, each “part” is abstracted as an interface  NOTE: next example not equivalent to previous  Helps keep example simple 29

Abstract Factory - Example 30

Warning – simplified example  The examples today avoided the parallel class hierarchies by using the “flat” Event class  More complicated when you want the Product and Creator hierarchies to mirror each other  Still, essential idea of avoiding abstract classes referring to concrete classes  Key difference between Factory Method and Abstract Factory  Collection of related parts made to work together  Each gets own (factory) method  Aggregate these methods using a class, of course 31

What’s really wrong w/ Simple Factory?  Had said: Factory user doesn’t have to use Date class  Basically too flexible (for some applications)  Can use factory (& Event in general) in myDateClass A.Overkill for the job, just need a (factory) method, not a whole class B.Violates Open/Closed Principle C.It’s just fine as is D.Trick question: original answer was right 32

Simple Factory (idiom) 33