# 27_二阶构造模式

2018-02-11 14:03:55来源:https://www.jianshu.com/p/88271f6ed200作者:jacob2359人点击

0. 构造函数的回顾

``#include <stdio.h>class Test{private:    int mi;    int mj;public:    Test(int i, int j)    {        mi = i;                return ;                mj = j;    }    int getI()    {        return mi;    }    int getJ()    {        return mj;    }};int main(){    Test t(1, 2);    printf("t.getI() = %d/n", t.getI());    printf("t.getJ() = %d/n", t.getJ());    return 0;}``

``t.getI() = 1t.getJ() = -1217052672``

1.没有办法判断构造函数的执行结果。

``#include <stdio.h>class Test{private:    int mi;    int mj;    bool mStatus;   // 添加一个状态值public:    Test(int i, int j) : mStatus(false) // 初始值为false    {        mi = i;                return ;                mj = j;                mStatus = true;         // 当构造函数完成时赋值为true    }    int getI()    {        return mi;    }    int getJ()    {        return mj;    }        bool getStatus()        // 获取状态值    {        return mStatus;     }};int main(){    Test t(1, 2);        if(t.getStatus())    {        printf("t.getI() = %d/n", t.getI());        printf("t.getJ() = %d/n", t.getJ());    }    else    {        printf("constructor error!/n");    }        return 0;}``

``constructor error!``

1. 构造函数tips

2. 半成品对象

3. 二阶构造

``#include <stdio.h>class TwoPhaseCons{private:    TwoPhaseCons()  // 第一阶段构造函数    {    }    bool construct()// 第二阶段构造函数    {        return true;    }public:    static TwoPhaseCons* NewInstance(); // 对象创建函数};TwoPhaseCons* TwoPhaseCons::NewInstance(){    TwoPhaseCons* ret = new TwoPhaseCons();        // 若第二阶段构造失败，返回NULL    if( !(ret&&ret->construct()) )    {        delete ret;        ret = NULL;    }        return ret;}int main(){    TwoPhaseCons* obj = TwoPhaseCons::NewInstance();        printf("obj = %p/n", obj);    return 0;}``

``obj = 0x897a008``

IntArray.h

``#ifndef _INTARRAY_H_#define _INTARRAY_H_class IntArray{private:    int m_length;    int* m_pointer;        IntArray(int len);    IntArray(const IntArray& obj);    bool construct();public:    static IntArray* NewInstance(int length);     int length();    bool get(int index, int& value);    bool set(int index ,int value);    ~IntArray();};#endif``

IntArray.cpp

``#include "IntArray.h"IntArray::IntArray(int len){    m_length = len;}bool IntArray::construct(){    bool ret = true;        m_pointer = new int[m_length];        if( m_pointer )    {        for(int i=0; i<m_length; i++)        {            m_pointer[i] = 0;        }    }    else    {        ret = false;    }        return ret;}IntArray* IntArray::NewInstance(int length) {    IntArray* ret = new IntArray(length);        if( !(ret && ret->construct()) )     {        delete ret;        ret = 0;    }            return ret;}int IntArray::length(){    return m_length;}bool IntArray::get(int index, int& value){    bool ret = (0 <= index) && (index < length());        if( ret )    {        value = m_pointer[index];    }        return ret;}bool IntArray::set(int index, int value){    bool ret = (0 <= index) && (index < length());        if( ret )    {        m_pointer[index] = value;    }        return ret;}IntArray::~IntArray(){    delete[]m_pointer;}``

main.cpp

``#include <stdio.h>#include "IntArray.h"int main(){    IntArray* a = IntArray::NewInstance(5);            printf("a.length = %d/n", a->length());        a->set(0, 1);        for(int i=0; i<a->length(); i++)    {        int v = 0;                a->get(i, v);                printf("a[%d] = %d/n", i, v);    }        delete a;        return 0;}``

4. 小结