Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I use abstract class instead of interface while implementing factory pattern. Would it still be a factory pattern?

For example : http://www.tutorialspoint.com/design_pattern/factory_pattern.htm

If I change interface shape on abstract class Shape, make concrete classes to extend Shape and Make the Shape factory return Shape abstract class typed objects. Is it still going to be a factory pattern ?

like image 593
Achozen Avatar asked Jul 17 '15 14:07

Achozen


People also ask

Can we use abstract class instead of interface?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

Is abstract factory a factory of factories?

Abstract Factory classes are often based on a set of Factory Methods, but you can also use Prototype to compose the methods on these classes. Abstract Factory can serve as an alternative to Facade when you only want to hide the way the subsystem objects are created from the client code.

Which of the following is true about abstract factory design pattern?

In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface. Q 8 - Which of the following is correct about Abstract Factory design pattern. A - This type of design pattern comes under creational pattern.


1 Answers

I would go with yes.

Lets look at definition of Factory method pattern:

the factory method pattern is a creational pattern which uses factory methods to deal with the problem of creating objects without specifying the exact class of object that will be created

The motivation behind this pattern is to separate object creation from the client using the object. Client should provide specification to factory but details how the object is built are abstracted away by the factory.

If this is an interface or abstract class is an implementation detail specific to situation, as long as your implementation of the factory lets you achieve the motivation behind pattern.

Consider using abstract classes if any of these statements apply to your situation:

  • You want to share code among several closely related classes.

  • You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).

  • You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

Consider using interfaces if any of these statements apply to your situation:

  • You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.

  • You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.

  • You want to take advantage of multiple inheritance of type.

In some implementations it might even make more sense to use abstract class rather then interface for the Products created by the factory. If there is shared set of features/behavior between all products then it does make sense to put these into base abstract class. This could apply even if products are built from different factories.

It boils down to: do you wish to and does it make sense to introduce coupling between products or not? In the end, client will get same result - Product built based upon specification, with details of construction abstracted away.

like image 87
John Avatar answered Oct 25 '22 00:10

John