Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Factories and Interfaces Question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 3 questions on these 2 topics:
1.) Most examples I have seen use factory classes to create almost every concrete class in the system. Is there ever a time where you don't need a factory to create a class or it is not a good idea?
2.) Same as the factory classes, most examples I have seen create an interface class for every concrete class in the system. Is there ever a time where this is not needed or not a good idea?
3.) The Factory and Abstract Factory patterns seem to require that concrete classes inherit from an abstract base class. Is this is a hard requirement or will implementing an interface instead of extending the abstract class abide by the pattern rules as well?
Thanks
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very good questions as I had similar questions in my mind sometime back.
1.) Most examples I have seen use factory classes to create almost every concrete class in the system. Is there ever a time where you don't need a factory to create a class or it is not a good idea?
Well this is a good design philosophy, which you will see in most systems being built. This is a pattern which a) separates the construction of being scattered across multiple places in code
b)sometimes an appropriate class is instantiated and cast to an interface and returned. (combination of Factory and a Manager). This way client code is shielded with nitty-gritty details and is simply a nice layer to your application. So if you have a code change you have only one place.
2.) Same as the factory classes, most examples I have seen create an interface class for every concrete class in the system. Is there ever a time where this is not needed or not a good idea?
pretty much as explained above.
3.) The Factory and Abstract Factory patterns seem to require that concrete classes inherit from an abstract base class. Is this is a hard requirement or will implementing an interface instead of extending the abstract class abide by the pattern rules as well?
no hard and fast rules as such....Abstract Factories are not typically used all that often in fact. I personally like implementing an interface (u can implement multiple interfaces ...not stuck with one.....
 
Rupert Rowan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.
I am working on the part II assignment of course, and my class diagram is quickly getting filled with factories and interfaces.
I was thinking of leaving some out for classes I have hidden behind a Session Facade. I figured maybe this was ok, since what I want is for all references to the business classes to be done via the facade. But it sounds like I should just go ahead and implement the factories and interfaces to make sure I don't risk losing points.
Thanks again.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The real practical use of Factory pattern is when it is used to instantiate objects of unknown classes. Many scenarios have requirements where functionality needs to be plugged in and the classes which implement new code are unknown..only contract is that they implement a known interface.
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic