Design Patterns: A Place to get Design Ideas

Slides:



Advertisements
Similar presentations
The Singleton Pattern II Recursive Linked Structures.
Advertisements

SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Scala Actors -Terrance Dsilva.  Thankfully, Scala offers a reasonable, flexible approach to concurrency  Actors aren’t a concept unique to Scala.
Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
Template Design Pattern Kalim Baig. Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Inheritance Only not the good kind of inheritance where you discover your Great Aunt left you a yacht. The bad kind that involves obscure Java syntax.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Refactoring1 Improving the structure of existing code.
Lexi case study (Part 2) Presentation by Matt Deckard.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
In the wide garden, I am dizzy with flowers. I choose a small vase. Haiku is a simple kind of poetry. It has 17 syllables, 5/7/5. Read the example. Count.
Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.
As you arrive… FIRST: Grab the handout SECOND: Snarf the code for today’s class THIRD: Explain what’s going on in this code (It’s Example 1 in the code.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Refactoring1 Improving the structure of existing code.
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.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Introduction to Exceptions in Java CS201, SW Development Methods.
SE2811 Week 8 – Class 2 Re: Lab due tonight SE-2811 Slide design: Dr. Mark L. Hornick Much Content: Dr. Hornick Most Errors: Dr. Yoder 1.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Factory Method. Intent/Purpose Factory Method is used to deal with a problem of creating objects without specifying the EXACT class of object that we.
Summary prepared by Kirk Scott
Web Design & Development Lecture 9
Sections Inheritance and Abstract Classes
How to be a Good Developer
Chapter 10 Design Patterns.
Design Patterns Lecture part 2.
Low Budget Productions, LLC
Introduction to Design Patterns
Inheritance and Polymorphism
Software Design and Architecture
Plan for today Refactoring and Design Patterns
How to be a Good Developer
object oriented Principles of software design
Chapter 17 Abstract Factory
Section 5: HW6 and Interfaces
Programming Design Patterns
Section 5: HW6 and Interfaces
Informatics 122 Software Design II
Object Oriented Design Patterns - Creational Patterns
Improving the structure of existing code
The Object-Oriented Thought Process Chapter 02
OO Design with Inheritance
More About Inheritance & Interfaces
Recitation 9 October 28, 2011.
Section 5: HW6 and Interfaces
Week 5 Review & Announcement
Overview of C++ Polymorphism
Section 5: HW6 and Interfaces
Dependency Inversion principle
HFOOAD Chapter 5 Interlude
Threads and concurrency / Safety
Presentation transcript:

Design Patterns: A Place to get Design Ideas As you arrive We will be doing online classwork today, so be sure to get your laptop set up. Once you’ve done that, page through the Design Patterns book and check out a few.

After Class Today You’ll know how I intend you to use design patterns in your work in this class You’ll be able to code up Factory Methods You’ll be able to repeat some “rules” for avoiding concrete classes (if we have time) We’ll talk about Template Method

Patterns – The Good Common solutions to problems, observed in many different codebases. Never exactly the same each time Name large scale structural things Very often arise naturally (but it is good to study them anyway, as long as you keep the right attitude)

Design Patterns are not justifications for your design. Don’t be that guy Yeah, well my design used 10 design patterns including Visitor Composite and Intepreter so I’m pretty sure it’s just about as flexible as it can be. Design Patterns are not justifications for your design.

Final Note about Design Patterns Though we will be going through more patterns in class, we won’t be covering everything that you might find useful in your designs.

Factory Method

The goal of the factory methods is to encapsulate object creation, so that different subclasses can be returned without the caller needing to change.

Abstract Superclass (or Interface) abstract int funct1() abstract String funct2() abstract SomeAbstactType createSomething() Subclass 1 func1() func2() SomeAbstractType createSomething() Subclass 2 func1() func2() SomeAbstractType createSomething() Either to: 1. Construct a related class 2. Defer a choice to subclasses Example: Iterator

Is this a Factory Method? class DoSomething { public ArrayList<Integer> createEmptyNumberList() { return new ArrayList<Integer>(); } Yes. No. If it was a factory method it ought to be called emtpyNumberListFactory() No. For it to be a factory, both the class and the method should be abstract. No. Because the return type is concrete (that is it’s not the superclass of anything or an interface)

Is this a Factory Method? public Bet parseBet(String bet) { if (bet.contains("even")) return new OddEvenBet(OddEvenBet.BetOption.EVEN); if (bet.contains("odd")) return new OddEvenBet(OddEvenBet.BetOption.ODD); return null; } Yes. No. If it was a factory method it ought to be called betFactory() and not do any parsing No. For it to be a factory, both the class and the method should be abstract. No. Because the return type is concrete (that is it’s not the superclass of anything or an interface)

Problems Better when the only purpose of subclassing is not to override factory method Commonly connects parallel class hierarchies…which are not always a good thing

Do Not Follow Design Rules Blindly No variable should hold a reference to a concrete class (possible using Factory Method!) No class should derive from a concrete class No method should override an implemented method of any of its base classes Layer of abstraction Concrete Class Another Concrete Class

No variable should hold a reference to a concrete class No class should derive from a concrete class No method should override an implemented method of any of its base classes “Always make your variables return types and parameters generic. For example never use ArrayList<String> when List<String> would do.”

An Example Problem Imagine you’re defining a bunch of classes that analyze web pages. One might count the number of words, another might analyze image formats, a third might look for Javascript. But they all have the same basic structure. public void generateReport() { for(String url : pagesToAnalyze()) { //do proccessing for individual page } // do a big bunch of analysis // Print out the report