Labels

.NET Job Questions About Java Absract class Abstract class Abstract Class and Interface Aggregation ajax aop apache ofbiz Apache ofbiz tutrial Association authentication autocad basics batch Binary Tree bootstrap loader in java build Builder design pattern C++ Job Questions caching CallableStatement in java certifications Chain of responsibility Design pattern charts check parentheses in a string Classes classloader in java classloading concept code quality collage level java program Composition concurrency Concurrency Tutorial Converting InputStream to String Core Java core java concept core java interview questions Core Java Interview Questions Core Java Questions core java tutorial CyclicBarrier in Java data structures database Database Job Questions datetime in c# DB Db2 SQL Replication deserialization in java Design Patterns designpatterns Downloads dtd Eclipse ejb example/sample code exception handling in core java file handling injava File I/O vs Memory-Mapped Filter first program in spring flex Garbage Collection Generics concept in java grails groovy and grails Guice Heap hibernate Hibernate Interview Questions how-to IBM DB2 IBM DB2 Tutorial ide immutable Interceptor Interface interview Interview Questions for Advanced JAVA investment bank j2ee java JAVA Code Examples Java 7 java changes java class loading JAVA Classes and Objects Java Classloader concept Java classloading concept java cloning concept java collection Java collection interview questions Java Collections java concurrency Java CountDownLatch java definiton Java design pattern Java EE 5 Java EE 6 Java Exceptions Java file Java Garbage Collection Java generics Java Glossary java hot concept java immutable concept Java Interface Java interview Question java interview question 2012 java interview question answer Java Interview Questions Java Interview Questions and Answers java interview topic java investment bank Java Job Questions java multithreading java multithreading concept java new features Java Packages java proxy object java questions Java Serialization Java serialization concept java serialization interview question java session concept java string Java Swings Questions java synchronization java threading Java Threads Questions java tutorial java util; java collections; java questions java volatile java volatile interview question Java Wrapper Classes java.java1.5 java.lang.ClassCastException JavaNotes javascript JAX-WS jdbc JDBC JDBC Database connection jdk 1.5 features JDK 1.5 new features Concurrent HashMap JMS interview question JMS tutorial job JSESSIONID concept JSESSIONID interview Question JSF jsp JSP Interview Question JSP taglib JSTL with JSP Junit Junit Concept Junit interview question.Best Practices to write JUnit test cases in Java JVM Linux - Unix tutorial Marker Interfaces MD5 encryption and decryption messaging MNC software java interview question musix NCR java interview question Networking Job Questions news Object Serialization Objects ojdbc14.jar OOP Oracle Oracle SQL Query for two timestamp difference orm own JavaScript function call in Apache ofbiz Packages Palm Apps patterns pdf persistence Portal Portlet Spring Integration Prime number test in java programs Rails Reboot remote computers REST Ruby Sample application schema SCJP security Senior java developer interviews servlet3 servlets session tracking singleton design pattern Spring Spring 2.5 Framework spring ebook Spring framework concept spring MVC spring pdf Spring Security Spring Security interview questions SQL SQL performance SQL Query to create xml file Sql Query tuning ssis and ssrs StAX and XML string concept string immutable string in java strings struts Struts2 Struts2 integration synchronization works in java Technical Interview testing tips Tomcat top Tutorial Volatile in deep Volatile working concept web Web Developer Job Questions web services weblogic Weblogic Application Server websphere what is JSESSIONID xml XML parsing in java XML with Java xslt


Saturday, 13 July 2013

AbstractFactory Design Pattern with an Example program


Creational Patterns - Abstract Factory Pattern

Definition:
  • Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
  • A hierarchy that encapsulates: many possible “platforms”, and the construction of a suite of “products”.
  •  The new operator considered harmful.
