Simple "design mode" of simple factory pattern (C ++)

Foreword

Mode Introduction

Simple plant model is actually not part of GoF23 (23 design patterns), more similar to a variant of the factory model. Is defined as instances of different classes may be returned in accordance with different parameters. Simple factory pattern to define a special class to other classes responsible for creating an instance, the instance is created normally have a common parent class.
It's a bit like going to a restaurant, the waiter at the door and say: "!! Waiter to a sea cucumber noodles" Under normal circumstances he would give you the sea cucumber noodles (no matter there are no sea cucumber). This second man came in, also shouted: "Waiter, come bowl of horseradish soup!" At this point he will give you a horseradish soup. You would not even consider cucumber and horseradish soup noodles fried loud noise, just need to tell the store, he gave you.
The above example of "servant" is a simple plant. The restaurant menu is a simple list of classes in the factory. By which point the waiter told the class, the waiter will be an instance of the class is the end come out of dishes. So for me, this client is concerned, Houchu do not need to know anything, just to get enough to eat dishes instance. If one day the sea cucumber noodles into a barbecue restaurant, but I did not need to know how Houchu string string children, also only need to go find the attendant to kidney OK.

UML class diagrams

Here involves two types, ① I: The client is responsible for calling the waiter dishes generation backend instance. ② Waiter: factory class is responsible for generating the back-end dishes and returned to the client. Dishes ③: After serve later category, generating dishes. The specific relationship UML class diagram as follows:

Code examples

The following is a noodle class is an abstract class, which includes a function of eating, the client is prepared to eat.

#ifndef NOODLE_H
#define NOODLE_H

class noodle {
public:
    noodle() {}
    ~noodle() {}

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

#endif // NOODLE_H

The following is a sea cucumber noodles class, integrated self-noodle category, achieving eating method:

#ifndef HAISHENNOODLE_H
#define HAISHENNOODLE_H

#include "noodle.h"

class haishennoodle : public noodle
{
public:
    haishennoodle();
    ~haishennoodle();

public:
    virtual void eating();
};

#endif // HAISHENNOODLE_H
#include <iostream>
#include "haishennoodle.h"

haishennoodle::haishennoodle()
{

}

haishennoodle::~haishennoodle()
{

}

void haishennoodle::eating()
{
    std::cout << "我是海参炒面,里面没有海参哦!!吃的时候注意!" << std::endl;
}

Here is horseradish soup class, inherited the noodle category, rewrite eating method:

#ifndef LAGENNOODLE_H
#define LAGENNOODLE_H

#include "noodle.h"

class lagennoodle : public noodle
{
public:
    lagennoodle();
    ~lagennoodle();

public:
    virtual void eating();
};

#endif // LAGENNOODLE_H
#include <iostream>
#include "lagennoodle.h"

lagennoodle::lagennoodle()
{

}

lagennoodle::~lagennoodle()
{

}

void lagennoodle::eating()
{
    std::cout << "我是辣根汤面,吃完呛的哼啊!!!" << std::endl;
}

Here is the kind waiter, responsible for generating back-end noodle. By generating a noodle createnoodle instantiated, factory.

#ifndef WAITER_H
#define WAITER_H

class noodle;
class waiter
{
public:
    waiter();
    ~waiter();

public:
    noodle *createnoodle(int type);
};

#endif // WAITER_H
#include <iostream>
#include "waiter.h"
#include "haishennoodle.h"
#include "lagennoodle.h"

waiter::waiter()
{

}

waiter::~waiter()
{

}

noodle *waiter::createnoodle(int type)
{
    noodle *n = NULL;
    switch(type) {
    case 0:
        n = new haishennoodle();
        break;
    case 1:
        n = new lagennoodle();
        break;
     default:
        std::cout << "对不起,我们这没有这个菜,请您换一个!" << std::endl;
    }

    return n;
}

The client code is as follows, I come into the store ordering steps:

#include <iostream>
#include <string.h>
#include "haishennoodle.h"
#include "lagennoodle.h"
#include "waiter.h"

using namespace std;

char *product_list[] = {
    "haishen-noodle",
    "lagen-noodle",
    NULL
};

int main()
{
    char *p = NULL;
    char *pd = "haishen-noodle";
    int i = 0;

    waiter *w = new waiter();
    noodle *n = NULL;

    for(p = product_list[i]; p != NULL; i++, p = product_list[i]) {
        if(strncmp(pd, p, strlen(pd)) == 0) {
            n = w->createnoodle(i);
            if(n) {
                cout << "开吃!!!" << endl;
                n->eating();
            }
        }
    }

    if(n) {
        delete n; n = NULL;
    }

    if(w) {
        delete w; w = NULL;
    }

    return 0;
}

Bonus CMakeList.txt Code:

cmake_minimum_required(VERSION 2.8)

project(noodle)
set(SRC_LIST main.cpp noodle.h lagennoodle.h lagennoodle.cpp haishennoodle.h haishennoodle.cpp waiter.h waiter.cpp)
add_executable(${PROJECT_NAME} ${SRC_LIST})

Compile and run results

Download link is: https: //github.com/erguangqiang/freesir_headfirst/blob/master/noodle.tar.gz
use cmake to generate Makefile, and compile an executable program noodle. Results are as follows:

erguangqiang@elab$./noodle
开吃!!!
我是海参炒面,里面没有海参哦!!吃的时候注意!

End

Advantage is simple factory pattern may be isolated and instantiates the client, so the client can ignore class instance of specific processes, to achieve the purpose of decoupling the logic and the client.
However, simple factory pattern is also very serious drawback, according to the design pattern of the open - closed principle, for the extension, not to alter the specific code, the class of integrated extension can be opened. Once a new noodle dishes, we need to modify the factory class. The method is to avoid the use of the factory model, it is achieved by way of extension.

Guess you like

Origin www.cnblogs.com/freeman2012/p/11206128.html