adapter, factory, and more...static “factory” methods keep ... java’s stringtokenizer class...
TRANSCRIPT
![Page 1: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/1.jpg)
Making the Concrete Abstract
Adapter, Factory, and More
1
![Page 2: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/2.jpg)
Factory Pattern: MotivationCorrectly 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
![Page 3: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/3.jpg)
Factory is the answer
Actually a collection of patterns Simply factory (idiom) Factory MethodAbstract Factory (not today)
3
Concrete class; Mysteryhow tomake one
![Page 4: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/4.jpg)
2¢ Factory for Dating Events
4
static “Factory” methods keep Event details local
…
![Page 5: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/5.jpg)
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
![Page 6: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/6.jpg)
Simple Factory (idiom)
6
How to makeEvents not necessarily part of Event
![Page 7: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/7.jpg)
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
![Page 8: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/8.jpg)
Class Diagram for Simple Factory
8
![Page 9: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/9.jpg)
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
![Page 10: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/10.jpg)
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 EventFactoryinterface, 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
![Page 11: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/11.jpg)
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()
…
![Page 12: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/12.jpg)
Factory Method Style: Separate Factory
Separate Factory like SEF but in hierarchy with abstract super
Super centralizes Event selector
Subs implement making12
![Page 13: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/13.jpg)
Factory Motivation for Pizza Example: Franchising
13
![Page 14: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/14.jpg)
Factory Method Class Diagram
14
“composes” them all because their constructors are referenced
![Page 15: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/15.jpg)
Object Relationships
15
![Page 16: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/16.jpg)
Adapter PatternAgain? Composition and Delegation, again?
16
![Page 17: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/17.jpg)
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
![Page 18: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/18.jpg)
Adapter: Example
18
![Page 19: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/19.jpg)
Adapter: Class Diagram
All the classic DP elements: Client is “open” because it composes interface Adapter implements interface, delegates to concrete class
19
![Page 20: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/20.jpg)
Adapter: Exercise Java’s StringTokenizer class breaks
a string into words using a specific algorithm
Oddly, it implements the Enumeration<E> interface instead of Iterator<E> 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<E> {boolean hasMoreElements();E nextElement();
}
interface Iterator<E> {boolean hasNext();E next();
}
20
![Page 21: Adapter, Factory, and More...static “Factory” methods keep ... Java’s StringTokenizer class breaks a string into words using a specific algorithm Oddly, it implements the Enumeration](https://reader036.vdocuments.mx/reader036/viewer/2022071117/600a6c11a5a7c110a7195ab6/html5/thumbnails/21.jpg)
Adapter Exercise (solution)
21
USE:Iterator<String> iter = new EnumerationIterator<String>(myStringTokenizer);while(iter.hasNext()) { String str = iter.next(); System.out.print(str + " "); }