Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #pragma once
  2. #include "My_types.h"
  3.  
  4. class Factory {
  5. public:
  6.  
  7. static Factory & getInstance(){return factory_;};
  8. unsigned int registerFigure(CreateFigure fun); //dodaje element do kolekcji,
  9. void read_data (std::ifstream &input,My_vector &v );
  10. Figure * Create(unsigned int id);
  11. private:
  12. static unsigned int ID_;
  13. static Factory factory_;
  14. //mapa przechowujaca pary id - klasy, wskaznik na funcje tworzaca obiekt
  15. //jej definicja w pliku naglowkowym My_types.h
  16. My_map fig_creators_;
  17. Factory(){};
  18.  
  19. };
  20.  
  21.  
  22. #pragma once
  23. #include "Figure.h"
  24. #include <vector>
  25. #include <map>
  26. typedef Figure * (*CreateFigure)(); //wskaznik na funkcje tworzaca obiekt
  27. typedef std::vector <Figure *> My_vector;
  28. typedef std::map<unsigned int, CreateFigure> My_map; //mapowanie pomiedzy identfikatorem a typem
  29.  
  30. /*
  31. * Rectangle.h
  32. *
  33. */
  34.  
  35. #pragma once
  36. #include "Figure.h"
  37.  
  38. class Rectangle: public Figure {
  39. public:
  40. Rectangle();
  41. Rectangle(float a, float b):A_(a),B_(b){};
  42. virtual ~Rectangle();
  43. void read(std::ifstream & input);
  44. void save (std::ofstream & output);
  45. void info();
  46. static Figure * CreateRectangle(){return new Rectangle();};
  47. static unsigned int id_;
  48. private:
  49. float A_,B_;
  50.  
  51. };
  52.  
  53. /*
  54. * Factory.cpp
  55. *
  56. */
  57.  
  58. #include "Factory.h"
  59.  
  60.  
  61. unsigned int Factory::registerFigure(CreateFigure fun)
  62. {
  63. //metoda automatycznie zwieksza licznikk ID_ i
  64. //zapisuje pare
  65. fig_creators_.insert(std::pair<unsigned int,CreateFigure>(++ID_,fun));
  66. //wynik zwracany przechowywany przez kazda klase w polu statycznym id_
  67. return ID_;
  68. }
  69.  
  70. Figure * Factory::Create(unsigned int id)
  71. {
  72. //Metoda na podstawie id tworzy nowy obiekt
  73. My_map::iterator it=fig_creators_.find(id);
  74. if (it!=fig_creators_.end())
  75. {
  76. return (it->second)();
  77. }
  78. return 0L;
  79. }
  80.  
  81. void Factory::read_data (std::ifstream &input, My_vector & v)
  82. {
  83. //zmienne lokalne
  84. unsigned int type;
  85. Figure * tempFigure;
  86. while(!input.eof())
  87. {
  88. type=0;
  89. //odczytujemy id_ klasy
  90. input>>type;
  91. //tu tworzymy nowy obiekt
  92. tempFigure=Create(type);
  93. if (tempFigure!=0L)
  94. {
  95. //odczytanie danych juz do konkretnego obiektu
  96. //kozystamy tu z potegi funkcji wiryalnych
  97. tempFigure->read(input);
  98. //wczytany obiekt (tak naprawde wskaznik do niego) umieszczamy w wektorze
  99. v.push_back(tempFigure);
  100. }
  101.  
  102. }
  103.  
  104. }
  105.  
  106. unsigned int Factory::ID_=1;
  107. Factory Factory::factory_;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement