Saturday 16 June 2018

Creation Design Patterns

  • Abstract Factory
    Creates an instance of several families of classes
  • Factory Method
    Creates an instance of several derived classes
  • Object Pool
    Avoid expensive acquisition and release of resources by recycling objects that are no longer in use
  • Prototype
    A fully initialized instance to be copied or cloned
  • Singleton
    A class of which only a single instance can exist
----------------------------
Abstract Factory:

// Factory method to create objects of different types.
// Change is required only in this function to create a new object type
Vehicle* Vehicle::create(vehicle_type type) {
    if (type == Road_vehicle_1)
        return new TwoWheeler();
    else if (type == Road_vehicle_2)
        return new ThreeWheeler();
    else if (type == Road_Vehicle_3)
        return new FourWheeler();
    else if(type == Water_vehicle_1)
         return new smallBoat();
    else if(type == water_vehicel_2)
           return new mediumBoat();
    else
            return NULL;
}
// Driver program
int main() {
    Vehicle *pVehicle = Vehicle::abstractCreate(Road_vehicle_1);
        pVehicle->printVehicle();
    Vehicle *pVehicle = Vehicle::abstractCreate(water_vehicle_1);
      pVehicle->printVehicle();
   
}
---------------------------------------
Object Pool
------
class Resource
{
    int value;
    public:
        int getValue()
        {
            return value;
        }
        void setValue(int number)
        {
            value = number;
        }
};

class ObjectPool
{
    private:
        std::list<Resource*> resources;//List the resourse

        static ObjectPool* instance;
        ObjectPool() {}
    public:
        static ObjectPool* getInstance()
        {
            if (instance == 0)
            {
                instance = new ObjectPool;
            }
            return instance;
        }
        Resource* getResource()
        {
            if (resources.empty())
            {
                std::cout << "Creating new." << std::endl;
                return new Resource;
            }
            else
            {
                std::cout << "Reusing existing." << std::endl;
                Resource* resource = resources.front();
                resources.pop_front();
                return resource;
            }
        }
       void returnResource(Resource* object)
        {
             resources.push_back(object);
        }
};
ObjectPool* ObjectPool::instance = 0;
int main()
{
    ObjectPool* pool = ObjectPool::getInstance();
    Resource* one;
    Resource* two;
    /* Resources will be created. */
    one = pool->getResource();
    one->setValue(10);
    std::cout << "one = " << one->getValue() << " [" << one << "]" << std::endl;
    two = pool->getResource();
    two->setValue(20);
    std::cout << "two = " << two->getValue() << " [" << two << "]" << std::endl;
    pool->returnResource(one);
    pool->returnResource(two);
    return 0;
}

Prototype Design Pattern
The prototype pattern is a creational design pattern. Prototype patterns is required, when object creation is time consuming, and costly operation, so we create object with existing object itself. One of the best available way to create object from existing objects are clone() method.
     


// Prototype
class Document 
{
public:
   virtual Document* clone() const = 0;
   virtual ~Document() { }
};

// Concrete prototypes : xmlDoc, plainDoc, spreadsheetDoc
class xmlDoc : public Document 
{
public:
   Document*   clone() const { return new xmlDoc; }
};

class plainDoc : public Document 
{
public:
   Document* clone() const { return new plainDoc; }
};
// makeDocument() calls Concrete Portotype's clone() method 
// inherited from Prototype
class DocumentManager {
public:
   static Document* makeDocument( int choice );
   ~DocumentManager(){}
private:
   static Document* mDocTypes[N];
};

Document* DocumentManager::mDocTypes[] = 
{
   0, new xmlDoc, new plainDoc
};

Document* DocumentManager::makeDocument( int choice ) 
{
   return mDocTypes[choice]->clone();
}

// for_each op ()
struct Destruct
{
    void operator()(Document *a) const { 
 delete a; 
    }
};

// Client
int main() {
   vector docs(N);
   int choice;
   cout << "quit(0), xml(1), plain(2) \n";
   while (true) {
   cout << "Type in your choice (0-2)\n";
   cin >> choice;
      if (choice <= 0 || choice >= N)
         break;
      docs[choice] = DocumentManager::makeDocument( choice );
   }

  Destruct d;
    // this calls Destruct::operator()
    for_each(docs.begin(), docs.end(), d);

   return 0;
}
 



No comments:

Post a Comment