【问题标题】:Is this an example of a factory or strategy?这是工厂或战略的一个例子吗?
【发布时间】:2016-09-29 22:25:24
【问题描述】:

假设以下类在运行时从依赖注入容器解析的一组给定的实例化单例对象中进行选择:

class myClass {

    private container;   // DI container

    function getMeAnObject(input) {
        switch(input) {
            case A: return this.container.get(singletonA);
            case B: return this.container.get(singletonB);
            // etc...
        }   
    }
}

在这个例子中,myClass 是工厂实现还是策略实现,或者两者都不是?为什么?

【问题讨论】:

  • “这个问题没有显示任何研究成果。” 认为答案是什么?为什么?

标签: design-patterns singleton factory strategy-pattern


【解决方案1】:

因为类的目标是返回一个对象,所以我认为它是一个工厂。虽然策略也可能返回一个对象,但我希望它更多地关注行为的特定方面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多