抽象工厂例子

//简单工厂
/*
当新添加一个操作时,需要添加新的实现类和在工厂方法的switch中添加,打破了开放封闭原则
*/
#if 0
class operation
{
public:
    virtual int  GetResult(int nParam1, int nParam2) = 0;
};

enum Oper
{
    eOper_Add,
    eOper_Sub,
};

class operationAdd : public operation
{
    int GetResult(int nParam1, int nParam2)
    {
        return nParam1 + nParam2;
    }
};

class operationSub : public operation
{
    int GetResult(int nParam1, int nParam2)
    {
        return nParam1 - nParam2;
    }
};

class Factory
{
public:
    operation* CreateOperate(Oper oper);
};

operation* Factory::CreateOperate(Oper oper)
{
    switch (oper)
    {
    case eOper_Add:
        return new operationAdd;
        break;
    case eOper_Sub:
        return new operationSub;
        break;
    default:
        break;
    }
    return nullptr;
}

int main()
{
    Factory fa;
    operation* op = fa.CreateOperate(eOper_Add);
    int sum = op->GetResult(1, 2);
    return 0;
}

#endif

//工厂方法
#if 0
/*
1.定义创建对象的接口,让子类决定实例化哪一个类
2.将简单工厂改变成一个抽象工厂和多个具体生产对象的工厂
3.当我们需要添加新产品时,需要添加产品类和相应的具体工厂类
4.工厂方法是由使用者决定实例化哪一个工厂,简单工厂在内存自己判断逻辑,而现在是把这个逻辑放在了使用者身上

缺点:
没增加一个产品就需要增加一个产品工厂的类
*/

class operation
{
public:
    virtual int  GetResult(int nParam1, int nParam2) = 0;
};

/// 具体产品类
class operationAdd : public operation
{
    int GetResult(int nParam1, int nParam2)
    {
        return nParam1 + nParam2;
    }
};

class operationSub : public operation
{
    int GetResult(int nParam1, int nParam2)
    {
        return nParam1 - nParam2;
    }
};

//抽象工厂
class IFactory
{
public:
    //定义了一个创建一个对象的接口
    virtual operation* CreateOperation() = 0;
};

///具体工厂方法
class AddFactory : public IFactory
{
public:
    operation* CreateOperation()
    {
        return new operationAdd;
    }
};

//减法工厂
class SubFactory : public IFactory
{
public:
    operation* CreateOperation()
    {
        return new operationSub;
    }
};

int main()
{
    IFactory* pFactory = new AddFactory;
    operation* pOp = pFactory->CreateOperation();
    int sum = pOp->GetResult(1, 2);
    return 0;
}
#endif


/*
抽象工厂方法有多个抽象产品类,而工厂方法只有一个抽象产品类
对于工厂方法工厂只能创建一类产品,而抽象工厂能创建多种产品
*/
#include <iostream>
struct User{};
class IUser
{
public:
    virtual void Insert(User* user) = 0;
    virtual User* GetUser(int nId) = 0;
};

class SqlserverUser : public IUser
{
public:
    void Insert(User* user)
    {
        std::cout << "SqlServer中User插入记录" << std::endl;
    }
    User* GetUser(int nId)
    {
        std::cout << "SqlServer中查询一条记录" << std::endl;
        return nullptr;
    }
};

class AcessUser : public IUser
{
public:
    void Insert(User* user)
    {
        std::cout << "Acess中User插入记录" << std::endl;
    }
    User* GetUser(int nId)
    {
        std::cout << "Acess中查询一条记录" << std::endl;
        return nullptr;

    }
};

struct Department{};
class IDepartment
{
public:
    virtual void Insert(Department user) = 0;
    virtual Department* GetUser(int nId) = 0;
};



class SqlserverDepartment : public IDepartment
{
public:
    void Insert(Department user)
    {
        std::cout << "SqlServer中Department插入记录" << std::endl;
    }
    Department* GetUser(int nId)
    {
        std::cout << "SqlServer中查询一条记录" << std::endl;
        return nullptr;
    }
};

class AcessDepartment : public IDepartment
{
public:
    void Insert(Department user)
    {
        std::cout << "Acess中Department插入记录" << std::endl;
    }
    Department* GetUser(int nId)
    {
        std::cout << "Acess中查询一条记录" << std::endl;
        return nullptr;

    }
};


//抽象工厂能创建多种产品
class IFactory
{
public:
    virtual IUser* CreateUser() = 0;
    virtual IDepartment* CreateDepart() = 0;
};

class SqlServerFactory : public IFactory
{
public:
    IUser* CreateUser()
    {
        return new SqlserverUser;
    }

    IDepartment* CreateDepart()
    {
        return new SqlserverDepartment;
    }
};

class AcessFactory : public IFactory
{
public:
    IUser* CreateUser()
    {
        return new AcessUser;
    }
    IDepartment* CreateDepart()
    {
        return new AcessDepartment;
    }
};

int main()
{
    User* user = new User;
    IFactory* pFact = new SqlServerFactory;
    IUser* pUser = pFact->CreateUser();

    pUser->GetUser(1);
    pUser->Insert(user);
    getchar();
}

 

posted @ 2015-03-20 17:04  zzyoucan  阅读(256)  评论(0编辑  收藏  举报