Abstract Factory (C ++ to achieve)

(This article aims to review personal knowledge)

1, the details:

Abstract factory pattern: create a series of related or dependent objects interface, without specifying their concrete classes.

Character:

Abstract Factory role: It is the core of the abstract factory pattern, it has nothing to do with the application. Is the specific role of the factory must implement or inherit the parent class.

Plant-specific roles: Include specific business logic-related code. Call to create the corresponding objects by the application specific products. It is achieved by the particular class.

Abstract Product role: the parent class to achieve specific products or inherited.

Examples of this role of specific factory objects created role: the role of specific products.

Advantages: ① facilitates the exchange of products; ② make specific separation process creates an instance of the client.

Disadvantages: New product family more complex, the need to change the abstract factory class category corresponds to a specific increase adding factory class, an abstract class of products, specific product class.

Objective: The client provides a unified interface, you can create more complex product family of product objects.

2, UML class diagram:

 

3, code implementation:

Mouse.h (abstract product class 1)

#pragma once
class Mouse
{
public:
    Mouse();
    ~Mouse();

public:
    virtual void showProperty1() = 0;
};

Mouse.cpp

#include "stdafx.h"
#include "Mouse.h"

Mouse::Mouse()
{
}

Mouse::~Mouse()
{
}

DellMouse.h (specific product 1)

#pragma once
#include "Mouse.h"
class DellMouse :
    public Mouse
{
public:
    DellMouse();
    ~DellMouse();

public:
    void showProperty1();
};

DellMouse.cpp

#include "stdafx.h"
#include "DellMouse.h"

DellMouse::DellMouse()
{
}

DellMouse::~DellMouse()
{
}

void DellMouse::showProperty1()
{
    cout << "This is the Dell Mouse" << endl;
}

HPMouse.h (specific products 2)

#pragma once
#include "Mouse.h"
class HPMouse :
    public Mouse
{
public:
    HPMouse();
    ~HPMouse();

public:
    void showProperty1();
};

HPMouse.cpp

#include "stdafx.h"
#include "HPMouse.h"

HPMouse::HPMouse()
{
}

HPMouse::~HPMouse()
{
}

void HPMouse::showProperty1()
{
    cout << "This is HP Mouse!" << endl;
}

Keyboard.h (abstract product 2)

#pragma once
class Keyboard
{
public:
    Keyboard();
    ~Keyboard();
public:
    virtual void showProperty2()=0;
};

Keyboard.cpp

#include "stdafx.h"
#include "Keyboard.h"

Keyboard::Keyboard()
{
}

Keyboard::~Keyboard()
{
}

DellKeyboard.h (specific product 1)

#pragma once
#include "Keyboard.h"
class DellKeyboard :
    public Keyboard
{
public:
    DellKeyboard();
    ~DellKeyboard();

public:
    void showProperty2();
};

DellKeyboard.cpp

#include "stdafx.h"
#include "DellKeyboard.h"

DellKeyboard::DellKeyboard()
{
}

DellKeyboard::~DellKeyboard()
{
}

void DellKeyboard::showProperty2()
{
    cout << "This is Dell Keyboard!" << endl;
}

HPKeyboard.h (specific products 2)

#pragma once
#include "Keyboard.h"
class HPKeyboard :
    public Keyboard
{
public:
    HPKeyboard();
    ~HPKeyboard();

public:
    void showProperty2();
};

HPKeyboard.cpp

#include "stdafx.h"
#include "HPKeyboard.h"

HPKeyboard::HPKeyboard()
{
}

HPKeyboard::~HPKeyboard()
{
}

void HPKeyboard::showProperty2()
{
    cout << "This is HP Keyboard!" << endl;
}

AbstractFactory.h (abstract factory class)

#pragma once
#include "Mouse.h"
#include "Keyboard.h"

class AbstractFactory
{
public:
    AbstractFactory();
    ~AbstractFactory();

public:
    virtual Mouse* getMouse(ProductType eProductType) = 0;
    virtual Keyboard* getKeyboard(ProductType eProductType) = 0;
};

AbstractFactory.cpp

#include "stdafx.h"
#include "AbstractFactory.h"

AbstractFactory::AbstractFactory()
{
}

AbstractFactory::~AbstractFactory()
{
}

MouseFactory.h (concrete factory class 1)

#pragma once
#include "AbstractFactory.h"
class MouseFactory :
    public AbstractFactory
{
public:
    MouseFactory();
    ~MouseFactory();

public:
    Mouse* getMouse(ProductType eProductType);
    Keyboard* getKeyboard(ProductType eProductType);
};

MouseFactory.cpp

#include "stdafx.h"
#include "MouseFactory.h"
#include "DellMouse.h"
#include "HPMouse.h"

MouseFactory::MouseFactory()
{
}

MouseFactory::~MouseFactory()
{
}

Mouse* MouseFactory::getMouse(ProductType eProductType)
{
    switch (eProductType)
    {
    case PT_DELL:
        return new DellMouse();
        break;
    case PT_HP:
        return new HPMouse();
        break;
    default:
        return NULL;
        break;
    }
}

Keyboard* MouseFactory::getKeyboard(ProductType eProductType)
{
    return NULL;
}

KeyboardFactory.h (concrete factory class 2)

#pragma once
#include "AbstractFactory.h"

class KeyboardFactory :
    public AbstractFactory
{
public:
    KeyboardFactory();
    ~KeyboardFactory();

public:
    Mouse* getMouse(ProductType eProductType);
    Keyboard* getKeyboard(ProductType eProductType);
};

KeyboardFactory.cpp

#include "stdafx.h"
#include "KeyboardFactory.h"
#include "DellKeyboard.h"
#include "HPKeyboard.h"

KeyboardFactory::KeyboardFactory()
{
}

KeyboardFactory::~KeyboardFactory()
{
}

Mouse* KeyboardFactory::getMouse(ProductType eProductType)
{
    return NULL;
}

Keyboard* KeyboardFactory::getKeyboard(ProductType eProductType)
{
    switch (eProductType)
    {
    case PT_DELL:
        return new DellKeyboard();
        break;
    case PT_HP:
        return new HPKeyboard();
        break;
    default:
        break;
        return NULL;
    }
}

the stdafx.h (precompiled headers)

// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#include <iostream>
using namespace std;
enum ProductType{ PT_DELL, PT_HP };

// TODO:  在此处引用程序需要的其他头文件

Calling code:

// FactoryMethodMemo.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "MyAlgorithm.h"
#include "AddFactory.h"
#include "SubFactory.h"
#include "MulFactory.h"

#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    MyAlgorithm* pAlgorithmm = NULL;
    Factory* pFactory = new AddFactory();
    pAlgorithmm = pFactory->GetAlgorithm();
    cout << "add:" << pAlgorithmm->GetResult(3.0, 5.0) << endl;
    delete pAlgorithmm;
    pAlgorithmm = NULL;
    delete pFactory;
    pFactory = NULL;

    pFactory = new SubFactory();
    pAlgorithmm = pFactory->GetAlgorithm();
    cout << "sub:" << pAlgorithmm->GetResult(3.0, 5.0) << endl;
    delete pAlgorithmm;
    pAlgorithmm = NULL;
    delete pFactory;
    pFactory = NULL;

    pFactory = new MulFactory();
    pAlgorithmm = pFactory->GetAlgorithm();
    cout << "mul:" << pAlgorithmm->GetResult(3.0, 5.0) << endl;
    delete pAlgorithmm;
    pAlgorithmm = NULL;

    system("pause");
    return 0;
}

Output:

 

Guess you like

Origin blog.csdn.net/qq_23903863/article/details/90643942