Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Patterns Solving problems with already known solutions Unit - 13.

Similar presentations


Presentation on theme: "Design Patterns Solving problems with already known solutions Unit - 13."— Presentation transcript:

1 Design Patterns Solving problems with already known solutions Unit - 13

2 Unit Introduction This unit covers design patterns using C++

3 Unit Objectives After covering this unit you will understand… What are design patterns What are design patterns Singleton design pattern Singleton design pattern Factory design pattern Factory design pattern Proxy design pattern Proxy design pattern Facade design pattern Facade design pattern

4 Design Patterns A design pattern is a special way of solving a particular class of problems A design pattern is a special way of solving a particular class of problems A design pattern has four essential elements A design pattern has four essential elements The Pattern Name is a handle, which is used to describe a design problem, its solution and consequences. The Pattern Name is a handle, which is used to describe a design problem, its solution and consequences. The Problem describes, when to apply the pattern The Problem describes, when to apply the pattern

5 Design Patterns (contd.) The Solution describes the elements the makeup the design, their responsibilities, and collaborations The Solution describes the elements the makeup the design, their responsibilities, and collaborations The Consequences are the results and trade off of applying the pattern The Consequences are the results and trade off of applying the pattern

6 Singleton This pattern is a way to provide, one and only one instance of an object This pattern is a way to provide, one and only one instance of an object Prevent from having any way to create an object except the provided way Prevent from having any way to create an object except the provided way Declare all constructors as private, and create at least one constructor to prevent the compiler from synthesizing a default constructor Declare all constructors as private, and create at least one constructor to prevent the compiler from synthesizing a default constructor

7 Example: Singleton #include #include class Singleton { private: private: static Singleton sm_s; static Singleton sm_s; int m_i; Singleton(int x) : m_i(x) { } Singleton(int x) : m_i(x) { } public: public: static Singleton& GetHandle() { return sm_s;} int GetValue() { return m_i; } int GetValue() { return m_i; } void SetValue(int x) { m_i = x; } void SetValue(int x) { m_i = x; }}; Singleton Singleton::sm_s(47); // initialize static member // variable // variable

8 Example: Singleton (contd.) void main() { Singleton& sObj = Singleton::GetHandle(); Singleton& sObj = Singleton::GetHandle(); cout << sObj.GetValue() << endl; // prints 47 cout << sObj.GetValue() << endl; // prints 47 Singleton& sObj2 = Singleton::GetHandle(); Singleton& sObj2 = Singleton::GetHandle(); sObj2.SetValue(9); sObj2.SetValue(9); cout << sObj.GetValue() << endl; // prints 9 cout << sObj.GetValue() << endl; // prints 9}

9 Factory Encapsulate object creation Encapsulate object creation Create objects through a common interface rather than to allow the creation code to be spread throughout program Create objects through a common interface rather than to allow the creation code to be spread throughout program All the code in a program must go through factory whenever it needs to create one of objects All the code in a program must go through factory whenever it needs to create one of objects Adding a new object modifies factory Adding a new object modifies factory

10 Example: Factory #include #include class Shape { public: public: virtual void Draw() = 0; virtual void Draw() = 0; virtual void Erase() = 0; virtual void Erase() = 0; virtual ~Shape() {} virtual ~Shape() {} class BadShapeCreation : public exception class BadShapeCreation : public exception{ string m_reason; string m_reason; public: public: BadShapeCreation(string type) BadShapeCreation(string type){

11 Example: Factory (contd.) m_reason = "Cannot create type "+ type; } ~BadShapeCreation() throw() {} ~BadShapeCreation() throw() {} const char *what() const throw() const char *what() const throw(){ return m_reason; return m_reason; } }; }; static Shape* Factory(string type) // exception handle static Shape* Factory(string type) // exception handle throw(BadShapeCreation); throw(BadShapeCreation); }; // declaration of Shape class ends here class Circle : public Shape { Circle() {} // Private constructor Circle() {} // Private constructor friend class Shape; friend class Shape;

12 Example: Factory (contd.) public: public: void Draw() { cout << "Circle::draw\n"; } void Draw() { cout << "Circle::draw\n"; } void Erase() { cout << "Circle::erase\n"; } void Erase() { cout << "Circle::erase\n"; } ~Circle() { cout << "Circle::~Circle\n"; } ~Circle() { cout << "Circle::~Circle\n"; }}; class Square : public Shape { Square() {} Square() {} friend class Shape; friend class Shape; public: public: void Draw() { cout << "Square::draw\n"; } void Draw() { cout << "Square::draw\n"; } void Erase() { cout << "Square::erase\n"; } void Erase() { cout << "Square::erase\n"; } ~Square() { cout << "Square::~Square\n"; } ~Square() { cout << "Square::~Square\n"; }}; Shape* Shape::Factory(string type) // exception handle throw(Shape::BadShapeCreation) throw(Shape::BadShapeCreation)

