简体   繁体   中英

Factory method with abstract classes

Good morning everyone! I have a problem with an implementation of a videogame and I would ask here for some help. I have two types of action in my project, "mainaction" and "quickaction", both of them have some subclasses that would implement a specific method, "execute" for example. I would like to use the factory method to implement this situation, so I instantiate an interface called "Action" and the two classes of action would implement that interface.

my question is about how to use the factory in this case, with mainaction and quickaction or it would be better to implement a factory with the subclasses of them? how can I do it?

In general, it is always a good idea to specify the return type of a method to be as general as possible

In fact, this is one of the strengths of using factories rather than the constructors of objects themselves: If the behaviour of the objects returned is specified by the return type, then you are free to change what objects are returned as much as you want.

If you refer to a class by it's abstract class ( or preferably interface, maybe some one else can give input here ), you can later decide to return a completely different object from the factory, as long as it is the sub type of the abstract class / implements the interface that the factory method returns. In other words, the client code is immune to changes in the implementation details of your 'action'-classes, because all communication to the action classes goes through the interface of the return type of the factory method.

A direct answer to your question

If MainAction and QuickAction are related, meaning they will be used in fairly similar settings for fairly similar purposes, I would create a factory for them, with factory method returning returning sub types of Action . This way you hide as much of the implementation details as possible from the client. Also, you are referring to the action-classes in as general of a way that you can.

If MainAction and QuickAction represents wildly different concepts however, I would just implement some static factory methods in their respective classes, and not bother with a factory. My reasoning behind this, is that a creating a factory for two different type of objects would result in low cohesion for the factory. Referring to different concepts that do not provide the same functionality using the same interface is an example of specifying a too general return type.

However, note that I am drawing from my fairly limited experience.

I recommend reading 'Effective Java' by Joshua Bloch, I learned a lot from that book.

If your clients only need methods declared in IAction, then have the Factory return IAction. For example,

public static IAction Create(string details) {...}

If your clients need methods specific to QuickAction and MainAction, then have 2 methods in your Factory. For example,

public static MainAction CreateMainAction(string details) {...}
public static QuickAction CreateQuickAction(string details) {...}

The implementation of these methods use details to create the specific subclasses. The clients should never need to cast; rather, they rely on polymorphism.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM