160 likes | 584 Views
Design Patterns. C++ Java C#. Design Patterns. In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code.
E N D
Design Patterns C++ Java C#
Design Patterns • In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. • A design pattern is not a finished design that can be transformed directly into code. • It is a description or template for how to solve a problem that can be used in many different situations. • Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.
Singleton Design Pattern In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects (say, five).
Singleton in C++ class Singleton { private: static Singleton oneandonly; Singleton() {} ~Singleton() {} Singleton(const Singleton &); // intentionally undefined Singleton & operator= (const Singleton &); // intentionally public: static Singleton &getInstance(){ return oneandonly; }; }
Singleton in Java public class Singleton { private static final Singleton oneandonly = new Singleton(); private Singleton() {} // Outside cannot create a Singleton public static Singleton getInstance() { return oneandonly; } }
Singleton in C# public sealed class Singleton { private static readonly Singleton oneandonly = new Singleton(); private Singleton() { } public static Singleton Instance{ get{ return oneandonly;} } }
Factory pattern • The factory method pattern is an object-oriented design pattern to implement the concept of factories. • Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The factory method design pattern handles this problem by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.
Example) complex numbers • In this example, a complex number which has a real and imaginary part can be created with exactly those two values. It can also be created with polar coordinates (length and angle). • Here we seek to allow the creator of the object to come to our class (factory) and create the object any way they want. But what we don’t allow is the way we choose to internally create them and so we make the constructor private.
Factory code for complex numbers class Complex { public static Complex fromCartesian(double real, double imag) { return new Complex(real, imag); } public static Complex fromPolar(double modulus, double angle) { return new Complex(modulus * cos(angle), modulus * sin(angle)); } private Complex(double a, double b) {…however we want} private double real; private double imaginary; } Complex c = Complex.fromPolar(1, pi);
Adapter Pattern • In computer programming, the adapter design pattern (often referred to as the wrapper pattern or simply a wrapper) translates one interface for a class into a compatible interface. • An adapter allows classes to work together that normally could not because of incompatible interfaces, by providing its interface to clients while using the original interface. • The adapter translates calls to its interface into calls to the original interface, and the amount of code necessary to do this is typically small. The adapter is also responsible for transforming data into appropriate forms
Example : lines and rectangles (1 of 2) class Line { public void draw(int x1, int y1, int x2, int y2) { System.out.println("line("+x1+','+y1+") to ("+x2+',' + y2 + ')'); } } class Rectangle { public void draw(int x, int y, int w, int h) { System.out.println("rect at ("+x+','+y+")width " + w +" height "+ h); } }
Example : lines and rectangles (1 of 2) public class AdapterDemo { public static void main(String[] args) { Object[] shapes = { new Line(), new Rectangle() }; // A begin and end point from a graphical editor intx1 = 10, y1 = 20; intx2 = 30, y2 = 60; for (inti = 0; i < shapes.length; ++i) if (shapes[i].getClass().getName().equals("Line")) ((Line)shapes[i]).draw(x1, y1, x2, y2); else if (shapes[i].getClass().getName().equals("Rectangle")) ((Rectangle)shapes[i]).draw(Math.min(x1,x2),Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1)); } }