潇's profile老肖的地盘PhotosBlogListsMore Tools Help

Blog


    October 20

    Abstract Factory

    老肖的简单代码实现如下:

    class AbstractProductA
    {
    public:
        virtual void CreateProduct();
    };

    class ProductA1 : public AbstractProductA
    {
    public:
        void CreateProduct();
    };

    class ProductA2 : public AbstractProductA
    {
    public:
        void CreateProduct();
    };

    class AbstractProductB
    {
    public:
        virtual void CreateProduct();
    };

    class ProductB1 : public AbstractProductB
    {
    public:
        void CreateProduct();
    };

    class ProductB2 : public AbstractProductB
    {
    public:
        void CreateProduct();
    };

    class AbstractFactory
    {
    public:
        virtual AbstractProductA* CreateProductA();
        virtual AbstractProductB* CreateProductB();
    };

    class ConcreteFactoryA : public AbstractFactory
    {
    public:
        AbstractProductA* CreateProductA() { return new ProductA1(); }
        AbstractProductB* CreateProductB() { return new ProductB1(); }
    };

    class ConcreteFactoryB : public AbstractFactory
    {
    public:
        AbstractProductA* CreateProductA() { return new ProductA2(); }
        AbstractProductB* CreateProductB() { return new ProductB2(); }
    };

    适用性:

    一个系统要独立于它的产品的创建、组合和表示时。

    一个系统要由多个产品系列中的一个来配置时。

    当你要强调一系列相关的产品对象的设计一边进行联合使用时。

    点你提供一个产品类库,而只想现实他们的接口而不是实现时。

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://seanxiaoxiao.spaces.live.com/blog/cns!7677365E5B9342DE!635.trak
    Weblogs that reference this entry
    • None