drj58429 2015-09-07 15:25
浏览 39
已采纳

非抽象产品的方法工厂

I was implementing the Factory Method pattern, but checking several examples out there I couldn't determinate if extending a concrete class for the product instead of creating an abstract class or interface is correct... This is just intended for an example (PHP).

So, I have my abstract factory and their concrete factories:

interface BookFactoryInterface
{
    public function createBook();
}

class ElectronicBookFactory implements BookFactoryInterface
{
    public function createBook()
    {
        return new ElectronicBook();
    }
}

class PaperBookFactory implements BookFactoryInterface
{
    public function createBook()
    {
        return new PaperBook();
    }
}

Now what I have seen in all the examples is that usually products extend from an abstract class or an interface, but when I was testing I realized that in my case there was not need for that, what I was needing is a concrete class with the common behavior and then my subclasses with the rest.

class Book
{
    /* 
      All properties like title, authors, publicationDate
      here, with the corresponding methods.
    */
}

class ElectronicBook extends Book
{
    //...
}

class PaperBook extends Book
{
    //...
}

The class instantiation is still been derived to subclasses, so I really believed this is a Factory Method implementation, but I could find another example code in this way.

So the question: is this still a Factory Method implementation? if not Why?

  • 写回答

2条回答 默认 最新

  • duanguangsong2380 2015-09-07 15:30
    关注

    This is very subjective, but I would argue you don't need any inheritence for it to be a factory method. This would actually suffice to meet the definition:

    class Book { 
    
    }
    
    class BookFactory {
    
       function createBook() {
    
         return new Book();    
    
       }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退