Posts

Abstract factory pattern

Image
 It is one of creational design pattern. The abstract design pattern provide an interface for creating family of related or dependent objects without specifying their concrete classes. The Factory pattern construct single object where as abstract factory pattern construct multiple objects. In the above UML diagram, There is one abstract factory classs which having 2 virtual funtion createButton and createCheck. In the derived classes LinuxFactory and WindowsFactory class which create corresponding button and check object. If we create a application which have more than one theme dark theme or light theme, there we can use abstract factory pattern. Code: #include<iostream> using namespace std; class Button { public: virtual void Draw() = 0; }; class Check { public: virtual void Draw() = 0; }; class LinuxButton : public Button { public: void Draw() { cout<<"linux button"<<endl; } }; class LinuxCheck: public Check { public: void Draw(...

STL Questions

1. Sorting a vector of custom objects     a)Using operator()         struct MyStruct {             int key;             std::string str;             MyStruct(int k, int s) : key(k), str(s) {}         };         struct less_than{             inline bool operator()(const MyStruct &s1, const MyStruct &b)             {                 return s1.key<s2.key;             }         };         std::vector<MyStruct> vec;         vec.push_back(MyStruct(4, "a")); ...

Producer Consumer problem using mutex

  #include<stdio.h> #include<pthread.h> #include<stdlib.h>   // Max buffer size user for producing #define buff_sz 10   // buffer index would be the current index volatile int buff_idx = -1; // buffer used to produce item volatile char *buff = NULL;;   // Initialization and declaration of muetxt and condition variables pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t buff_not_full = PTHREAD_COND_INITIALIZER; pthread_cond_t buff_not_empty = PTHREAD_COND_INITIALIZER;   // Consumer thread void *Consumer() {          while (1)          {                  // Taking lock to avoid race condition                  pthread_mutex_ lock(&m...