Where to use & benefits
  1. Creates families of related or dependent objects like Kit.
  2. Provides a class library of products, exposing interface not implementation.
  3. Needs to isolate concrete classes from their super classes.
  4. A system needs independent of how its products are created, composed, and represented.
  5. Try to enforce a constraint.
  6. An alternative to Facade to hide platform-specific classe.
  7. Easily extensible to a system or a family.
  8. Related patterns include.
  9. Factory method, which is often implemented with an abstract factory.
    • Singleton, which is often implemented with an abstract factory.
    • Prototype, which is often implemented with an abstract factory.
    • Facade, which is often used with an abstract factory by providing an interface for creating implementing class.        
 This pattern is one level of abstraction higher than factory pattern. This means that the abstract factory returns the factory of classes. Like Factory pattern returned one of the several sub-classes, this returns such factory which later will return one of the sub-classes.

Working with Abstract Factory Design Pattern 

The classes that participate to the Abstract Factory pattern are:

    AbstractFactory - declares a interface for operations that create abstract products.
    ConcreteFactory - implements operations to create concrete products.
    AbstractProduct - declares an interface for a type of product objects.
    Product - defines a product to be created by the corresponding ConcreteFactory; it implements the         AbstractProduct interface.
    Client - uses the interfaces declared by the AbstractFactory and AbstractProduct classes.

The classic implementation for the Abstract Factory pattern is the following:

<!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;color:black;background:white;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #008000; font-weight: bold">package</span> com<span style="color: #303030">.</span><span style="color: #0000C0">kota</span><span style="color: #303030">.</span><span style="color: #0000C0">java</span><span style="color: #303030">;</span>
 
<span style="color: #008000; font-weight: bold">abstract</span> <span style="color: #008000; font-weight: bold">class</span> <span style="color: #B00060; font-weight: bold">AbstractProductA</span><span style="color: #303030">{</span>
 <span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">abstract</span> <span style="color: #303090; font-weight: bold">void</span> <span style="color: #0060B0; font-weight: bold">operationA1</span><span style="color: #303030">();</span>
 <span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">abstract</span> <span style="color: #303090; font-weight: bold">void</span> <span style="color: #0060B0; font-weight: bold">operationA2</span><span style="color: #303030">();</span>
<span style="color: #303030">}</span>
 
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #B00060; font-weight: bold">ProductA1</span> <span style="color: #008000; font-weight: bold">extends</span> AbstractProductA<span style="color: #303030">{</span>
 ProductA1<span style="color: #303030">(</span>String arg<span style="color: #303030">){</span>
  System<span style="color: #303030">.</span><span style="color: #0000C0">out</span><span style="color: #303030">.</span><span style="color: #0000C0">println</span><span style="color: #303030">(</span><span style="background-color: #fff0f0">&quot;Hello &quot;</span><span style="color: #303030">+</span>arg<span style="color: #303030">);</span>
 <span style="color: #303030">}</span> <span style="color: #808080">// Implement the code here</span>
 <span style="color: #008000; font-weight: bold">public</span> <span style="color: #303090; font-weight: bold">void</span> <span style="color: #0060B0; font-weight: bold">operationA1</span><span style="color: #303030">()</span> <span style="color: #303030">{</span> <span style="color: #303030">};</span>
 <span style="color: #008000; font-weight: bold">public</span> <span style="color: #303090; font-weight: bold">void</span> <span style="color: #0060B0; font-weight: bold">operationA2</span><span style="color: #303030">()</span> <span style="color: #303030">{</span> <span style="color: #303030">};</span>
<span style="color: #303030">}</span>
 
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #B00060; font-weight: bold">ProductA2</span> <span style="color: #008000; font-weight: bold">extends</span> AbstractProductA<span style="color: #303030">{</span>
 ProductA2<span style="color: #303030">(</span>String arg<span style="color: #303030">){</span>
  System<span style="color: #303030">.</span><span style="color: #0000C0">out</span><span style="color: #303030">.</span><span style="color: #0000C0">println</span><span style="color: #303030">(</span><span style="background-color: #fff0f0">&quot;Hello &quot;</span><span style="color: #303030">+</span>arg<span style="color: #303030">);</span>
 <span style="color: #303030">}</span> <span style="color: #808080">// Implement the code here</span>
 <span style="color: #008000; font-weight: bold">public</span> <span style="color: #303090; font-weight: bold">void</span> <span style="color: #0060B0; font-weight: bold">operationA1</span><span style="color: #303030">()</span> <span style="color: #303030">{</span> <span style="color: #303030">};</span>
 <span style="color: #008000; font-weight: bold">public</span> <span style="color: #303090; font-weight: bold">void</span> <span style="color: #0060B0; font-weight: bold">operationA2</span><span style="color: #303030">()</span> <span style="color: #303030">{</span> <span style="color: #303030">};</span>
