Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help understanding Abstract Factory from a factory Example

 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose i have the following Factory example


I totally understand the factory but am confused with what an abstract factory would be like ?? Any example would be appreciated..
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


All object factories above will be able to execute the hetso909Rule(). If this method should ever need to be modifed, it will only be modified in a single location.
 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the example.. However a few questions
1)Why the need of a AbstractDog cant ConcreteDog impliment Dog ??
2)Why place an abstract with the interface isnt an interface itself abstract ??
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)Why the need of a AbstractDog cant ConcreteDog impliment Dog ??


This is only an example. As you might see, there are many different ways to design. In my example, there is a possibility that certain common behaviors might need to be implemented in the future. Having an AbstractDog class in the middle is good design and may turn out to be useful especially if there will be multiple ConcreteDog classes needed in the future. This example is designed upfront to support future changes with minimal impact, testing, etc.


2)Why place an abstract with the interface isnt an interface itself abstract ??

Why not?
 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing that up.. regarding the second question I just wanted to know if its possible to ommit the abstract keyword with the interface. Will it make any difference ? Thanks again for the great example
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome. Yes, the abstract keyword may be ommitted from the source code. The language designers were flexible. But, things are different inside the JVM and before compiling your class, the code signal for abstract will be added to the compiler input. Will it make any difference? Not one that a human can detect. However, always remember that an interface class ISA type of abstract class.
 
Radoslaw Maruszewski
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jimmy Clark wrote:You are welcome. Yes, the abstract keyword may be ommitted from the source code. The language designers were flexible. But, things are different inside the JVM and before compiling your class, the code signal for abstract will be added to the compiler input. Will it make any difference? Not one that a human can detect. However, always remember that an interface class ISA type of abstract class.


There's no real difference between "abstract interface" or "interface". I'd recommend to always use "interface", as it results in cleaner and more readable code.

Jimmy Clark wrote:Having an AbstractDog class in the middle is good design and may turn out to be useful especially if there will be multiple ConcreteDog classes needed in the future.


Creating scaffolding for classes that may be needed in the future is rarely a sign of a good design. Create what is needed now, and give yourself a possibility of extending this solution in the future. AbstractDog can be easily inserted to the class structure when necessary, so I'd postpone creation of this class until that time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic