aspose file tools
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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Reply Bookmark "But Uncle Bob: Abstract Factory" Watch "But Uncle Bob: Abstract Factory" New topic
Author

But Uncle Bob: Abstract Factory

Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2906
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.


"Don't succumb to the false authority of a tool or model. There is no substitute for thinking."
Andy Hunt, Pragmatic Thinking & Learning: Refactor Your Wetware p.41
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

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
Ranch Hand

Joined: Jul 11, 2001
Posts: 15230

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?
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2906
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: But Uncle Bob: Abstract Factory
 
Similar Threads
What's a factory?
Factory class
What is the difference between AbstractFactory and Factory pattern
Interview Question
Java Pattern