Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

Hi, Is there any pdf file available for all these design patters? Regards, Praveen
- Praveen
One thing is nagging me in your example. Your factory should be without parameters and creatComputer should accept the parameters, currently multile calls to createComputer will create same computer instead of ones with different configuration. Existing methods: public ServerFactory(String ram, String hdd, String cpu){ public Computer createComputer() { Should be changed to these methods with following signature. public ServerFactory() public Computer createComputer(String ram, String hdd, String cpu) Factory should be without parameters and createComputer should accept parameters.
- Vishwas
Hi Pankaj, I am not able to understand Abstract Factory Design Pattern perfectly.Can you please help me ?? In ComputerFactory class, getComputer() method is taking ComputerAbstractFactory as argument.Are you sure this is correct ?? In your client program i.e. TestDesignPatterns.java, To create pc, the code is " Computer pc = com.journaldev.design.abstractfactory.ComputerFactory.getComputer(new PCFactory(“2 GB”,“500 GB”,“2.4 GHz”));". It means client have access to class PCFactory. When client has access to class PCFactory, then pc can be created without using ComputerFactory class. PCFactory factory = new PCFactory(“2 GB”,“500 GB”,“2.4 GHz”); Computer pc = factory.createComputer(); What is the use of ComputerFactory class when client have direct access to PCFactory or ServerFactory??? Best Regards, Badri
- Badri
I do not feel the If - else logic is actually irradicated. Sorry, to spoil the fun. When i first read it, I felt at last something that explains a difference between Factory and abstract factory. The if-else is removed from the Factory code as in other examples you find here and many other link https://www.tutorialspoint.com/design\_pattern/abstract\_factory\_pattern.htm. The if-else logic is still present in the factory class. However, this seems to b worse. Actually the responsibility has shifted to client side. Imagine, at compile time you do not what the client requests and the code would look like. String computerType= getRequestFromClient(); Computer remoteComputer = null; //Now this is where the actual if else comes in. if(computerType.equals(“PC”)){ remoteComputer = ComputerFactory.getComputer(new PCFactory(“2 GB”,“500 GB”,“2.4 GHz”)); } else if(computerType.equals(“SERVER”)){ remoteComputer = ComputerFactory.getComputer(new ServerFactory(“16 GB”,“1 TB”,“2.9 GHz”)) } //And remember this code block would be present everywhere you want the remoteComputer object from the factory… This was the reason why this code was moved to Factory to not let client worry about it… Else why to go with so much of Fuzz just type in: if(computerType.equals(“PC”)){ remoteComputer = new PCFactory(“2 GB”,“500 GB”,“2.4 GHz”); } else if(computerType.equals(“SERVER”)){ remoteComputer = new Server(“16 GB”,“1 TB”,“2.9 GHz”)) } //This looks much cleaner. This is why I feel som many people are confused with Design patterns because everyone just explain the way they like it.
- Vinay Kallat
Good Article. But if you aren’t still convinced why this pattern is to be used, we can refer to another example. Say you are writing application code independent of OS . Like Google Chrome Code. Now there will be a factory class for Windows OS to generate Windows Buttons, Menus etc. Similarly there will be another factory class for Linux OS to generate its buttons, menus. Now application code instead of coding to any of these factory classes will code to its interface - AbstractFactoryClass for simplicity This class is used more than to prevent if/else in factory pattern
- Rajesh KSV
This is no correct abstract factory implementation, here client knows about the concrete factories and just passing to consumer. Client shouldn’t know about concrete factories or concrete computer implementations. Abstract factory only should know the actual concrete factories to be used.
- Kailash Singh
Hi Pankaj. A question. In your TestDesignPatterns , it looks like the client directly knows about PCFactory and ServerFactory. Is this correct? Isn’t this a kind of coupling between the client and the factory classes?
- Valentino
I think it is worth to add one more abstract product and bunch of end products which extends this one (two sets of products). Than it makes sens. https://en.wikipedia.org/wiki/Abstract\_factory\_pattern#/media/File:Abstract\_factory\_UML.svg
- Jacek Feliksiak
Your article and your example is very good and useful. I used some your example on my blog: https://stackjava.com and I place back link to your page. Because my students can not read english language… Please let me know if you feel bothered I will remove it. Thanks!
- kai