{"id":2370,"date":"2023-08-31T14:00:27","date_gmt":"2023-08-31T14:00:27","guid":{"rendered":"https:\/\/msgprogramator.sk\/?p=2370"},"modified":"2025-07-07T10:57:18","modified_gmt":"2025-07-07T10:57:18","slug":"java-abstract-factory","status":"publish","type":"post","link":"https:\/\/msgprogramator.sk\/en\/java-abstract-factory\/","title":{"rendered":"Java design pattern Abstract Factory"},"content":{"rendered":"<p>In this article, we will look at the latest <strong>Java<\/strong> <strong>design pattern<\/strong> from the category of patterns for creating objects (<strong>creational patterns<\/strong>) &#8211; Abstract Factory.<\/p>\n<p>Read more about other Java design patterns:<\/p>\n<ul>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-singleton\/\">Singleton design pattern<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-builder\/\">Builder design pattern<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-prototype\/\">Prototype design pattern<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-factory\/\">Factory design pattern<\/a><\/li>\n<li><a href=\"https:\/\/msgprogramator.sk\/en\/java-adapter\/\">Adapter design pattern<\/a><\/li>\n<\/ul>\n<h2>What is the Abstract Factory design pattern?<\/h2>\n<p><em>Abstract Factory<\/em> is one of the so-called creational design patterns in software engineering.  The goal of this pattern is to provide an interface for creating families of related objects without specifying their specific classes.<\/p>\n<h2>What problem does the Abstract Factory design pattern solve?<\/h2>\n<p>Abstract Factory solves the problem of creating instances of classes that share a common interface or are part of the same class family. The <em>Abstract Factory<\/em> design pattern is based on the principle of abstraction. It provides an interface that defines methods for creating objects. Specific implementations of this interface are represented by individual factories that create instances of specific classes. It helps decouple the process of creating objects from using them, and makes it easy to replace an entire family of objects with another without changing the code that uses those objects.<\/p>\n<h2>Example of Abstract Factory implementation in Java<\/h2>\n<p>Now we will show how to implement the <em>Abstract Factory<\/em> pattern in Java. This example illustrates how the <em>Abstract Factory<\/em> pattern allows the creation of families of related objects without the need to specify their specific classes in code. In this case, it&#8217;s a chocolate and nougat factory from the Lindt manufacturer.<\/p>\n<p><u>CokoladaAbstractFactory<\/u> interface defines two methods &#8211; vyrobCokoladu() and vyrobNugat(), which are to return objects of type Cokolada and Nugat of the corresponding factories. This interface represents an abstract chocolate factory.<\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ Rozhranie pre abstraktnu vyrobnu cokoladu\npublic interface CokoladaAbstractFactory {\n    Cokolada vyrobCokoladu();\n    Nugat vyrobNugat();\n}\n<\/code><\/pre>\n<p><u>LindtFactory<\/u> class implements the CokoladaAbstractFactory interface and provides implementations of the vyrobCokoladu() and vyrobNugat() methods. This class is a concrete implementation of an abstract chocolate factory for the Lindt manufacturer.<\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ Vyrobca Lindt cokolady implementuje AbstractFactory\npublic class LindtFactory implements CokoladaAbstractFactory {\n    public Cokolada vyrobCokoladu() {\n        return new LindtCokolada();\n    }\n\n    public Nugat vyrobNugat() {\n        return new LindtNugat();\n    }\n}\n<\/code><\/pre>\n<p><u>Cokolada<\/u> interface defines the zobrazTyp() method, which is implemented in the LindtCokolada class. This interface represents the type of chocolate.<\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ Rozhranie pre cokoladu\npublic interface Cokolada {\n    void zobrazTyp();\n}\n<\/code><\/pre>\n<p><u>LindtCokolada<\/u> class implements the Chocolate interface and defines the zobrazTyp() method that prints information about the chocolate.<\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ Konkretna implementacia Lindt cokolady\npublic class LindtCokolada implements Cokolada {\n    public void zobrazTyp() {\n        System.out.println(&quot;Lindt \u010dokol\u00e1da&quot;);\n    }\n}\n<\/code><\/pre>\n<p><u>Nugat<\/u> interface defines the zobrazTyp() method, which is implemented in the LindtNugat class. This interface represents a type of nougat.<\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ Rozhranie pre nugat\npublic interface Nugat {\n    void zobrazTyp();\n}\n<\/code><\/pre>\n<p><u>LindtNugat<\/u> class implements the Nugat interface and defines the zobrazTyp() method that prints information about the nugat.<\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\n\/\/ Konkretna implementacia Lindt nugatu\npublic class LindtNugat implements Nugat {\n    public void zobrazTyp() {\n        System.out.println(&quot;Lindt nug\u00e1t&quot;);\n    }\n}\n<\/code><\/pre>\n<p><u>Main<\/u> class contains the main() method, where the use of the abstract factory is demonstrated. A LindtFactory instance is created, which is of CokoladaAbstractFactory type. Subsequently, the Cokolada and Nugat objects are created using this abstract factory. Finally, the zobrazTyp() methods are called on these objects, which prints out the chocolate and nougat information.<\/p>\n<pre><code class=\"language-java\" data-line=\"\">import designpatterns.Cokolada;\nimport designpatterns.CokoladaAbstractFactory;\nimport designpatterns.LindtFactory;\nimport designpatterns.Nugat;\n\n\/\/ Priklad pouzitia Abstract Factory\npublic class Main {\n    public static void main(String[] args) {\n        \/\/ Vytvorenie abstraknej tovarne na cokoladu\n        CokoladaAbstractFactory cokoladaFactory = new LindtFactory();\n\n        \/\/ Vyroba cokolady a nugatu pomocou abstraktnej tovarne\n        Cokolada cokolada = cokoladaFactory.vyrobCokoladu();\n        Nugat nugat = cokoladaFactory.vyrobNugat();\n\n        \/\/ A zobrazenie informacii o nich\n        cokolada.zobrazTyp();\n        nugat.zobrazTyp();\n    }\n}\n<\/code><\/pre>\n<p>Note that by using the Abstract Factory design pattern, you can simply replace the LindtFactory instance with any other instance implementing the CokoladaAbstractFactory interface and the rest of the code will remain unchanged. For example, we could create a MilkaFactory to produce Milka chocolate.<\/p>\n<figure id=\"attachment_1877\" aria-describedby=\"caption-attachment-1877\" style=\"width: 390px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1328\" src=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/08\/abtract-factory-vystup.png\" alt=\"Program output using Abstract Factory design pattern\" width=\"390\" height=\"176\" srcset=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/08\/abtract-factory-vystup.png 390w, https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/08\/abtract-factory-vystup-300x135.png 300w\" sizes=\"auto, (max-width: 390px) 100vw, 390px\" \/><figcaption id=\"caption-attachment-1877\" class=\"wp-caption-text\">Program output using Abstract Factory design pattern<\/figcaption><\/figure>\n<p>We have prepared the files with the above example in the form of code that you can run directly in Java. <a href=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2023\/08\/AbstractFactory.zip\">Download the Java Abstract Factory code here.<\/a><\/p>\n<p>If you&#8217;re looking for a job and you&#8217;re a <a href=\"https:\/\/msg-life.sk\/en\/jobs\/java-programmer-senior\/\">Java developer<\/a>, read more about <a href=\"https:\/\/msgprogramator.sk\/en\/it-salary-developer\/\">developer salary<\/a> and check out our <a href=\"https:\/\/msg-life.sk\/en\/jobs\/\">job offers<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Abstract Factory design pattern and what is it for? Read our new article and download the example code.<\/p>\n","protected":false},"author":14,"featured_media":1876,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[57],"tags":[],"class_list":["post-2370","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/comments?post=2370"}],"version-history":[{"count":6,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2370\/revisions"}],"predecessor-version":[{"id":5217,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2370\/revisions\/5217"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media\/1876"}],"wp:attachment":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media?parent=2370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/categories?post=2370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/tags?post=2370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}