This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes But Uncle Bob: Abstract Factory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "But Uncle Bob: Abstract Factory" Watch "But Uncle Bob: Abstract Factory" New topic

But Uncle Bob: Abstract Factory

Peer Reynders

Joined: Aug 19, 2005
Posts: 2922
Abstract Factory by Daniel T

... kind of reminds me of Herb Sutter's Conversations (with a Guru) column and of course Uncle Bob's The Craftsman articles - but then again it's probably supposed to.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

I love it!

One thing it captures is the too-often-unclear idea that design patterns are ways to capture best practices, things that people already do. I also like the point at the end about the purpose of design patterns as a communication tool.

[Jess in Action][AskingGoodQuestions]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299

I like the story. I'm still having a hard time grasping how the code is better now. But it may be because some of the code was left to the imagination. I'm assuming the reason it's better is because the code that normally passed in "A" or "B" is now passing in the paserFactory. But wouldn't the code that passes that in then have the IF/ELSE? Or are we thinking 2 different pieces of code are doing this?

GenRocket - Experts at Building Test Data
Peer Reynders

Joined: Aug 19, 2005
Posts: 2922
This is the part that had me scratching my head:

if self.param = "A":
self.preParser = PreParserA()
self.parser = ParserA()
elif self.param = "B":
self.preParser = PreParserB()
self.parser = ParserB()

John said, "I tried that, the user rep. said that it took too long for
the code to start parsing that way."

The conceit is that it takes so long to create the Parser that it leaves the user impatiently waiting.

Once you accept that, the rest is pretty standard. First you have two IFs:

These IFs are replaced by a single one:

No further IFs are needed as they have fallen victim to the Replace Conditional with Polymorphism refactoring. In other words there are no further decisions to be made because each concrete factory knows exactly and unambiguously what needs to be done.
It is sorta covered in the JavaRanch Style Guide.
subject: But Uncle Bob: Abstract Factory
Similar Threads
What's a factory?
Java Pattern
What is the difference between AbstractFactory and Factory pattern
Interview Question
Factory class