13 Example: Factory (contd.) { if(type == "Circle") return new Circle; if(type == "Circle") return new Circle; if(type == "Square") return new Square; if(type == "Square") return new Square; throw BadShapeCreation(type); throw BadShapeCreation(type);} char* p_Shlist[] = { "Circle", "Square", "Square", "Circle", "Circle", "Circle", "Square", "" }; "Circle", "Circle", "Circle", "Square", "" }; void main() { vector shapes; vector shapes; try try { for(char** cp = p_Shlist; **cp; cp++) for(char** cp = p_Shlist; **cp; cp++) shapes.push_back(Shape::Factory(**cp)); shapes.push_back(Shape::Factory(**cp)); } }

14 Example: Factory (contd.) catch(Shape::BadShapeCreation e) catch(Shape::BadShapeCreation e) { cout << e.what() << endl; cout << e.what() << endl; return 1; return 1; } } for(int i = 0; i < shapes.size(); i++) for(int i = 0; i < shapes.size(); i++) { shapes[i]->Draw(); shapes[i]->Draw(); shapes[i]->Erase(); shapes[i]->Erase(); } }}

15 Proxy Proxy pattern is used to control access to the actual implementation Proxy pattern is used to control access to the actual implementation Proxy is a surrogate class that hides the implementation class Proxy is a surrogate class that hides the implementation class A call made to proxy class is delegated to the implementation class A call made to proxy class is delegated to the implementation class

16 Example: Proxy #include #include class Graphic { public: public: virtual ~Graphic(); virtual ~Graphic(); virtual void Draw (const Point& at) = 0; virtual const Point& GetExtent() = 0; virtual void Load (istream& from) = 0; protected: protected:Graphic();}; class Image : public Graphic {

17 Example: Proxy (contd.) public: public: Image(const char* p_File);// Load Image from File Image(const char* p_File);// Load Image from File virtual ~Image(); virtual void Draw (const Point& at); virtual const Point& GetExtent(); virtual void Load (istream& from); private: private: // own private data }; class ImageProxy : public Graphic { public: public: ImageProxy(const char* p_File);// Load Image from File ImageProxy(const char* p_File);// Load Image from File virtual ~ImageProxy(); virtual void Draw (const Point& at);

18 Example: Proxy (contd.) virtual const Point& GetExtent(); virtual void Save (ostream& to); protected: protected: Image* GetImage(); private: private: // own private data Image* m_pImage; Point m_Extent; char* m_pFileName; }; ImageProxy::ImageProxy(const char* p_FileName) { m_pFileName = strdup(p_Filename); m_pFileName = strdup(p_Filename); m_Extent = Point::Zero;// Don’t know extent yet m_Extent = Point::Zero;// Don’t know extent yet m_pImage = new Image(m_pFileName); m_pImage = new Image(m_pFileName);}

19 Example: Proxy (contd.) const Point& ImageProxy::GetExtent() { if (m_Extent == Point::Zero) if (m_Extent == Point::Zero) { m_Extent = GetImage()->GetExtent(); m_Extent = GetImage()->GetExtent(); } return m_Extent; return m_Extent;} void ImageProxy::Draw(const Point& at) { GetImage()->Draw(at); GetImage()->Draw(at);}

20 Example: Proxy (contd.) void ImageProxy::Load (istream& from) { from >> m_Extent >> m_pFileName; from >> m_Extent >> m_pFileName;} class TextDocument { public: public:TextDocument(); void Insert(Graphic*); //… more functions }; void main() { TextDocument* p_Text = new TextDocument; TextDocument* p_Text = new TextDocument; p_Text->Inert(new ImageProxy(“AnImageFile.jpg”)); p_Text->Inert(new ImageProxy(“AnImageFile.jpg”));}

21 Facade Provide a unified interface to a set of interfaces in a subsystem Provide a unified interface to a set of interfaces in a subsystem Defines a higher level interface the makes the sub systems easier to use Defines a higher level interface the makes the sub systems easier to use Facade reduces the complexity Facade reduces the complexity Minimise communication and dependencies among sub systems Minimise communication and dependencies among sub systems Useful when layering sub systems Useful when layering sub systems

22 Example: Facade include include // following classes perform different functions required for // compiling a program class Scanner// Reads input {}; class Parser // Parse input {}; class ProgramNodeBuilder // Build Nodes {}; class CodeGenerator // Generates code {};

23 Example: Facade (contd.) class Compiler { public: public:Compiler(); virtual void Compile(istream&, BytecodeStream&); }; // Facade implementation void Compiler::Compile(istream& input, BytecodeStream outp) { Scanner scanner(input); Scanner scanner(input); ProgramNodeBuilder builder; ProgramNodeBuilder builder; Parser parser; Parser parser; parser.Parse(scanner, builder); parser.Parse(scanner, builder); RISCCodeGenerator generator(outp); RISCCodeGenerator generator(outp);}

24 Unit Summary In this unit you have covered … In this unit you have covered … What are design patterns What are design patterns Singleton, Factory, Proxy and Facade design patterns using C++ Singleton, Factory, Proxy and Facade design patterns using C++


Download ppt "Design Patterns Solving problems with already known solutions Unit - 13."

Similar presentations


Ads by Google