DESIGN PATTERNS C Java C Design Patterns In

  • Slides: 12
Download presentation
DESIGN PATTERNS C++ Java C#

DESIGN PATTERNS C++ Java C#

Design Patterns • In software engineering, a design pattern is a general reusable solution

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

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 in C++ class Singleton { private: static Singleton oneandonly; Singleton() {} ~Singleton() {} Singleton(const Singleton &); // intentionally undefined Singleton & operator= (const Singleton &); // intentionally public: static Singleton &get. Instance(){ return oneandonly; }; }

Singleton in Java public class Singleton { private static final Singleton oneandonly = new

Singleton in Java public class Singleton { private static final Singleton oneandonly = new Singleton(); private Singleton() {} // Outside cannot create a Singleton public static Singleton get. Instance() { return oneandonly; } }

Singleton in C# public sealed class Singleton { private static readonly Singleton 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

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

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 from. Cartesian(double real,

Factory code for complex numbers class Complex { public static Complex from. Cartesian(double real, double imag) { return new Complex(real, imag); } public static Complex from. Polar(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. from. Polar(1, pi);

Adapter Pattern • In computer programming, the adapter design pattern (often referred to as

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

Example : lines and rectangles (1 of 2) class Line { public void draw(int x 1, int y 1, int x 2, int y 2) { System. out. println("line("+x 1+', '+y 1+") to ("+x 2+', ' + y 2 + ')'); } } 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 Adapter. Demo { public

Example : lines and rectangles (1 of 2) public class Adapter. Demo { public static void main(String[] args) { Object[] shapes = { new Line(), new Rectangle() }; // A begin and end point from a graphical editor int x 1 = 10, y 1 = 20; int x 2 = 30, y 2 = 60; for (int i = 0; i < shapes. length; ++i) if (shapes[i]. get. Class(). get. Name(). equals("Line")) ((Line)shapes[i]). draw(x 1, y 1, x 2, y 2); else if (shapes[i]. get. Class(). get. Name(). equals("Rectangle")) ((Rectangle)shapes[i]). draw(Math. min(x 1, x 2), Math. min(y 1, y 2), Math. abs(x 2 - x 1), Math. abs(y 2 - y 1)); } }