Simple "design mode" of the factory model (C ++)

Foreword

Mode Introduction

Before simple factory pattern, we introduce a simple factory pattern defect is contrary to the open - closed principle. If you add the roasted sea cucumber in the face of the museum, it will modify the waiter factory class. Contrary to the principle of the closed type.

Also to the noodle shop, for example, now two kinds of surface, with a waiter to sell on it, if this waiter quit, sell face behind her need to replace a part-time waiter, but the chef can not leave the stove, it will be turned into a model style window line up, a team of sea cucumber noodles, another team horseradish soup. Each window has a chef were put meal, a sea cucumber noodles will do, will do another horseradish soup. Boss feel that this mode is very good, if to do a grilled sea cucumber, you only need to open a window like grilled sea cucumber, the waiter does not need to re-learn, because it will sell grilled sea cucumber. This turned into a factory mode.

UML class diagrams

This involves two types ① to my client, queuing to buy food. ② chef cooking class, factory class to produce food for me. ③ dishes class, class generated dishes. The specific relationship UML class diagram as follows:

Code examples

Here is the noodle category, is used for the factory class, you can inherit his extended noodle category:

#ifndef NOODLE_H
#define NOODLE_H

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

   virtual void eating() = 0;
};

#endif // NOODLE_H

The following is a sea cucumber noodles class, inherited noodle, eating realization method to eat cucumber noodles:

#ifndef HAISHENNOODLE_H
#define HAISHENNOODLE_H

#include "noodle.h"

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

    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, inherited noodle, eating realization method, spicy root soup:

#ifndef LAGENNOODLE_H
#define LAGENNOODLE_H

#include "noodle.h"

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

    virtual void eating();
};

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

lagennoodle::lagennoodle()
{

}

lagennoodle::~lagennoodle()
{

}

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

The following is the base class factory waiter. All plants have inherited this class:

#ifndef WAITER_H
#define WAITER_H

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

    virtual noodle *createnoodle() = 0;
};

#endif // WAITER_H

The following is a sea cucumber chef (Plant 1), sea cucumber sea cucumber noodles cook just do, rewrite createnoodle method:

#ifndef HAISHEN_H
#define HAISHEN_H

#include "waiter.h"

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

    virtual noodle *createnoodle();
};

#endif // HAISHEN_H
#include <iostream>
#include "haishen.h"
#include "haishennoodle.h"

haishen::haishen()
{

}

haishen::~haishen()
{

}

noodle *haishen::createnoodle()
{
    std::cout << "面是我炒得,我的名字叫海参!!!" << std::endl;
    return new haishennoodle();
}

Here is horseradish chef (Plant 1), horseradish horseradish soup cook just do, rewrite createnoodle method:

#ifndef LAGEN_H
#define LAGEN_H

#include "waiter.h"

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

    virtual noodle *createnoodle();
};

#endif // LAGEN_H
#include <iostream>
#include "lagen.h"
#include "lagennoodle.h"

lagen::lagen()
{

}

lagen::~lagen()
{

}

noodle *lagen::createnoodle()
{
    std::cout << "吃辣根汤面,你不觉得呛得哼吗??" << std::endl;
    return new lagennoodle();
}

The following is a client, the client category by using the corresponding class factory to establish the appropriate instance:

#include <iostream>
#include <string.h>

#include "haishen.h"
#include "lagen.h"
#include "noodle.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 = NULL;
    noodle *n = NULL;

    for(p = product_list[i]; p != NULL; i++, p = product_list[i]) {
        if(strncmp(pd, p, strlen(pd)) == 0) {
           if(i == 0) {
               w = new haishen();
           } else if(i == 1) {
               w = new lagen();
           } else {
               cout << "对不起,请您排在队内!!!" << std::endl;
               break;
           }

        }
    }

    if(w) n = w->createnoodle();
    if(n) n->eating();

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

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

Here is CMakeList.txt file, help generate Makefile:

cmake_minimum_required(VERSION 2.8)

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

Compile and run results

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

erguangqiang@elab$./noodle-factory
面是我炒得,我的名字叫海参!!!
我是海参炒面,里面没有海参哦!!吃的时候注意!

End

Factory pattern to solve simple factory violated the open - closed principle. Although tired structure becomes complicated, but for the expansion has been greatly improved.

Guess you like

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