java - 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 change interface shape on abstract class shape, make concrete classes extend shape , make shape factory return shape abstract class typed objects. still going factory pattern ?
i go yes.
lets @ definition of factory method pattern:
the factory method pattern creational pattern uses factory methods deal problem of creating objects without specifying exact class of object created
the motivation behind pattern separate object creation client using object. client should provide specification factory details how object built abstracted away factory.
if interface or abstract class implementation detail specific situation, long implementation of factory lets achieve motivation behind pattern.
consider using abstract classes if of these statements apply situation:
you want share code among several closely related classes.
you expect classes extend abstract class have many common methods or fields, or require access modifiers other public (such protected , private).
you want declare non-static or non-final fields. enables define methods can access , modify state of object belong.
consider using interfaces if of these statements apply situation:
you expect unrelated classes implement interface. example, interfaces comparable , cloneable implemented many unrelated classes.
you want specify behavior of particular data type, not concerned implements behavior.
you want take advantage of multiple inheritance of type.
in implementations might make more sense use abstract class rather interface products created factory. if there shared set of features/behavior between products make sense put these base abstract class. apply if products built different factories.
it boils down to: wish , make sense introduce coupling between products or not? in end, client same result - product built based upon specification, details of construction abstracted away.
Comments
Post a Comment