Advertisement
Razzim

wzorcebuilder Truszczak

Nov 7th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. class Zamowienie{
  7. private:
  8. string wymiar;
  9. string material;
  10. string kolor;
  11. string typ;
  12.  
  13. public:
  14. void setwymiar(const string& s)
  15. {
  16. this->wymiar = s;
  17. }
  18. void setmaterial(const string& s)
  19. {
  20. this->material = s;
  21. }
  22. void setkolor(const string& s)
  23. {
  24. this->kolor = s;
  25. }
  26. void settyp(const string& s)
  27. {
  28. this->typ = s;
  29. }
  30. void show(){
  31. cout <<endl;
  32. cout << typ << ": " <<endl;
  33. cout << "Dlugosc = " << wymiar << endl;
  34. cout << "Material = " << material << endl;
  35. cout << "Kolor = " << kolor <<endl;
  36. }
  37. Zamowienie(){}
  38. ~Zamowienie(){}
  39.  
  40. };
  41.  
  42. class Builder{
  43. public:
  44. virtual void BuildWymiar()=0;
  45. virtual void BuildMaterial()=0;
  46. virtual void BuildKolor()=0;
  47. virtual void BuildTyp()=0;
  48. virtual Zamowienie* GetProduct()=0;
  49. Builder(){}
  50. virtual ~Builder(){}
  51. };
  52.  
  53. class BMarynarka:public Builder{
  54. public:
  55. BMarynarka(){
  56. this->m_zam = new Zamowienie();
  57. }
  58. ~BMarynarka(){
  59.  
  60. }
  61. virtual void BuildWymiar(){
  62. int k;
  63. while(true)
  64. {
  65. cout << "Wybierz wymiar marynarki: (1) 50 cm, (2) 100 cm , (3) 150 cm" << endl;
  66. cin >> k;
  67. if(k>0 && k<4)
  68. break;
  69. }
  70. string wyb;
  71. if (k==1) wyb = "50 cm";
  72. else if (k==2) wyb = "100 cm";
  73. else if (k==3) wyb = "150 cm";
  74. this->m_zam->setwymiar(wyb);
  75. }
  76. virtual void BuildMaterial(){
  77. int k;
  78. while(true)
  79. {
  80. cout << "Wybierz material marynarki: (1) Welna, (2) Bawelna , (3) Skora Krokodyla" << endl;
  81. cin >> k;
  82. if(k>0 && k<4)
  83. break;
  84. }
  85. string wyb;
  86. if (k==1) wyb = "Welna";
  87. else if (k==2) wyb = "Bawelna";
  88. else if (k==3) wyb = "Skora Krokodyla";
  89. this->m_zam->setmaterial(wyb);
  90. }
  91. virtual void BuildKolor(){
  92. int k;
  93. while(true)
  94. {
  95. cout << "Wybierz kolor marynarki: (1) Blue, (2) Black , (3) Pink" << endl;
  96. cin >> k;
  97. if(k>0 && k<4)
  98. break;
  99. }
  100. string wyb;
  101. if (k==1) wyb = "Blue";
  102. else if (k==2) wyb = "Black";
  103. else if (k==3) wyb = "Pink";
  104. this->m_zam->setkolor(wyb);
  105. }
  106. virtual void BuildTyp(){
  107. this->m_zam->settyp("Marynarka");
  108. }
  109. virtual Zamowienie* GetProduct(){
  110. return this->m_zam;
  111. }
  112. private:
  113. Zamowienie* m_zam;
  114.  
  115. };
  116.  
  117. class BSpodnie:public Builder{
  118. public:
  119. BSpodnie(){
  120. this->m_zam = new Zamowienie();
  121. }
  122. ~BSpodnie(){
  123.  
  124. }
  125. virtual void BuildWymiar(){
  126. int k;
  127. while(true)
  128. {
  129. cout << "Wybierz wymiar spodni: (1) 50 cm, (2) 100 cm , (3) 150 cm" << endl;
  130. cin >> k;
  131. if(k>0 && k<4)
  132. break;
  133. }
  134. string wyb;
  135. if (k==1) wyb = "50 cm";
  136. else if (k==2) wyb = "100 cm";
  137. else if (k==3) wyb = "150 cm";
  138. this->m_zam->setwymiar(wyb);
  139. }
  140. virtual void BuildMaterial(){
  141. int k;
  142. while(true)
  143. {
  144. cout << "Wybierz material spodni: (1) Welna, (2) Bawelna , (3) Skora Krokodyla" << endl;
  145. cin >> k;
  146. if(k>0 && k<4)
  147. break;
  148. }
  149. string wyb;
  150. if (k==1) wyb = "Welna";
  151. else if (k==2) wyb = "Bawelna";
  152. else if (k==3) wyb = "Skora Krokodyla";
  153. this->m_zam->setmaterial(wyb);
  154. }
  155. virtual void BuildKolor(){
  156. int k;
  157. while(true)
  158. {
  159. cout << "Wybierz kolor spodni: (1) Blue, (2) Black , (3) Pink" << endl;
  160. cin >> k;
  161. if(k>0 && k<4)
  162. break;
  163. }
  164. string wyb;
  165. if (k==1) wyb = "Blue";
  166. else if (k==2) wyb = "Black";
  167. else if (k==3) wyb = "Pink";
  168. this->m_zam->setkolor(wyb);
  169. }
  170. virtual void BuildTyp(){
  171. this->m_zam->settyp("Spodnie");
  172. }
  173. virtual Zamowienie* GetProduct(){
  174. return this->m_zam;
  175. }
  176. private:
  177. Zamowienie* m_zam;
  178.  
  179. };
  180.  
  181. class BSpodnica:public Builder{
  182. public:
  183. BSpodnica(){
  184. this->m_zam = new Zamowienie();
  185. }
  186. ~BSpodnica(){
  187.  
  188. }
  189. virtual void BuildWymiar(){
  190. int k;
  191. while(true)
  192. {
  193. cout << "Wybierz dlugosc spodnicy: (1) 50 cm, (2) 60 cm , (3) 70 cm" << endl;
  194. cin >> k;
  195. if(k>0 && k<4)
  196. break;
  197. }
  198. string wyb;
  199. if (k==1) wyb = "50 cm";
  200. else if (k==2) wyb = "100 cm";
  201. else if (k==3) wyb = "150 cm";
  202. this->m_zam->setwymiar(wyb);
  203. }
  204. virtual void BuildMaterial(){
  205. int k;
  206. while(true)
  207. {
  208. cout << "Wybierz material spodnicy: (1) Welna, (2) Bawelna , (3) Skora Krokodyla" << endl;
  209. cin >> k;
  210. if(k>0 && k<4)
  211. break;
  212. }
  213. string wyb;
  214. if (k==1) wyb = "Welna";
  215. else if (k==2) wyb = "Bawelna";
  216. else if (k==3) wyb = "Skora Krokodyla";
  217. this->m_zam->setmaterial(wyb);
  218. }
  219. virtual void BuildKolor(){
  220. int k;
  221. while(true)
  222. {
  223. cout << "Wybierz kolor spodnicy: (1) Blue, (2) Black , (3) Pink" << endl;
  224. cin >> k;
  225. if(k>0 && k<4)
  226. break;
  227. }
  228. string wyb;
  229. if (k==1) wyb = "Blue";
  230. else if (k==2) wyb = "Black";
  231. else if (k==3) wyb = "Pink";
  232. this->m_zam->setkolor(wyb);
  233. }
  234. virtual void BuildTyp(){
  235. this->m_zam->settyp("Spodnica");
  236. }
  237. virtual Zamowienie* GetProduct(){
  238. return this->m_zam;
  239. }
  240. private:
  241. Zamowienie* m_zam;
  242.  
  243. };
  244.  
  245. class Director
  246. {
  247. public:
  248. Director(Builder* pBuilder){
  249. this->m_pBuilder = pBuilder;
  250. }
  251. ~Director(){
  252. delete this->m_pBuilder;
  253. this->m_pBuilder = NULL;
  254. }
  255. void Construct(){
  256. this->m_pBuilder->BuildWymiar();
  257. this->m_pBuilder->BuildMaterial();
  258. this->m_pBuilder->BuildKolor();
  259. this->m_pBuilder->BuildTyp();
  260. }
  261. Builder* Build(){
  262. return m_pBuilder;
  263. }
  264. private:
  265. Builder* m_pBuilder;
  266. };
  267.  
  268.  
  269. int main(){
  270.  
  271. list <Zamowienie*> lst;
  272. list <Zamowienie*>::iterator t;
  273.  
  274. Director *d;
  275. int a;
  276.  
  277. for(int i = 0; i<5; i++)
  278. {
  279. cout << "Wybierz produkt: (1) Marynarka, (2) Spodnie , (3) Spodnica" << endl;
  280. cin >> a;
  281. switch(a)
  282. {
  283. case 1:
  284. d = new Director(new BMarynarka());
  285. break;
  286. case 2:
  287. d= new Director(new BSpodnie());
  288. break;
  289. case 3:
  290. d = new Director(new BSpodnica());
  291. break;
  292. default:
  293.  
  294. break;
  295. }
  296. d->Construct();
  297. lst.push_back(d->Build()->GetProduct());
  298. delete d;
  299. }
  300.  
  301. for(t=lst.begin(); t!= lst.end(); ++t){
  302. (*t)->show();
  303. }
  304.  
  305. for(list <Zamowienie*>::iterator temp, t=lst.begin(); t!=lst.end();){
  306. temp = t;
  307. t++;
  308. delete *temp;
  309. }
  310. lst.clear();
  311.  
  312. return 0;
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement