{"id":44746,"date":"2015-09-30T06:34:48","date_gmt":"2015-09-30T03:34:48","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=44746"},"modified":"2023-12-07T11:18:55","modified_gmt":"2023-12-07T09:18:55","slug":"factory-method-design-pattern","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html","title":{"rendered":"Factory Method Design Pattern Example"},"content":{"rendered":"<p><em>This article is part of our Academy Course titled <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/java-design-patterns\/\">Java Design Patterns<\/a>.<\/em><\/p>\n<p>In this course you will delve into a vast number of Design Patterns and see how those are implemented and utilized in Java. You will understand the reasons why patterns are so important and learn when and how to apply each one of them. Check it out <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/java-design-patterns\/\">here<\/a>!<\/p>\n<div class=\"toc\">\n<h4>Table Of Contents<\/h4>\n<dl>\n<dt><a href=\"#Introduction\">1. Introduction<\/a><\/dt>\n<dt><a href=\"#definition of FMP\">2. What is the Factory Method Pattern<\/a><\/dt>\n<dt><a href=\"#Implementing FMP\">3. Implementing the Factory Method Pattern<\/a><\/dt>\n<dt><a href=\"#when to use FMP\">4. When to use the Factory Method Pattern<\/a><\/dt>\n<dt><a href=\"#FMP in JDK\">5. Factory Method Pattern in JDK<\/a><\/dt>\n<dt><a href=\"#download source code\">6. Download the Source Code<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"Introduction\"><\/a>1. Introduction<\/h2>\n<p>In today\u2019s modern world, everyone is using software to facilitate their jobs. Recently, a product company has shifted the way they used to take orders from their clients. The company is now looking to use an application to take orders from them. They receive orders, errors in orders, feedback for the previous order, and responses to the order in an XML format. The company has asked you to develop an application to parse the XML and display the result to them.<\/p>\n<p>The main challenge for you is to parse an XML and display its content to the user. There are different XML formats depending on the different types of messages the company receives from its clients. Like, for example, an order type XML has different sets of xml tags as compared to the response or error XML. But the core job is the same; that is, to display to the user the message being carried in these XMLs.<\/p>\n<p>Although the core job is the same, the object that would be used varies according to the kind of XML the application gets from the user. So, an application object may only know that it needs to access a class from within the class hierarchy (hierarchy of different parsers), but does not know exactly which class from among the set of subclasses of the parent class is to be selected.<\/p>\n<p>In this case, it is better to provide a factory, i.e. a factory to create parsers, and at runtime a parser gets instantiated to do the job, according to the kind of XML the application receives from the user.<\/p>\n<p>The Factory Method Pattern, suited for this situation, defines an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.<\/p>\n<p>Let us see some more details about the Factory Method Pattern and then we will use it to implement the XML parser for the application.<\/p>\n<h2><a name=\"definition of FMP\"><\/a>2. What is the Factory Method Pattern<\/h2>\n<p>The Factory Method Pattern gives us a way to encapsulate the instantiations of concrete types. The Factory Method pattern encapsulates the functionality required to select and instantiate an appropriate class, inside a designated method referred to as a factory method. The Factory Method selects an appropriate class from a class hierarchy based on the application context and other influencing factors. It then instantiates the selected class and returns it as an instance of the parent class type.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>The advantage of this approach is that the application objects can make use of the factory method to get access to the appropriate class instance. This eliminates the need for an application object to deal with the varying class selection criteria.<\/p>\n<p><figure id=\"attachment_6753\" aria-describedby=\"caption-attachment-6753\" style=\"width: 384px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/Factoryclass_diagram_1.jpg\"><img decoding=\"async\" class=\"wp-image-6753 size-medium\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/Factoryclass_diagram_1.jpg\" alt=\"Figure 1\" width=\"384\" height=\"163\" \/><\/a><figcaption id=\"caption-attachment-6753\" class=\"wp-caption-text\">Figure 1<\/figcaption><\/figure><\/p>\n<p><strong>Product<\/strong><\/p>\n<ul>\n<li>Defines the interface of objects the factory method creates.<\/li>\n<\/ul>\n<p><strong>ConcreteProduct<\/strong><\/p>\n<ul>\n<li>Implements the Product interface.<\/li>\n<\/ul>\n<p><strong>Creator<\/strong><\/p>\n<ul>\n<li>Declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.<\/li>\n<li>May call the factory method to create a Product object.<\/li>\n<\/ul>\n<p><strong>ConcreteCreator<\/strong><\/p>\n<ul>\n<li>Overrides the factory method to return an instance of a ConcreteProduct.<\/li>\n<\/ul>\n<p>Factory methods eliminate the need to bind application-specific classes into your code. The code only deals with the Product interface; therefore it can work with any user-defined ConcreteProduct classes.<\/p>\n<h2><a name=\"Implementing FMP\"><\/a>3. Implementing Factory Method Pattern<\/h2>\n<p>To implement the solution for the application as discussed above, let\u2019s first check the product we have.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.factorymethodpattern;\n\npublic interface XMLParser {\n\n\tpublic String parse();\n\n}<\/pre>\n<p>The above interface will be used by the different XML parsers.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.factorymethodpattern;\n\npublic class ErrorXMLParser implements XMLParser{\n\n\t@Override\n\tpublic String parse() {\n\t\tSystem.out.println(\"Parsing error XML...\");\n\t\treturn \"Error XML Message\";\n\t}\n\n}\n<\/pre>\n<p>The <code>ErrorXMLParser<\/code> implements the <code>XMLParser<\/code> and is used to parse the error message XMLs.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.factorymethodpattern;\n\npublic class FeedbackXML implements XMLParser{\n\n\t@Override\n\tpublic String parse() {\n\t\tSystem.out.println(\"Parsing feedback XML...\");\n\t\treturn \"Feedback XML Message\";\n\t}\n\n}<\/pre>\n<p>The above class is used to parse the feedback message XMLs.<\/p>\n<p>The other XML parsers are:<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.factorymethodpattern;\n\npublic class OrderXMLParser implements XMLParser{\n\n\t@Override\n\tpublic String parse() {\n\t\tSystem.out.println(\"Parsing order XML...\");\n\t\treturn \"Order XML Message\";\n\t}\n\n}\n\npackage com.javacodegeeks.patterns.factorymethodpattern;\n\npublic class ResponseXMLParser implements XMLParser{\n\n\t@Override\n\tpublic String parse() {\n\t\tSystem.out.println(\"Parsing response XML...\");\n\t\treturn \"Response XML Message\";\n\t}\n\n}<\/pre>\n<p>To display the parsed messages from the parsers, an abstract service class is created which will be extends by service specific, i.e. parser specific, display classes.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.factorymethodpattern;\n\npublic abstract class DisplayService {\n\n\tpublic void display(){\n\t\tXMLParser parser = getParser();\n\t\tString msg = parser.parse();\n\t\tSystem.out.println(msg);\n\t}\n\n\tprotected abstract XMLParser getParser();\n\n}<\/pre>\n<p>The above class is used to display the message fetched by the XML parser to the user. The above class is an abstract class that contains two important methods. The <code>display<\/code> method is used to display the message to the user. The <code>getParser<\/code> method is the factory method which is implemented by the subclasses to instantiate the parser object and the method is used by the <code>display<\/code> method in order to parse the XML and gets the message to display.<br \/>\n[ulp id=&#8217;7eyRwVlMDyxg6Ptw&#8217;]<br \/>\n&nbsp;<br \/>\nBelow are the subclasses of the <code>DisplayService<\/code> which implement the <code>getParser<\/code> method.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.factorymethodpattern;\n\npublic class ErrorXMLDisplayService extends DisplayService{\n\n\t@Override\n\tpublic XMLParser getParser() {\n\t\treturn new ErrorXMLParser();\n\t}\n\n}\n<\/pre>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.factorymethodpattern;\n\npublic class FeedbackXMLDisplayService extends DisplayService{\n\n\t@Override\n\tpublic XMLParser getParser() {\n\t\treturn new FeedbackXML();\n\t}\n\n}\n<\/pre>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.factorymethodpattern;\n\npublic class OrderXMLDisplayService extends DisplayService{\n\n\t@Override\n\tpublic XMLParser getParser() {\n\t\treturn new OrderXMLParser();\n\t}\n\n}\n<\/pre>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.factorymethodpattern;\n\npublic class ResponseXMLDisplayService extends DisplayService{\n\n\t@Override\n\tpublic XMLParser getParser() {\n\t\treturn new ResponseXMLParser();\n\t}\n\n}\n<\/pre>\n<p>Now, let\u2019s test the factory method.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.factorymethodpattern;\n\npublic class TestFactoryMethodPattern {\n\n\tpublic static void main(String[] args) {\n\t\tDisplayService service = new FeedbackXMLDisplayService();\n\t\tservice.display();\n\n\t\tservice = new ErrorXMLDisplayService();\n\t\tservice.display();\n\n\t\tservice = new OrderXMLDisplayService();\n\t\tservice.display();\n\n\t\tservice = new ResponseXMLDisplayService();\n\t\tservice.display();\n\n\t}\n\n}<\/pre>\n<p>The above class results to the following output:<\/p>\n<pre class=\"brush:bash\">Parsing feedback XML...\nFeedback XML Message\nParsing error XML...\nError XML Message\nParsing order XML...\nOrder XML Message\nParsing response XML...\nResponse XML Message\n<\/pre>\n<p>In the above class, you can clearly see that the by letting the subclasses to implement the factory method creates the different instances of parsers which can be used at runtime according to the need.<\/p>\n<h2><a name=\"when to use FMP\"><\/a>4. When to use the Factory Method Pattern<\/h2>\n<p>Use the Factory Method pattern when<\/p>\n<ul>\n<li>A class can&#8217;t anticipate the class of objects it must create.<\/li>\n<li>A class wants its subclasses to specify the objects it creates.<\/li>\n<li>Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.<\/li>\n<\/ul>\n<h2><a name=\"FMP in JDK\"><\/a>5. Factory Method Pattern in JDK<\/h2>\n<p>The following are the usage(s) of the Factory Method Pattern in JDK.<\/p>\n<ul>\n<li><code>java.util.Calendar#getInstance()<\/code><\/li>\n<li><code>java.util.ResourceBundle#getBundle()<\/code><\/li>\n<li><code>java.text.NumberFormat#getInstance()<\/code><\/li>\n<li><code>java.nio.charset.Charset#forName()<\/code><\/li>\n<li><code>java.net.URLStreamHandlerFactory#createURLStreamHandler(String)<\/code> (Returns singleton object per protocol)<\/li>\n<\/ul>\n<h2><a name=\"download source code\"><\/a>6. Download the Source Code<\/h2>\n<p>This was a lesson on the Factory Method Pattern. You may download the source code here:<br \/>\n<a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/FactoryMethodPattern-Project.zip\"><strong>FactoryMethodPattern-Project<\/strong><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how those are implemented and utilized in Java. You will understand the reasons why patterns are so important and learn when and how to apply each one of &hellip;<\/p>\n","protected":false},"author":967,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[145],"class_list":["post-44746","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-design-patterns"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Factory Method Design Pattern Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Factory Method Design Pattern Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-30T03:34:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-07T09:18:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Rohit Joshi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rohit Joshi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/factory-method-design-pattern.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/factory-method-design-pattern.html\"},\"author\":{\"name\":\"Rohit Joshi\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/ddd1b95af381c5042c09833e6a108e20\"},\"headline\":\"Factory Method Design Pattern Example\",\"datePublished\":\"2015-09-30T03:34:48+00:00\",\"dateModified\":\"2023-12-07T09:18:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/factory-method-design-pattern.html\"},\"wordCount\":983,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/factory-method-design-pattern.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Design Patterns\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/factory-method-design-pattern.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/factory-method-design-pattern.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/factory-method-design-pattern.html\",\"name\":\"Factory Method Design Pattern Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/factory-method-design-pattern.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/factory-method-design-pattern.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2015-09-30T03:34:48+00:00\",\"dateModified\":\"2023-12-07T09:18:55+00:00\",\"description\":\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/factory-method-design-pattern.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/factory-method-design-pattern.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/factory-method-design-pattern.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/factory-method-design-pattern.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Factory Method Design Pattern Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/ddd1b95af381c5042c09833e6a108e20\",\"name\":\"Rohit Joshi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"caption\":\"Rohit Joshi\"},\"description\":\"Rohit Joshi is a Senior Software Engineer from India. He is a Sun Certified Java Programmer and has worked on projects related to different domains. His expertise is in Core Java and J2EE technologies, but he also has good experience with front-end technologies like Javascript, JQuery, HTML5, and JQWidgets.\",\"sameAs\":[\"http:\\\/\\\/www.javacodegeeks.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/rohit-joshi\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Factory Method Design Pattern Example - Java Code Geeks","description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html","og_locale":"en_US","og_type":"article","og_title":"Factory Method Design Pattern Example - Java Code Geeks","og_description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","og_url":"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-09-30T03:34:48+00:00","article_modified_time":"2023-12-07T09:18:55+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Rohit Joshi","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Rohit Joshi","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html"},"author":{"name":"Rohit Joshi","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/ddd1b95af381c5042c09833e6a108e20"},"headline":"Factory Method Design Pattern Example","datePublished":"2015-09-30T03:34:48+00:00","dateModified":"2023-12-07T09:18:55+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html"},"wordCount":983,"commentCount":1,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Design Patterns"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html","url":"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html","name":"Factory Method Design Pattern Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2015-09-30T03:34:48+00:00","dateModified":"2023-12-07T09:18:55+00:00","description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/factory-method-design-pattern.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"Factory Method Design Pattern Example"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/ddd1b95af381c5042c09833e6a108e20","name":"Rohit Joshi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","caption":"Rohit Joshi"},"description":"Rohit Joshi is a Senior Software Engineer from India. He is a Sun Certified Java Programmer and has worked on projects related to different domains. His expertise is in Core Java and J2EE technologies, but he also has good experience with front-end technologies like Javascript, JQuery, HTML5, and JQWidgets.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/rohit-joshi"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/967"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=44746"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44746\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=44746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=44746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=44746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}