Razzim

wzorcebuilder Moje

Nov 7th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.63 KB | None | 0 0
  1. // ConsoleApplication2.cpp: Określa punkt wejścia dla aplikacji konsoli.
  2. //
  3.  
  4.  
  5. #include <iostream>
  6. #include <list>
  7. #include <typeinfo>
  8. #include <string>
  9.  
  10. #define _CRTDBG_MAP_ALLOC
  11. #include <stdlib.h>
  12. #include <crtdbg.h>
  13.  
  14. // krawiec - marynarke spodnie spodniczke
  15. // produkt - zamowienie
  16. // zbieraniem zamowien zajmuje sie konkretny budowniczy
  17. // inny budowniczy dla marynarki, spodni i spodniczki
  18. // kazde zamowienie do listy produktow
  19. // krawiec zbiera: dlugosc spodni, obwod w pasie - po 2 wymiary
  20. // marynarka - kolniez i dlugosc rekawow
  21. // l - dlgosc w calach, w - szerokosc dla spodni
  22. // spodniczka - dlugosc, obwod
  23. // to wszystko w stringu - dlugosc:30, szerokosc:60
  24. // drugi parametr to z jakiego materialu - welniane, jedwab - zestaw materialow do wyboru dla krawca - jedne materialy nie nadaja sie dla marynarki
  25. // trzeci parametr: kolor
  26. // co ma byc? marynarka - tworzymy budowniczego od marynarki - wynik to zamowienie ktore zamiescimy na liste
  27. // na koniec lista z metoda do wypisania jej zawartosci na ekranie
  28. // tak zrobic zeby jak dodamy koszule nie bedzie trzeba nic modyfikowac
  29. // ustawic interfejs budowniczego - jakie metody
  30. // jakie czesci wspolne - rozmiary, material, kolor
  31.  
  32.  
  33.  
  34. // w case trworzysz budowniczego
  35. // nastepnie tworzymy dyrektora
  36. // na dyrektorze construkt
  37. // getproduct
  38.  
  39. // metoda statyccznna - mozna wywolac metode na samej klasie, bez obiektu
  40.  
  41. /* w konstruktorze otworzyc plik w destruktorze zamknac do obserwatora*/
  42.  
  43. using namespace std;
  44.  
  45. class Product
  46. {
  47. private:
  48. string m_partName;
  49. string m_partA;
  50. string m_partB;
  51. string m_partC;
  52. public:
  53. void setPartName(const string& s); // metoda czysto wirtualna
  54. void setPartA(const string& s);
  55. void setPartB(const string& s);
  56. void setPartC(const string& s);
  57. Product();
  58.  
  59.  
  60. string Nazwa()
  61. {
  62. return m_partName;
  63. }
  64.  
  65. string Rozmiar()
  66. {
  67. return m_partA;
  68. }
  69. string Material()
  70. {
  71. return m_partB;
  72. }
  73. string Kolor()
  74. {
  75. return m_partC;
  76. }
  77.  
  78. void Wypisz()
  79. {
  80. cout << "Nr. zamowienia: " << ": " << m_partName << " - Rozmiar: " << m_partA << " Material: " << m_partB << " Kolor: " << m_partC << endl;
  81. }
  82.  
  83. ~Product();
  84. };
  85.  
  86. class Builder
  87. {
  88. public:
  89. virtual void BuildPartName() = 0;
  90. virtual void BuildPartA() = 0;
  91. virtual void BuildPartB() = 0;
  92. virtual void BuildPartC() = 0;
  93. virtual Product* GetProduct() = 0;
  94. Builder();
  95. virtual ~Builder();
  96. };
  97.  
  98. class ConcreteBuilder1 :public Builder
  99. {
  100. public:
  101. ConcreteBuilder1();
  102. ~ConcreteBuilder1();
  103. virtual void BuildPartName();
  104. virtual void BuildPartA();
  105. virtual void BuildPartB();
  106. virtual void BuildPartC();
  107. virtual Product* GetProduct();
  108. private:
  109. Product* m_pProduct;
  110. };
  111.  
  112. class ConcreteBuilder2 :public Builder
  113. {
  114. public:
  115. ConcreteBuilder2();
  116. ~ConcreteBuilder2();
  117. virtual void BuildPartName();
  118. virtual void BuildPartA();
  119. virtual void BuildPartB();
  120. virtual void BuildPartC();
  121. virtual Product* GetProduct();
  122. private:
  123. Product* m_pProduct;
  124. };
  125.  
  126. class ConcreteBuilder3 :public Builder
  127. {
  128. public:
  129. ConcreteBuilder3();
  130. ~ConcreteBuilder3();
  131. virtual void BuildPartName();
  132. virtual void BuildPartA();
  133. virtual void BuildPartB();
  134. virtual void BuildPartC();
  135. virtual Product* GetProduct();
  136. private:
  137. Product* m_pProduct;
  138. };
  139.  
  140. class Director
  141. {
  142. public:
  143. Director(Builder* pBuilder);
  144. ~Director();
  145. void Construct();
  146. Builder* Build()
  147. {
  148. return m_pBuilder;
  149. }
  150. private:
  151. Builder* m_pBuilder;
  152. };
  153.  
  154. Product::~Product()
  155. {
  156. }
  157.  
  158. Product::Product()
  159. {
  160. }
  161.  
  162. void Product::setPartName(const string& s)
  163. {
  164. this->m_partName = s;
  165. }
  166.  
  167. void Product::setPartA(const string& s)
  168. {
  169. this->m_partA = s;
  170. }
  171.  
  172. void Product::setPartB(const string& s)
  173. {
  174. this->m_partB = s;
  175. }
  176.  
  177. void Product::setPartC(const string& s)
  178. {
  179. this->m_partC = s;
  180. }
  181.  
  182.  
  183. Builder::Builder()
  184. {
  185. }
  186.  
  187. Builder::~Builder()
  188. {
  189. }
  190.  
  191. ConcreteBuilder1::ConcreteBuilder1()
  192. {
  193. this->m_pProduct = new Product();
  194. cout << endl << "Tworzenie marynarki" << endl;
  195. }
  196.  
  197. void ConcreteBuilder1::BuildPartName()
  198. {
  199. this->m_pProduct->setPartName("Marynarka");
  200. }
  201.  
  202. void ConcreteBuilder1::BuildPartA()
  203. {
  204. string temp = "";
  205. cout << endl << "Rozmiar kolnierzyka: ";
  206. cin >> temp;
  207. this->m_pProduct->setPartA(temp);
  208. cout << "Wprowadzony rozmiar kolnierzyka: " << temp << endl;
  209. }
  210.  
  211. void ConcreteBuilder1::BuildPartB()
  212. {
  213. int temp = 0;
  214. bool menu = true;
  215.  
  216.  
  217. while (menu)
  218. {
  219. cout << endl << "1 - Bawelna" << endl << "2 - Skora" << endl << "3 - Powidla" << endl << "Wybierz material: ";
  220. cin >> temp;
  221. menu = false;
  222. switch (temp)
  223. {
  224. case 1:
  225. {
  226. cout << endl << "Wybrales material: ";
  227. this->m_pProduct->setPartB("Bawelna");
  228. cout << "Bawelna" << endl;
  229. break;
  230. }
  231. case 2:
  232. {
  233. cout << endl << "Wybrales material: ";
  234. this->m_pProduct->setPartB("Skora");
  235. cout << "Skora" << endl;
  236. break;
  237. }
  238. case 3:
  239. {
  240. cout << endl << "Wybrales material: ";
  241. this->m_pProduct->setPartB("Powidla");
  242. cout << "Powidla" << endl;
  243. break;
  244. }
  245. default:
  246. {
  247. cout << "Nie ma takiej opcji! Podaj material ponownie.";
  248. menu = true;
  249. break;
  250. }
  251. }
  252. }
  253. }
  254.  
  255. void ConcreteBuilder1::BuildPartC()
  256. {
  257. string temp = "";
  258. cout << endl << "podaj kolor: ";
  259. cin >> temp;
  260. this->m_pProduct->setPartC(temp);
  261. cout << "Wprowadzono kolor: " << temp << endl;
  262. }
  263.  
  264. Product* ConcreteBuilder1::GetProduct()
  265. {
  266. return this->m_pProduct;
  267. }
  268.  
  269. ConcreteBuilder1::~ConcreteBuilder1()
  270. {
  271. delete this->m_pProduct;
  272. this->m_pProduct = NULL;
  273. }
  274.  
  275. ConcreteBuilder2::ConcreteBuilder2()
  276. {
  277. this->m_pProduct = new Product();
  278. cout << endl << "Tworzenie spodni" << endl;
  279. }
  280.  
  281. void ConcreteBuilder2::BuildPartName()
  282. {
  283. this->m_pProduct->setPartName("Spodnie");
  284. }
  285.  
  286. void ConcreteBuilder2::BuildPartA()
  287. {
  288. string temp = "";
  289. cout << endl << "podaj rozmiar: ";
  290. cin >> temp;
  291. this->m_pProduct->setPartA(temp);
  292. cout << "Wprowadzono rozmiar: " << temp << endl;
  293. }
  294.  
  295. void ConcreteBuilder2::BuildPartB()
  296. {
  297. int temp = 0;
  298. bool menu = true;
  299.  
  300. while (menu)
  301. {
  302. cout << endl << "1 - bawelna" << endl << "2 - skora" << endl << "3 - powidla" << endl << "Wybierz material: ";
  303. cin >> temp;
  304. menu = false;
  305. switch (temp)
  306. {
  307. case 1:
  308. {
  309. cout << endl << "Wybrales material: ";
  310. this->m_pProduct->setPartB("Bawelna");
  311. cout << "bawelna" << endl;
  312. break;
  313. }
  314. case 2:
  315. {
  316. cout << endl << "Wybrales material: ";
  317. this->m_pProduct->setPartB("Skora");
  318. cout << "skora" << endl;
  319. break;
  320. }
  321. case 3:
  322. {
  323. cout << endl << "Wybrales material: ";
  324. this->m_pProduct->setPartB("Powidla");
  325. cout << "powidla" << endl;
  326. break;
  327. }
  328. default:
  329. {
  330. cout << "Nie ma takiej opcji!";
  331. menu = true;
  332. break;
  333. }
  334. }
  335. }
  336. }
  337.  
  338. void ConcreteBuilder2::BuildPartC()
  339. {
  340. string temp = "";
  341. cout << endl << "podaj kolor: ";
  342. cin >> temp;
  343. this->m_pProduct->setPartC(temp);
  344. cout << "Wprowadzono kolor: " << temp << endl;
  345. }
  346.  
  347. Product* ConcreteBuilder2::GetProduct()
  348. {
  349. return this->m_pProduct;
  350. }
  351.  
  352. ConcreteBuilder2::~ConcreteBuilder2()
  353. {
  354. delete this->m_pProduct;
  355. this->m_pProduct = NULL;
  356. }
  357.  
  358. ConcreteBuilder3::ConcreteBuilder3()
  359. {
  360. this->m_pProduct = new Product();
  361. cout << endl << "Tworzenie spodniczki" << endl;
  362. }
  363.  
  364. void ConcreteBuilder3::BuildPartName()
  365. {
  366. this->m_pProduct->setPartName("Spodniczka");
  367. }
  368.  
  369. void ConcreteBuilder3::BuildPartA()
  370. {
  371. string temp = "";
  372. cout << endl << "podaj rozmiar: ";
  373. cin >> temp;
  374. this->m_pProduct->setPartA(temp);
  375. cout << "Wprowadzono rozmiar: " << temp << endl;
  376. }
  377.  
  378. void ConcreteBuilder3::BuildPartB()
  379. {
  380. int temp = 0;
  381. bool menu = true;
  382.  
  383.  
  384. while (menu)
  385. {
  386. cout << endl << "1 - bawelna" << endl << "2 - skora" << endl << "3 - powidla" << endl << "Wybierz material: ";
  387. cin >> temp;
  388. menu = false;
  389. switch (temp)
  390. {
  391. case 1:
  392. {
  393. cout << endl << "Wybrales material: ";
  394. this->m_pProduct->setPartB("Bawelna");
  395. cout << "bawelna" << endl;
  396. break;
  397. }
  398. case 2:
  399. {
  400. cout << endl << "Wybrales material: ";
  401. this->m_pProduct->setPartB("Skora");
  402. cout << "skora" << endl;
  403. break;
  404. }
  405. case 3:
  406. {
  407. cout << endl << "Wybrales material: ";
  408. this->m_pProduct->setPartB("Powidla");
  409. cout << "powidla" << endl;
  410. break;
  411. }
  412. default:
  413. {
  414. cout << "Nie ma takiej opcji!";
  415. menu = true;
  416. break;
  417. }
  418. }
  419. }
  420. }
  421.  
  422. void ConcreteBuilder3::BuildPartC()
  423. {
  424. string temp = "";
  425. cout << endl << "podaj kolor: ";
  426. cin >> temp;
  427. this->m_pProduct->setPartC(temp);
  428. cout << "Wprowadzono kolor: " << temp << endl;
  429. }
  430.  
  431. Product* ConcreteBuilder3::GetProduct()
  432. {
  433. return this->m_pProduct;
  434. }
  435.  
  436. ConcreteBuilder3::~ConcreteBuilder3()
  437. {
  438. delete this->m_pProduct;
  439. this->m_pProduct = NULL;
  440. }
  441.  
  442. Director::Director(Builder* pBuilder)
  443. {
  444. this->m_pBuilder = pBuilder;
  445. }
  446.  
  447. void Director::Construct()
  448. {
  449. this->m_pBuilder->BuildPartName();
  450. this->m_pBuilder->BuildPartA();
  451. this->m_pBuilder->BuildPartB();
  452. this->m_pBuilder->BuildPartC();
  453. }
  454.  
  455. Director::~Director()
  456. {
  457. delete this->m_pBuilder;
  458. this->m_pBuilder = NULL;
  459. }
  460.  
  461. int main()
  462. {
  463. int produkt = 0;
  464. bool menu = true;
  465.  
  466. /*_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
  467. _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
  468. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
  469. _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
  470. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
  471. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
  472. _CrtDumpMemoryLeaks();
  473. system("pause");*/
  474.  
  475. list <Product*> lista;
  476. //list <int> list1;
  477.  
  478. /*_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
  479. _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
  480. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
  481. _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
  482. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
  483. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
  484. _CrtDumpMemoryLeaks();
  485. system("pause");*/
  486.  
  487. list <Product*>::iterator t;
  488. int indeks = 1;
  489.  
  490.  
  491.  
  492.  
  493. /*Director* pDirector1 = new Director(new ConcreteBuilder1());
  494. pDirector1->Construct();
  495.  
  496. Director* pDirector2 = new Director(new ConcreteBuilder2());
  497. pDirector2->Construct();
  498.  
  499. Director* pDirector3 = new Director(new ConcreteBuilder3());
  500. pDirector3->Construct();*/
  501.  
  502. //delete pDirector1;
  503. //delete pDirector2;
  504. //delete pDirector3;
  505.  
  506. Director *d;
  507.  
  508.  
  509.  
  510. while (menu)
  511. {
  512. cout << endl << "Wybierz produkt:" << endl << "1: Marynarka" << endl << "2: Spodnie" << endl << "3: Spodniczka" << endl << "4: Wypisz liste produktow" << endl<< "5: Zamknij program"<< endl;
  513. cout << "1-5: ";
  514. cin >> produkt;
  515.  
  516. /* try
  517. {
  518. //if (cin.fail()))
  519. cin >> produkt;
  520. }
  521.  
  522. catch (cin.fail())
  523. {
  524. cerr << blad.what();
  525. }*/
  526.  
  527. /* while(true)
  528. {
  529. cin >> produkt;
  530. try{
  531. if(cin.fail()){
  532. throw "error";
  533. }
  534. if(produkt>0){
  535. cout<<"number greater than 0"<<endl;
  536. }
  537. }
  538. catch( char* error){
  539. cout<<error<<endl;
  540. break;
  541. }
  542. }*/
  543.  
  544.  
  545. switch (produkt)
  546. {
  547. case 1:
  548. {
  549. //cout << endl << "Wybrales marynarke";
  550.  
  551. //Director* pDirector1 = new Director(new ConcreteBuilder1());
  552. //pDirector1->Construct();
  553. //ConcreteBuilder1* b = new ConcreteBuilder1();
  554. //cout << "Tworzenie marynarki" << endl;
  555. d = new Director(new ConcreteBuilder1());
  556.  
  557. break;
  558. }
  559. case 2:
  560. {
  561. //ConcreteBuilder2* b = new ConcreteBuilder2();
  562. d = new Director(new ConcreteBuilder2());
  563.  
  564. break;
  565. }
  566. case 3:
  567. {
  568. //ConcreteBuilder3* b = new ConcreteBuilder3();
  569. d = new Director(new ConcreteBuilder3());
  570.  
  571. break;
  572. }
  573.  
  574. case 4:
  575. {
  576. //lista.push_back(b->GetProduct());
  577.  
  578.  
  579.  
  580. // wypisz liste
  581. cout << endl << endl << "Lista produktow: " << endl;
  582. for (t = lista.begin(); t != lista.end(); ++t)
  583. {
  584. (*t)->Wypisz();//cout << "Nr. zamowienia: " << indeks << ": " << (*t)->Nazwa() << " - Rozmiar: " << (*t)->Rozmiar() << " Material: " << (*t)->Material() << " Kolor: " << (*t)->Kolor() << endl;
  585. indeks++;
  586. }
  587. break;
  588. }
  589. case 5:
  590. {
  591. menu = false;
  592. break;
  593.  
  594. }
  595. default:
  596. {
  597.  
  598. cout << "Nie ma takiej opcji!" << endl;
  599. //break;
  600. }
  601.  
  602.  
  603. }
  604. d->Construct();
  605. lista.push_back(d->Build()->GetProduct());
  606. delete d;
  607.  
  608.  
  609. }
  610.  
  611. /*for (int i = 0; i < lista.max_size(); i++)
  612. {
  613. lista.clear;
  614. }*/
  615.  
  616. list <Product*>::iterator temp;
  617.  
  618. for (t = lista.begin(); t != lista.end();)
  619. {
  620. temp = t;
  621. t++;
  622. delete *temp;
  623. }
  624.  
  625.  
  626. //lista.clear();
  627.  
  628. //delete d;
  629.  
  630. /*_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
  631. _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
  632. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
  633. _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
  634. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
  635. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
  636. _CrtDumpMemoryLeaks();
  637. system("pause");*/
  638.  
  639. return 0;
  640. }
Add Comment
Please, Sign In to add comment