Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

But Uncle Bob: Abstract Factory

 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic