Download presentation
Presentation is loading. Please wait.
Published byConstance Jackson Modified over 5 years ago
1
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.
2
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
4
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)
5
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.
6
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.
7
Factory Method
8
The goal of the factory methods is to encapsulate object creation, so that different subclasses can be returned without the caller needing to change.
9
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
10
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)
11
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)
12
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
13
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
14
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.”
15
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.