<span style="color: #303030">}</span>
 
<span style="color: #008000; font-weight: bold">abstract</span> <span style="color: #008000; font-weight: bold">class</span> <span style="color: #B00060; font-weight: bold">AbstractProductB</span><span style="color: #303030">{</span>
 <span style="color: #808080">//public abstract void operationB1();</span>
 <span style="color: #808080">//public abstract void operationB2();</span>
<span style="color: #303030">}</span>
 
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #B00060; font-weight: bold">ProductB1</span> <span style="color: #008000; font-weight: bold">extends</span> AbstractProductB<span style="color: #303030">{</span>
 ProductB1<span style="color: #303030">(</span>String arg<span style="color: #303030">){</span>
  System<span style="color: #303030">.</span><span style="color: #0000C0">out</span><span style="color: #303030">.</span><span style="color: #0000C0">println</span><span style="color: #303030">(</span><span style="background-color: #fff0f0">&quot;Hello &quot;</span><span style="color: #303030">+</span>arg<span style="color: #303030">);</span>
 <span style="color: #303030">}</span> <span style="color: #808080">// Implement the code here</span>
<span style="color: #303030">}</span>
 
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #B00060; font-weight: bold">ProductB2</span> <span style="color: #008000; font-weight: bold">extends</span> AbstractProductB<span style="color: #303030">{</span>
 ProductB2<span style="color: #303030">(</span>String arg<span style="color: #303030">){</span>
  System<span style="color: #303030">.</span><span style="color: #0000C0">out</span><span style="color: #303030">.</span><span style="color: #0000C0">println</span><span style="color: #303030">(</span><span style="background-color: #fff0f0">&quot;Hello &quot;</span><span style="color: #303030">+</span>arg<span style="color: #303030">);</span>
 <span style="color: #303030">}</span> <span style="color: #808080">// Implement the code here</span>
<span style="color: #303030">}</span>
 
<span style="color: #008000; font-weight: bold">abstract</span> <span style="color: #008000; font-weight: bold">class</span> <span style="color: #B00060; font-weight: bold">AbstractFactory</span><span style="color: #303030">{</span>
 <span style="color: #008000; font-weight: bold">abstract</span> AbstractProductA <span style="color: #0060B0; font-weight: bold">createProductA</span><span style="color: #303030">();</span>
 <span style="color: #008000; font-weight: bold">abstract</span> AbstractProductB <span style="color: #0060B0; font-weight: bold">createProductB</span><span style="color: #303030">();</span>
<span style="color: #303030">}</span>
 
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #B00060; font-weight: bold">ConcreteFactory1</span> <span style="color: #008000; font-weight: bold">extends</span> AbstractFactory<span style="color: #303030">{</span>
 AbstractProductA <span style="color: #0060B0; font-weight: bold">createProductA</span><span style="color: #303030">(){</span>
  <span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #0060B0; font-weight: bold">ProductA1</span><span style="color: #303030">(</span><span style="background-color: #fff0f0">&quot;ProductA1&quot;</span><span style="color: #303030">);</span>
 <span style="color: #303030">}</span>
 AbstractProductB <span style="color: #0060B0; font-weight: bold">createProductB</span><span style="color: #303030">(){</span>
  <span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #0060B0; font-weight: bold">ProductB1</span><span style="color: #303030">(</span><span style="background-color: #fff0f0">&quot;ProductB1&quot;</span><span style="color: #303030">);</span>
 <span style="color: #303030">}</span>
<span style="color: #303030">}</span>
 
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #B00060; font-weight: bold">ConcreteFactory2</span> <span style="color: #008000; font-weight: bold">extends</span> AbstractFactory<span style="color: #303030">{</span>
 AbstractProductA <span style="color: #0060B0; font-weight: bold">createProductA</span><span style="color: #303030">(){</span>
  <span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #0060B0; font-weight: bold">ProductA2</span><span style="color: #303030">(</span><span style="background-color: #fff0f0">&quot;ProductA2&quot;</span><span style="color: #303030">);</span>
 <span style="color: #303030">}</span>
 AbstractProductB <span style="color: #0060B0; font-weight: bold">createProductB</span><span style="color: #303030">(){</span>
  <span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000; font-weight: bold">new</span> <span style="color: #0060B0; font-weight: bold">ProductB2</span><span style="color: #303030">(</span><span style="background-color: #fff0f0">&quot;ProductB2&quot;</span><span style="color: #303030">);</span>
 <span style="color: #303030">}</span>
<span style="color: #303030">}</span>
 
<span style="color: #808080">//Factory creator - an indirect way of instantiating the factories</span>
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #B00060; font-weight: bold">FactoryMaker</span><span style="color: #303030">{</span>
 <span style="color: #008000; font-weight: bold">private</span> <span style="color: #008000; font-weight: bold">static</span> AbstractFactory pf<span style="color: #303030">=</span><span style="color: #008000; font-weight: bold">null</span><span style="color: #303030">;</span>
 <span style="color: #008000; font-weight: bold">static</span> AbstractFactory <span style="color: #0060B0; font-weight: bold">getFactory</span><span style="color: #303030">(</span>String choice<span style="color: #303030">){</span>
  <span style="color: #008000; font-weight: bold">if</span><span style="color: #303030">(</span>choice<span style="color: #303030">.</span><span style="color: #0000C0">equals</span><span style="color: #303030">(</span><span style="background-color: #fff0f0">&quot;a&quot;</span><span style="color: #303030">)){</span>
   pf<span style="color: #303030">=</span><span style="color: #008000; font-weight: bold">new</span> ConcreteFactory1<span style="color: #303030">();</span>
  <span style="color: #303030">}</span><span style="color: #008000; font-weight: bold">else</span> <span style="color: #008000; font-weight: bold">if</span><span style="color: #303030">(</span>choice<span style="color: #303030">.</span><span style="color: #0000C0">equals</span><span style="color: #303030">(</span><span style="background-color: #fff0f0">&quot;b&quot;</span><span style="color: #303030">)){</span>
    pf<span style="color: #303030">=</span><span style="color: #008000; font-weight: bold">new</span> ConcreteFactory2<span style="color: #303030">();</span>
   <span style="color: #303030">}</span> <span style="color: #008000; font-weight: bold">return</span> pf<span style="color: #303030">;</span>
 <span style="color: #303030">}</span>
<span style="color: #303030">}</span>
 
<span style="color: #808080">// Client</span>
<span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">class</span> <span style="color: #B00060; font-weight: bold">Client</span><span style="color: #303030">{</span>
 <span style="color: #008000; font-weight: bold">public</span> <span style="color: #008000; font-weight: bold">static</span> <span style="color: #303090; font-weight: bold">void</span> <span style="color: #0060B0; font-weight: bold">main</span><span style="color: #303030">(</span>String args<span style="color: #303030">[]){</span>
  AbstractFactory pf<span style="color: #303030">=</span>FactoryMaker<span style="color: #303030">.</span><span style="color: #0000C0">getFactory</span><span style="color: #303030">(</span><span style="background-color: #fff0f0">&quot;a&quot;</span><span style="color: #303030">);</span>
  AbstractProductA product<span style="color: #303030">=</span>pf<span style="color: #303030">.</span><span style="color: #0000C0">createProductA</span><span style="color: #303030">();</span>
  <span style="color: #808080">//more function calls on product</span>
 <span style="color: #303030">}</span>
<span style="color: #303030">}</span>
</pre></td></tr></table></div>

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...