Posts

Showing posts from February, 2018

Virtual constructor & virtual copy constructor

virtual constructor :   virtual constructor is function that creates different type of object of objects depending on the input it is given. Example: Factory design pattern is the example of virtual constructor. virtual copy constructor : virtual copy constructor returns a pointer to a new copy of the the object invoking the function. Because of this behaviour, virtual copy constructor is also known as 'copyself', 'cloneself' or just 'clone'. class base {     public:         virtual base* clone() = 0; }; class Text : public base {     public:         virtual Text* clone()         {             return new Text(*this);         } }; class Image : public base {     public:         virtual Image* clone()     ...

Adapter Design Pattern

Image
This is one of structural design pattern. The adapter pattern is a design pattern that is used to allow two incompatible types to communicate. Example: In one user database system all information regarding user info like name, phone number, emailid, address etc are stored. But to call the user all information is not required, only user name & phone number is sufficient. In this case we can create a new class adapter. This class will provide only those information which is required and hide unrequired information. class User {     string name;     string phonenumber;     string emailId;     string address;     public:         User(string na, string ph, string email, string addr)         {             name = na;             phonenumber = ph; ...

SOLID Principles

Image
Solid principle are 5 dependency management for object oriented and design. while design a system we should follow SOLID principle, so that the code will be more flexible, robust & easy to enhance for future. The solid principle is combination of below 5 principles 1. Single Responsibility Principle 2. Open / Closed Principle 3. Liskov Substitution Principle (LSP) 4. Interface Segregation Principle (ISP) 5. Dependency Inversion Principle (DIP) 1. Single Responsibility Principles :   Design the classes so that each class should have single purpose. This does not mean that each class should have only one method but that all of the members in the class are related to the class's primary functions. If a class has multiple responsibility that should be separated into new classes. Example: in one project if we need to read data from any service provider parse that data & take any action on that data. class RequestAlert {     pu...

Decorator Design Pattern

Image
This is one of structural design pattern. The decorator pattern is a design pattern that extends the functionality of individual objects by wrapping them with one or more decorator classes. These decorators can modify existing members and add new methods and properties at run-time. #include<iostream> using namespace std; class Product {     public:         virtual int getPrice()=0; }; class Item : public Product {     int price;     string name;     public:         Item(string str, int value)         {             name = str;             price = value;         }         int getPrice()         {           ...