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.
I didnt get the use of AbstractFactory
class there. What advantage we got when the Factories are of AbstractFactory
type.
Moreover why to use class there? we can go for an Interface
right?
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.