• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help understanding Abstract Factory from a factory Example

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic