十四、建造者模式

在这里插入图片描述

#include<iostream>
#include<list>
#include<string>
using namespace std;

class product
{
	list<string>ls;
public:
	void add(string m_str)
	{
		ls.push_back(m_str);
	}
	void show()
	{
		list<string>::iterator itr = ls.begin();
		for(;itr!=ls.end();++itr)
		{ cout<<*itr<<endl;
		}
	}
};
class builder
{
public:
	virtual void head(){};
	virtual void body(){};
	virtual void leg(){};
	virtual void hand(){};
	virtual product getresult(){product s;return s;};
};

class  conbuild1:public builder
{
private:
	product *m_product;
public:
	conbuild1()
	{
		m_product = new product();
	}
	void head()
	{
		m_product->add("1的头");
	}
	void body()
	{
		m_product->add("1的身体");
	}
	void leg()
	{
		m_product->add("1的腿");
	}
	void hand()
	{
		m_product->add("1的手");
	}
	product getresult()
	{
		return *m_product;
	}
};

class  conbuild2:public builder
{
private:
	product *m_product;
public:
	conbuild2()
	{
		m_product = new product();
	}
	void head()
	{
		m_product->add("2的头");
	}
	void body()
	{
		m_product->add("2的身体");
	}
	void leg()
	{
		m_product->add("2的腿");
	}
	void hand()
	{
		m_product->add("2的手");
	}
	product getresult()
	{
		return *m_product;
	}
};

class director
{
public:
	void createpeople(builder *s)
	{
		s->body();
		s->hand();
		s->leg();
		s->head();
	}
};

int main()
{
	director s;
	builder *p1 = new conbuild1;
	builder*p2 = new conbuild2;
	s.createpeople(p1);
	product s1 = p1->getresult();
	s1.show();
	cout<<"---------------"<<endl;
	s.createpeople(p2);
	product s2 = p2->getresult();
	s2.show();
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119

文章来源: blog.csdn.net,作者:IM-STONE,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/doubleintfloat/article/details/86530796

(完)