0

I am going through AbstractFactory design pattern in tutorialpoints site : http://www.tutorialspoint.com/design_pattern/abstract_factory_pattern.htm.

I have understood the basic concept. I have two doubts here.

  1. I didnt get the use of AbstractFactory class there. What advantage we got when the Factories are of AbstractFactory type.

  2. Moreover why to use class there? we can go for an Interface right?

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
user3766874
  • 793
  • 2
  • 6
  • 14

2 Answers2

0

I find that example slightly confusing, since you are using an Abstract Factory to create two different items (and I am not a big fan of returning null values), that being said:

Sometimes you need to define additional behavior other than the method which generates the type you are after.

Let us take a similar example used in that site:

public abstract class AbstractShapeFactory{
   abstract Shape getShape(String shape) ;

   protected bool isShapeNameValid(String shapeName) {
       ...//Maybe you need to do some complex validation here.
          //For instance, `green` would not be a valid shape type.
   }
}

Then you extend it like so:

public class 2DShapeFactory extends AbstractShapeFactory {
     public Shape getShape(String shape) {
          super.isShapeNameValid();
          ... //Maybe some additional checks to see if the user really asked for a 2D shape.
          ....
     }
}

This would allow you guarantee that a given class implements a series of methods (as would an interface) but would also save you time because certain methods have already been implemented.

npinti
  • 51,780
  • 5
  • 72
  • 96
0

I didnt get the use of AbstractFactory class there. What advantage we got when the Factories are of AbstractFactory type.

When your system has to create multiple families of products or you want to provide a library of products without exposing the implementation details, you can use this pattern.

A key characteristic of this pattern : It will decouple the concrete classes from the client.

Refer to this below SE question for more details:

What is the basic difference between the Factory and Abstract Factory Patterns?

An example of an Abstract Factory in use could be UI tool kits ( Across Windows, Mac and Linux).

However, the implementation of these widgets vary across platforms. You could write a platform independent client thanks to the Abstract Factory implementation.

Moreover why to use class there? we can go for an Interface right?

Interface can't achieve the intent of Abstract Factory pattern. With interfaces, client need to know the contract. Any changes in contract are visible to client and these changes may break client functionality. But Abstract Factory decouple client from concrete classes, which are implementing the interface ( Factory Method)

Below articles are useful for better understanding

dzone article

oodesign article

sourcemaking article

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211