1
\$\begingroup\$

I reproduced my version of Abstract Factory Pattern in Java. I know it's redundant but can I get critical comments about my code. Is it really the classic implementation of the pattern or not?

/*
* The demonstration of Abstract Factory Pattern
* 
* Here the 'DocumentFactory' creates documents of types 
* 'Resume' or 'Letter' which are concrete classes responsible to
* create things like 'FancyResume' or 'CoverLetter'
* 
* The example is inspired by Wikipedia documentation of Abstract
* Factory.
*/
interface letter{
 void print();
}
class CoverLetter implements letter{
 @Override
 public void print(){
 System.out.println("Cover letter printed");
 }
}
class FancyLetter implements letter{
 @Override
 public void print(){
 System.out.println("Fancy letter printed");
 }
}
interface resume{
 void process();
}
class CorporateResume implements resume{
 @Override
 public void process(){
 System.out.println("Corporate resume processed");
 }
}
class FancyResume implements resume{
 @Override
 public void process(){
 System.out.println("Fancy resume processed");
 }
}
//Create 'Concrete' factories 
class LetterFactory{
 public letter getLetter(String type){
 switch(type){
 case "Fancy":
 return new FancyLetter();
 case "Cover":
 return new CoverLetter();
 }
 return null;
 }
}
class ResumeFactory{
 public resume getResume(String type){
 switch(type){
 case "Fancy":
 return new FancyResume();
 case "Corporate":
 return new CorporateResume();
 }
 return null;
 }
}
// create abstract factory
abstract class DocumentFactory{
 abstract LetterFactory letterFactory();
 abstract ResumeFactory resumeFactory();
}
class DocumentCreator extends DocumentFactory{
 @Override
 LetterFactory letterFactory(){
 return new LetterFactory();
 }
 @Override
 ResumeFactory resumeFactory(){
 return new ResumeFactory();
 }
}
class abstractfactory{
 public static void main(String args[]){
 DocumentCreator creator = new DocumentCreator();
 // create a fancy letter
 letter myLetter = creator.letterFactory().getLetter("Fancy");
 myLetter.print();
 // create a corporate resume
 resume myResume = creator.resumeFactory().getResume("Corporate");
 myResume.process();
 }
}
asked Apr 15, 2016 at 9:22
\$\endgroup\$
3
  • 3
    \$\begingroup\$ I don't see where you would benefit from this extra level of abstraction here. And I find it very confusing that some of your types have lower-case names. \$\endgroup\$ Commented Apr 15, 2016 at 9:41
  • \$\begingroup\$ Is this not a valid implementation of "Abstract Factory" pattern? \$\endgroup\$ Commented Apr 15, 2016 at 9:55
  • 5
    \$\begingroup\$ I think you might have been trapped by one of the more dangerous aspects of design patterns: When followed blindly, they encourage cargo-cult programming for the sake of using the pattern rather than actually trying to solve a problem. If we should review your code, we have to know what it's supposed to accomplish. Also note that we don't review hypothetical code here. \$\endgroup\$ Commented Apr 15, 2016 at 10:17

1 Answer 1

2
\$\begingroup\$

First, use standard Java conventions for interfaces names: not resume or letter but Letter and Resume

Second, I would define e.g. StyleDocumentsFactory API like this

interface StyleDocumentsFactory {
 Letter createLetter();
 Resume createResume();
}

and then implement FuncyStyleDocumentsFactory and CorporateStyleDocumentsFactory instances of this API.

So you will have 2 abstract factories (for creation group of object with common theme: funcy and corporate) with set of factory methods.

Instantiate appropriate StyleDocumentsFactory impl in your main method at the program beginning.

answered Apr 15, 2016 at 16:02
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.