开发者

What is a name of that pattern?

What is a name of that pattern ? I guess, it's a factory, but i am not sure.

using System.Windows.Forms;

    public class TreeNodeHelper
    {
        public TreeNode GetTreeNodeType1()
        {
            return new TreeNode("type1");
        }

        public TreeNode GetTreeNodeType2()
        {
            return new TreeNode("type2");
        }

        // etc
    }

TreeNodeHelper class returns different instances of TreeNode. It only returns TreeNodes instances and nothi开发者_运维百科ng else.


It's a factory of kinds, but not the factory pattern.

Also note that the factory pattern(or variants), usually only have one method, with one (or more) parameter(s) with which it can decide for which type to create an instance, not two (or more) methods.

I would also rename the methods to something like "CreateNodeInstance" or something similar. You're creating and returning instances, not retrieving types.

Edit

Without knowing your requirements completely, a simple modification would be something along the lines of

static public class TreeNodeHelper
{
    static public TreeNode CreateNodeInstance(criterion)
    {
         if (criterion == xyz)
         {
             return new XyzTreeNode();
         }
         else if (criterion == foo)
         {
             return new FooTreeNode();
         }
         else if (etc...etc...
    }
}

This would be an implementation of the factory method pattern, not the abstract factory pattern. The latter link also contains an example in C#, but I doubt if you will need the full abstract factory implementation.


It creates and returns objects, so it's some variant of a Factory or Builder. Since it returns simple objects, and not complex ones, it's a Factory variant.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