Design Patterns C++ Java C#.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Design Patterns.
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable.
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Introduction to Object-oriented programming and software development Lecture 1.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Programming in Java CSCI-2220 Object Oriented Programming.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
Programmeerimine Delphi keskkonnas MTAT Programmeerimine Delphi keskkonnas MTAT Jelena Zaitseva
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Object Oriented Programming
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Introduction To Design Patterns
Modern Programming Tools And Techniques-I
Object-Orientated Analysis, Design and Programming
Design Patterns: Brief Examples
Unit II-Chapter No. : 5- design Patterns
Building Java Programs
Inheritance ITI1121 Nour El Kadri.
Friends of Template Classes
MPCS – Advanced java Programming
Design Patterns C++ Java C#.
Low Budget Productions, LLC
Factory Patterns 1.
Pertemuan 08 Design Patterns & Anti-Patterns
Inheritance-Basics.
Final and Abstract Classes
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
03/10/14 Inheritance-2.
Design Patterns Damian Gordon.
Introduction To Design Patterns
Design Patterns Based on slides provided by Abbie Jarrett
Software Design and Architecture
Plan for today Refactoring and Design Patterns
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Advanced Programming Behnam Hatami Fall 2017.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Week 6 Object-Oriented Programming (2): Polymorphism
What is Singleton Category: Creational pattern
Singleton design pattern
CS 350 – Software Design Singleton – Chapter 21
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Chapter 8 Inheritance Part 2.
Introduction To Design Patterns
OOP Aga private institute for computer science 5th grade
Final and Abstract Classes
Introduction To Design Patterns
Software Design Lecture : 28.
CSG2H3 Object Oriented Programming
עקרונות תכנות מונחה עצמים תרגול 7 – Design Patterns
Presentation transcript:

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 (2 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 int x1 = 10, y1 = 20; int x2 = 30, y2 = 60; for (int i = 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)); }