如果我在实现工厂模式时使用抽象类而不是接口。它仍然是工厂模式吗? [英] If I use abstract class instead of interface while implementing factory pattern. Would it still be a factory pattern?

查看:651
本文介绍了如果我在实现工厂模式时使用抽象类而不是接口。它仍然是工厂模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如: http://www.tutorialspoint.com/design_pattern/factory_pattern.htm



如果我在抽象类Shape上更改界面形状,则使具体类扩展Shape并使Shape工厂返回Shape抽象类类型对象。是否仍然是一个工厂模式?

解决方案

我会与yes一起去。



让我们看看Factory方法模式的定义:


工厂方法模式是一个创造性

动机此模式之后是使用该对象将客户机的对象创建分开。客户端应该向工厂提供规范,但详细说明对象的构造是如何由工厂抽象出来的。



如果这是一个接口或抽象类,则是具体情况的实现细节只要您实现该工厂就可以实现模式背后的动机。



如果任何这些语句适用于您的情况,请考虑使用抽象类:



  • 您希望在几个密切相关的课程中共享代码。


  • 您希望扩展抽象类的类具有许多常见的方法或字段,或者需要除public之外的访问修饰符(例如受保护和私有)。


  • 您要声明非静态或非最终字段。这使您能够定义可以访问和修改其所属对象状态的方法。



如果任何这些语句适用于您的情况,请考虑使用接口:



  • 你期望不相关的类将实现你的接口。例如,可比较和可克隆的接口由许多不相关的类实现。


  • 你想指定特定数据类型的行为,但不关心谁


  • 您想要利用类型的多重继承。



在某些实现中,使用抽象类而不是工厂创建的产品的界面可能更有意义。如果所有产品之间共享一组功能/行为,那么将它们放入基础抽象类是有意义的。这可能适用,即使产品是由不同的工厂生产的。



它归结为:您是否希望在产品之间引入耦合
或不?
最终,客户端将获得相同的结果 - 根据规范生成产品,并将构造的细节抽象出来。


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 ?

解决方案

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.

这篇关于如果我在实现工厂模式时使用抽象类而不是接口。它仍然是工厂模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