Advertisement
Guest User

Untitled

a guest
May 8th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3.  
  4. class IPacket //pseudo interface
  5. {
  6.     public:
  7.         virtual ~IPacket() {}
  8.         virtual std::string getDesc(void) = 0; //metoda jest abstrakcyjna
  9. };
  10.  
  11. class IPacketFactory //pseudo interface
  12. {
  13.     public:
  14.         virtual ~IPacketFactory() {}
  15.         virtual IPacket* createPacket() = 0;//metoda jest abstrakcyjna
  16. };
  17.  
  18. class walkPacket : public IPacket
  19. {
  20.     public:
  21.         virtual std::string getDesc(void) { return std::string("I am walkPacket!\n"); }
  22. };
  23.  
  24. class movePacket : public IPacket
  25. {
  26.     public:
  27.         virtual std::string getDesc(void) { return std::string("I am movePacket!\n"); }
  28. };
  29.  
  30. class incomingPacketFactory : public IPacketFactory
  31. {
  32.     public:
  33.         virtual IPacket* createPacket()
  34.         {
  35.             IPacket* mo = new walkPacket;
  36.             return mo;
  37.         }
  38. };
  39.  
  40. class outgoingPacketFactory : public IPacketFactory
  41. {
  42.     public:
  43.         virtual IPacket* createPacket()
  44.         {
  45.             IPacket* mo = new movePacket;
  46.             return mo;
  47.         }
  48. };
  49.  
  50. int main()
  51. {
  52.    
  53.     IPacketFactory *incomingPacket = new incomingPacketFactory;
  54.     IPacketFactory *outgoingPacket = new outgoingPacketFactory;
  55.    
  56.     IPacket *walkPacket = incomingPacket->createPacket();
  57.     IPacket *movePacket = outgoingPacket->createPacket();
  58.    
  59.     std::cout << walkPacket->getDesc();
  60.     std::cout << movePacket->getDesc();
  61.    
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement