?
Solved

factory v/s abstract factory

Posted on 2005-02-25
10
Medium Priority
?
470 Views
Last Modified: 2012-08-13
hi experts



i want to know what exactly is difference between "factory design pattern" and abstract factory design pattern"


is it true that abstract factory and factory design patterns are used to gether, and factory design pattern instantiates
abstract factory design pattern.

a short n clear description would greatly help..

please do not redirect me to websites such as      http://c2.com/cgi/wiki?AbstractFactoryVsFactoryMethod

i just need a clear picture of whats going on..

thanks in advance,
J
0
Comment
Question by:jaggernat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13409157
Abstract Factory - provide an interface for crreating families of related or dependant objects without specifying their concrete classes

Factory Metod - define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
0
 
LVL 10

Author Comment

by:jaggernat
ID: 13409159
can you please give a short example.

thanks for prompt res,
J
0
 
LVL 92

Expert Comment

by:objects
ID: 13409201
AF - a gui toolkit could use this pattern to create widgets, the abstract factory would define methods for creating various types of widgets avaiolable. Each L&F (eg. Windows, Motif) would provide an implementation of this factory to return the widget subclass for that l&f thus making the client independant of the l&f

FM - an application framework that deals with different document types. In this case the particulkar Document type to create is application specific and the framework has no way of knowing what type of document is to be created (it only knows about abstract document class). So instead application subclasses redefine the factory method to return the appropriate document subclass.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Expert Comment

by:Mayank S
ID: 13409766
>> please do not redirect me to websites such as      

I could've copy-pasted the whole stuff, but actually there's good explanation there :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13409793
"provide an interface for crreating families of related or dependant objects without specifying their concrete classes"

That's why i told you earlier (largely ignored) your abstract soup factory should not be instantiating (or promising to instantiate) concrete soups
0
 
LVL 1

Accepted Solution

by:
sanjusk earned 1200 total points
ID: 13411691
The Factory Method provides a simple decision making class that
returns one of several possible subclasses of an abstract base class depending
on the data that are provided.
The Abstract Factory Method provides an interface to create and
return one of several families of related objects.

I suggest
http://www.patterndepot.com/put/8/DesignJava.PDF get thiis file. it explain thing very clearly. You justs need too study first few page.that it.

If still this book doesn't help then i'll try to give an example
regards
Sanjeev
0
 
LVL 19

Assisted Solution

by:Jim Cakalic
Jim Cakalic earned 200 total points
ID: 13417092
Hi, jaggernat. Here's an explanation I gave once a long time ago to try to help someone else differentiate the three factory design patterns. Maybe it will help you.

Simple Factory creates an instance of one of several possible classes depending on the data provided to it. It says, in effect, "You give me some criteria and I will decide how to make an appropriate object. You shouldn't care what the class is of the object I give you -- they will all offer the same services." As a silly example, suppose you wanted to construct a Vehicle object based on the number of wheels that it has. Which object you get is irrelevant for your purposes. You might define it like this:

public class VehicleFactory {
   public Vehicle getVehicle(int wheels) {
       switch (wheels) {
       case 1:  return new Unicycle();
       case 2:  return new Bicycle();
       case 3:  return new Tricycle();
       case 4:  return new Carriage();
       default: return null;
   }
}

interface Vehicle {
   // common methods
}

public class Unicycle implements Vehicle {
}
public class Bicycle implements Vehicle {
}
public class Tricycle implements Vehicle {
}
public class Carriage implements Vehicle {
}

So now, based on the data supplied, this Vehicle factory can decide which subclass of a single hierarchy to instantiate. The client knows that it is working with a factory.

Factory Method is a subtle extension of this idea. The difference is that there is no real factory decision point at which it is determined to construct one class or another as in Simple Factory. Instead, there are two hierarchies of classes at work. Specific instances of classes from one hierarchy are created as determined by the needs of the client program. Which specific class from the second heirarchy is created is decided by each class from the first hierarchy. Totally clear? How about another example.

Extending the previous example, I'm going to define two class hierarchies. The first will be Vehicle and the second will be SteeringMechanism. A picture would be good but I haven't figured out how to post pictures to the forum. In lieu of a picture, here are some abbreviated class definitions.

public abstract class Vehicle {
   public abstract SteeringMechanism getSteeringMechanism();
}
public class Unicycle extends Vehicle {
   public SteeringMechanism getSteeringMechanism() {
       return new SeatOfPants();
   }
}
public class Bicycle extends Vehicle {
   public SteeringMechanism getSteeringMechanism() {
       return new Handlebars();
   }
}
public class Tricycle extends Vehicle {
   public SteeringMechanism getSteeringMechanism() {
       return new Handlebars();
   }
}
public class Car extends Vehicle {
   public SteeringMechanism getSteeringMechanism() {
       return new SteeringWheel();
   }
}

public abstract class SteeringMechanism {
   // some useful abstract method definitions
}
public class SeatOfPants extends SteeringMechanism {
   // implementation
}
public class Handlebars extends SteeringMechanism {
   // implementation
}
public class SteeringWheel extends SteeringMechanism {
   // implementation
}

In this example, the decision about which Vehicle subclass to instantiate determines which SteeringMechanism class will be instantiated. And, as you can see, there is not necessarily a one-to-one correspondence between classes in the two hierarchies. Both Bicycle and Tricycle instantiate a Handlebar class as the SteeringMechanism. The pattern is called Factory Method because the, well, the factory is implemented in a method. Note that each Vehicle class implements its own getSteeringMechanism method? This method is actually the "factory method" of the pattern.

Finally, Abstract Factory is about creating factories for families of objects. Instead of being able to create just one kind of object, each concrete factory can greate several kinds of objects. Each concrete factory implements a common interface so that after the specific concrete factory is selected the application can use it without concern as to which factory it is using. That is what makes this an "Abstract Factory". GUI toolkits are the classic example here. Lets say you want to support Motif, Windows, and Macintosh GUIs which can each display a ListBox, a TextField, and a Label. First you'd define interfaces and/or abstract implementations for Toolkit, ListBox, TextField, and Label. The Toolkit is the abstract factory in this case perhaps with methods for createListBox, createTextField, and createLabel. Then you'd implement the concrete classes for these interfaces, three of each: MotifToolkit, MotifListBox, MotifTextField, and MotifLabel for Motif and similar for Windows and Mac. Once the application had selected the UI factory, the it could call the create methods without concern for which specific family of objects was being returned.

Best regards,
Jim Cakalic
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13425111
I thought objects also deserved some points here.
0
 
LVL 10

Author Comment

by:jaggernat
ID: 13526402
is it? my appologies to you( and objects) if i was wrong in judging points. actually the pdf which sanjusk gave me was clear with easy to understand examples.

thanks
J
 
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question