Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
April 20023CSG11 Electronic Commerce Java (1) John Wordsworth Department of Computer Science The University of Reading Room 129,
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Design Patterns Yes, they are important Robert Cotton April 23, 2009.
05/26/2004www.indyjug.net1 Indy Java User’s Group June Knowledge Services, Inc.
Computers: Tools for an Information Age
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
March R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software.
1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
+ Java vs. Javascript Jessi Style. + Java Compiled Can stand on its own Written once, run anywhere Two-stage debugging Java is an Object Oriented Programming.
Design Patterns Alan Shalloway, James Trott, Design Patterns Explained, Addison-Wesley, Gamma, Helm, Johnson, Vlissides, Design Patterns, Elements.
Programming Languages and Paradigms Object-Oriented Programming.
Microsoft Visual Basic 2005: Reloaded Second Edition
UNIVERSITI TENAGA NASIONAL “Generates Professionals” CHAPTER 4 : Part 2 INTRODUCTION TO SOFTWARE DEVELOPMENT: PROGRAMMING & LANGUAGES.
Implementing Design Patterns Using Java St. Louis Java Special Interest Group Eric M. Burke Object Computing, Inc. Presented on July 9, 1998 (updated July.
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.
CSSE 374: Introduction to Gang of Four Design Patterns
DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer You may like to write these down now...
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
CS 325: Software Engineering February 12, 2015 Applying Responsibility-Assignment Patterns Design Patterns Situation-Specific Patterns Responsibility-Assignment.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
(c) University of Washington08-1 CSC 143 Models and Views Reading: Ch. 18.
Computer Programs and Programming Languages What are low-level languages and high-level languages? High-level language Low-level language Machine-dependent.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
CPSC 372 John D. McGregor Module 4 Session 1 Design Patterns.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
Module 4 Part 2 Introduction To Software Development : Programming & Languages Introduction To Software Development : Programming & Languages.
05/26/2004www.indyjug.net1 Indy Java User’s Group May Knowledge Services, Inc.
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
12 Chapter 12: Advanced Topics in Object-Oriented Design Systems Analysis and Design in a Changing World, 3 rd Edition.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 1 An Introduction to Visual Basic.NET and Program Design.
Lesson 2: First Java Programs. 2.1 Why Java? Java is one of the most popular programming languages in the world. Java is a modern object-oriented programming.
PROGRAMMING (1) LECTURE # 1 Programming and Languages: Telling the Computer What to Do.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Introduction CMSC 202 Fall Instructors Mr. Ryan Bergeron – Lecture Section 01 Tues/Thu 1:00 – 2:15 am, Sondheim 111 – Lecture Section 04 Tues/Thu.
Introduction To Design Patterns
Design Patterns Source: “Design Patterns”, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides And Created.
The need for Programming Languages
The Object-Oriented Thought Process Chapter 15
MPCS – Advanced java Programming
Design Patterns C++ Java C#.
Design Patterns Introduction
Design Patterns C++ Java C#.
Introduction To Design Patterns
Plan for today Refactoring and Design Patterns
Advanced Programming Behnam Hatami Fall 2017.
08/15/09 Design Patterns James Brucker.
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Model, View, Controller design pattern
Introduction To Design Patterns
Introduction To Design Patterns
Presentation transcript:

Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.

James Tam What Is A Design Pattern? A general and reusable solution to a commonly occurring problem in the design of software. IT IS a template for how to solve a problem that has been used in many different situations. IT IS NOT a finished algorithm that can be directly translated into program code. The various Object-Oriented design patterns show interactions between classes and objects without being tied to the specific the program code that implements the pattern (language independent) – e.g., Information hiding, inheritance etc.

James Tam Origin Of Design Patterns The foundation for design patterns come from the original patterns specified in the book “ Design Patterns: Elements of Reusable Object-Oriented Software ” Authors: “The gang of four” (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides). Although examples of the patterns were provided in the C++ and SmallTalk programming languages the patterns can be applied to any Object-Oriented language.

James Tam The Model-View-Controller Pattern 1 Sometimes the same data may have to be accessed under different contexts e.g., powerful desktop, web, mobile device. Each context may require a different interface (e.g., web page on a mobile device, software on a computer). Even the context of a single program running on a single device there may be a desire to see different views of the data e.g., financial analysts may want to see details (spreadsheet and/or financial statement) whereas the shareholders or management may focus on overview views (graphs) 1 Some additional sources that describe the model-view controller pattern: I.Sun Microsystems: II.Microsoft:

James Tam The Model-View-Controller Pattern 1 With this pattern, different parts are separate and independent: – Model: The data (database, text file): – View: How the data appears or the perspective under which it is viewed (graph, numerical) – Controller: How the data can be interacted with (GUI, command line). Model State (data) View Display of data Interface Controller Event handling State change State query Change notification User interaction View selection

James Tam Model-View-Controller Pattern (2) Implementing different parts that are decoupled (minimized dependencies) provides many benefits: – One part may be changed independent of the other parts e.g., updates to the interface can have minimal impact on the data. – It’s seldom that one person will have a deep understanding of all parts (e.g., knowledge of Accounting to create the financial statements vs. knowledge of web design to create the web interface). Different people with different areas of expertise can work on the different parts. – One version of the data can be created and maintained and as needed different ways of interacting and viewing data can be developed.

James Tam The Strategy Pattern The algorithm is determined at run time. Chess algorithms (Difficulty levels) Beginner Intermediate Advanced Computer fighting style: sparring simulation Hard- style Muay Thai Soft style Images: colourbox.com

James Tam The Strategy Pattern (2) One object contains a reference to another object. The second object determines the algorithm to execute.

James Tam The Strategy Algorithm: Example Location of the example: /home/219/examples/designPatterns/strategy public class Driver { public static void main (String [] args) { MyContainer aContainer = null; // First algorithm aContainer = new MyContainer (new AddAlgorithm()); System.out.println(aContainer.executeAlgorithm(2,5)); // Second algorithm aContainer = new MyContainer (new MultiplyAlgorithm()); System.out.println(aContainer.executeAlgorithm(2,5)); }

James Tam The Strategy Algorithm: An Example (2) public class MyContainer { private Algorithm anAlgorithm; public MyContainer (Algorithm anAlgorithm) { this.anAlgorithm = anAlgorithm; } public int executeAlgorithm (int x, int y) { return(anAlgorithm.execute(x,y)); }

James Tam The Strategy Algorithm: An Example (3) public interface Algorithm { public int execute (int x, int y); } public class AddAlgorithm implements Algorithm { public int execute (int x, int y) { return (x+y); } public class MultiplyAlgorithm implements Algorithm { public int execute (int x, int y) { return (x*y); }

James Tam Advantages Of The Strategy Pattern It decouples the context/container from the algorithm used by the context/container. – For the container it may allow the context/container to easily substitute additional algorithms. ‘Expansion packs’ – For the algorithm, the algorithm may be used in a number of different contexts/containers (e.g., sorting algorithms).

James Tam Side Note: Static Attributes Static attributes of a class are initialized when the Java virtual machine (“java”) loads a class into memory. This must be done before any of the methods of the class can be called (even the constructor). Location of an illustrative example: /home/219/examples/designPatterns/static

James Tam Static Attributes: Driver Class public class Driver { public static void main (String [] args) { Foo aFoo = new Foo(); }

Static Attributes: Class Foo & Bar public class Foo { private static Bar aBar = new Bar(); public Foo() { System.out.println(">>> Trace only: constructor Foo() <<<"); } public class Bar { public Bar() { System.out.println(">>> Trace only: constructor Bar() <<<"); }

James Tam The Singleton Pattern Singleton class: there is only one instance of the class (one object). That object provides a common set of operations for the rest of the program and globally accessible (variable) data. It is not the same as a purely static class. – Static methods but no variable attributes. The Singleton pattern is enforced by making the constructor private. Example singleton class: Random number generator. – For testing/debugging it is desirable to generate the same sequence of random numbers.

James Tam Singleton Example Location of the example: /home/219/examples/designPatterns/singleton

James Tam Singleton: Driver public class DriverSingleRandom { public static void main(String [] args) { SingleRandom aSingleRandom = SingleRandom.getInstance(); aSingleRandom.setSeed(1); for (int i = 0; i < 10; i++) System.out.println(i + ": " + aSingleRandom.nextInt()); System.out.println(); }

James Tam Class SingleRandom public class SingleRandom { private Random generator; private static SingleRandom instance = new SingleRandom(); private SingleRandom() { System.out.println(">>> Trace only: this.SingleRandom() <<<"); generator = new Random(); } public static SingleRandom getInstance() { System.out.println(">>> Trace only: SingleRandom.getInstance() <<<"); return(instance); } 1st 2nd 3rd

James Tam Class SingleRandom (2) public void setSeed(int seed) { System.out.println(">>> Trace only: ref.setSeed() <<<"); generator.setSeed(seed); } public int nextInt() { System.out.println(">>> Trace only: ref.nextInt() <<<"); return (generator.nextInt()); }

James Tam Discussions/Resources: Singleton Pattern

James Tam You Should Now Know What is a design pattern How the three example design patterns work