标签: factory-pattern

Factory Method模式,用于避免基于条件逻辑的对象实例化

在下面的场景中,对象需要根据某些条件逻辑进行实例化,工厂方法模式是否有助于避免客户端代码由于if/elseif条件的数量而变得混乱(如果越来越多,这也将是一个维护噩梦由于逻辑的不同变化,需要创建产品.

或者是否有任何其他设计模式可以拯救?

public interface IProduct
{
    void Method1();
}

public class ProductA : IProduct
{
    void Method1()
    {
    }
}

public class ProductB : IProduct
{
    void Method1()
    {
    }
}

public class ProductC : IProduct
{
    void Method1()
    {
    }
}

public class Client
{
    public void Test()
    {
        int count = 5;

        IProduct product;

        if (count < 10)
        {
            product = new ProductA();
        }
        else if (count == 10)
        {
            product = new ProductB();
        }
        else …
Run Code Online (Sandbox Code Playgroud)

design-patterns factory-pattern

0
推荐指数
1
解决办法
1812
查看次数

设计模式名称:它是工厂吗?

以下类显示了与实际用例类似的内容.它始终为同一个线程返回相同的实例.

public class LookingForName {

    private static final ThreadLocal<Something> threadLocal = 
        new ThreadLocal<Something>(){
            @Override
            protected Something initialValue() {
                return getSomethingSpecial(); // not relevant
            }
        };

    /**
     * @return always the same instance of "Something" for the current thread.
     */
    public static Something getInstance() {
        return threadLocal.get();
    }

}
Run Code Online (Sandbox Code Playgroud)

你怎么称呼它?它是"工厂"吗?"价值持有者"?"ThreadLocalStore"?

java design-patterns naming-conventions thread-local factory-pattern

0
推荐指数
1
解决办法
610
查看次数

Delphi:如何隐藏祖先方法?

这是我之前关于如何隐藏继承的构造函数的问题的变体.你如何隐藏继承的方法:

在Delphi允许您构造COM对象的方式之后进行建模:

CoDOMDocument = class
   class function Create: IXMLDOMDocument2;
end;
Run Code Online (Sandbox Code Playgroud)

我有一个工厂,创建一个实现接口的对象:

CoCondition = class
public
   class function Create: ICondition;
end;
Run Code Online (Sandbox Code Playgroud)

这很好用.虽然在祖先中有一种叫做方法,但它工作正常Create.它的工作原理是因为我没有overload关键字存在.只要我添加overload关键字:Delphi将允许继承的Create方法"闪耀":

CoCondition = class
public
   class function Create: ICondition; overload;
end;
Run Code Online (Sandbox Code Playgroud)

所以现在CoCondition有两种Create方法可用:

class function CoCondition.Create: ICondition;
constructor TObject.Create;
Run Code Online (Sandbox Code Playgroud)

你要打电话给哪一个是模棱两可的.修复,显然是没有overload关键字(为什么你,你没有重载任何东西?).好吧,事实证明我正在超载的东西:

CoCondition = class
public
   class function Create: ICondition; overload;
   class function Create(const ConditionType: TConditionType): ICondition; overload;
   class function Create(const PropertyName: string; …
Run Code Online (Sandbox Code Playgroud)

delphi inheritance factory factory-pattern

0
推荐指数
2
解决办法
3799
查看次数

用PHP构建"工厂"

我创建了一个File类,它负责文件,I/O上的所有操作,并根据文件的性质采取不同的操作.我对它的实际结构不满意,看起来像这样:

    class File
    {
        function __construct($id)
        {
            $bbnq = sprintf("
                SELECT *
                FROM documents
                WHERE id = %u",
                $id);
            $req = bbnf_query($bbnq);
            $bbn = $req->fetch();
            $this->file_type = $bbn['file_type'];
            $this->file_name = $bbn['file_name'];
            $this->title = $bbn['title'];
        }
        function display()
        {
            return '<a href="'.$this->file_name.'">'.$this->title.'</a>';
        }
    }

    class Image extends File
    {
        function __construct($id)
        {
            global $bbng_imagick;
            if ( $bbng_imagick )
                $this->imagick = true;
            parent::__construct($id);
        }
        function display()
        {
            return '<img src="'.$this->file_name.'" alt="'.$this->title.'" />';
        }
    }
Run Code Online (Sandbox Code Playgroud)

在这里,我首先需要知道文件类型,以确定使用哪个类/子类.
我想要实现相反的目的,即向我的类发送一个ID,它返回一个对应于文件类型的对象.
我最近更新到PHP 5.3,我知道有一些新功能可用于创建"工厂"(后期静态绑定?).我的OOP知识非常简单,所以我想知道是否有一些结构建议,以便创建一个独特的类,它将调用正确的构造函数.

谢谢!

php oop factory-pattern

0
推荐指数
1
解决办法
445
查看次数

如何将这4个功能合二为一?

你可以使用枚举: enum StatusRouter { Stop = 0, Start, Resume, Suspect };

public bool StartSelectedRouter()
{
    for (int i = 0; i < m_listPlatforms.Count; i++)
    {
        if (m_listPlatforms[i].IsCheked)
            m_listPlatforms[i].Start();
    }
    return false;
}
public bool ResumeSelectedRouter()
{
    for (int i = 0; i < m_listPlatforms.Count; i++)
    {
        if (m_listPlatforms[i].IsCheked)
            m_listPlatforms[i].Resume();
    }
    return false;
}
public bool SuspendSelectedRouter()
{
    for (int i = 0; i < m_listPlatforms.Count; i++)
    {
        if (m_listPlatforms[i].IsCheked)
            m_listPlatforms[i].Suspend();
    }
    return false;
}
public bool StopSelectedRouter()
{
    for (int …
Run Code Online (Sandbox Code Playgroud)

c# factory-pattern c#-2.0

0
推荐指数
1
解决办法
82
查看次数

你能自动将一个类的对象转换为子类并在Java中调用重载方法吗?

我有以下设置:

class Base {};
class ImplA extends Base {};
class ImplB extends Base {};
class ImplC extends Base {};

Base baseFactory(int type) {
    switch(type) {
    case 0:
        return new ImplA();
    case 1:
        return new ImplB();
    case 2:
        return new ImplC();
}

Base a = baseFactory(0);
Base b = baseFactory(1);
Base c = baseFactory(2);


List<Base> list = new ArrayList<Base>();

list.add(a);
list.add(b);
list.add(c);

// Somewhere else I have:
interface BaseHandler {
    process(ImplA a);
    process(ImplB b);
    process(ImplC c);
};
Run Code Online (Sandbox Code Playgroud)

现在,我希望能够做的是:

class Processor { …
Run Code Online (Sandbox Code Playgroud)

java polymorphism overloading factory-pattern

0
推荐指数
1
解决办法
110
查看次数

在C++中混淆了delete关键字opearation

我想知道删除是如何工作的?在主要功能我删除了cfact object.但仍然是cfact->Hello()工程,而不是抛出错误.在我发现删除时发现的调试时,cfact释放内存.一旦factory* c2fact = newfun.Newfun("c2_fact");行执行cfact获得一些内存位置.

怎么样?请帮助我理解这个概念..

class factory{

public:
    virtual void Hello() = 0;
};
class c_fact: public factory
{
public:
    void Hello(){
    cout << "class c_fact: public factory"<<endl;
    }
};
class c2_fact: public factory
{
public:
    void Hello(){
    cout << "class c2_fact: public factory"<<endl;
    }
};

class callFun{
public:
    virtual factory* Newfun(string data)
    {
        if(data == "c_fact")
            {return new c_fact;}
        else
            {return new c2_fact;}
    }
};
class newFun:public callFun{ …
Run Code Online (Sandbox Code Playgroud)

c++ virtual-functions new-operator factory-pattern delete-operator

0
推荐指数
1
解决办法
533
查看次数

从Java Factory Pattern返回类型

这可能是一个非常基本的问题,答案非常明显,但我很难搞清楚这一点.

如何知道java工厂模式中涉及的类方法的返回类型.例如,查看下面的代码...什么是方法调用的返回类型以及如何正确地转换它...以及如何为类编写javadoc.

我正在尝试编写一个用户然后可以插入他们的项目的库...

我有一个界面

public interface myInterface
{
     public Object doA();
     public Object doB();
}
Run Code Online (Sandbox Code Playgroud)

具体课程如下

public class concerete1 implements myInterface
{
public concerete1() {
}

@override
public Object doA()
{ return new String("Hello"); }

@override
public Object doB()
     { return "hello".getBytes(); }

}
Run Code Online (Sandbox Code Playgroud)

public class concerete1 implements myInterface
{
public concerete2() {
}

@override
public Object doA()
{ return "hello".getBytes(); }

@override
public Object doB()
{ return new String("Hello"); }

}
Run Code Online (Sandbox Code Playgroud)

我的工厂课程如下

public class factory
{
     private myInterface …
Run Code Online (Sandbox Code Playgroud)

java javadoc factory-pattern

0
推荐指数
1
解决办法
3436
查看次数

Swift:工厂模式,子类方法不可见

我是Swift编程的新手,我在构建Factory设计模式方面遇到了阻碍.以下是我的代码:

protocol IInterface {
   func InterfaceMethod() -> Void
}

public class SubClass: IInterface {
   init() {}

   func InterfaceMethod() { }

   public func SubClassMethod() { }
}

public class Factory() {
    class func createFactory() -> IInterface {
         return SubClass()
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,我试图像下面这样访问它

let mgr = Factory.createFactory()

//calling Interface Method
mgr.InterfaceMethod() -- This works, when i keep a dot (mgr.) it shows the method name
//calling subclass method
mgr.SubClassMethod() -- This doesn't work, when i keep a dot (mgr.) it doesnt even …
Run Code Online (Sandbox Code Playgroud)

factory factory-pattern ios swift

0
推荐指数
1
解决办法
1542
查看次数

具有参数化arity的函数

是否可以编写函数来返回其签名取决于构建器函数的参数的函数?

具体来说,我正在改进我写的原始递归的实现.我希望有类似工厂的函数,数字参数生成一个函数,该函数对元组或长度等于为数字参数传递的参数的列表起作用.目前我pr 1 2 [0,5,13]在运行时通过Either以下方式处理类似于从原始递归角度看的无效语句的情况:

pr :: (Show a) => Int -> Int -> [a] -> Either String a
pr i k args
  | 1 <= i && i <= k && length args == k = Right $ args!!(i-1)
  | i <= 0 = Left "first argument of pr needs to be greater or equal to 1"
  | k < i = Left "first argument of pr needs to be lesser or equal to the …
Run Code Online (Sandbox Code Playgroud)

templates haskell factory-pattern

0
推荐指数
1
解决办法
86
查看次数