Session 07: C# Design Patterns

Slides:



Advertisements
Similar presentations
Composite: Shapes Organisation State Observer Design Patterns.
Advertisements

Lecture 3 Introduction to Collections Advanced Java Programming 1 dr inż. Wojciech Bieniecki
Design Patterns CMPS Design Patterns Consider previous solutions to problems similar to any new problem ▫ must have some characteristics in common.
Design Patterns Examples of smart use of inheritance and polymorphism: Composite Pattern State Pattern FEN 2014UCN Teknologi/act2learn1.
Object-Oriented Design Patterns Composite Singleton State Observer … Autumn 2012UCN Technology: IT/Computer Science1.
Interfaces Reference: Joe Hummel. 2 UCN Technology: Computer Science 2012 Objectives “Good class design starts with good application design — how many.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Plab – Tirgul 12 Design Patterns
Software Reuse Building software from reusable components Objectives
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Ralph Johnson - University of Illinois1 Patterns: What They Are, and How to Write Them Ralph Johnson University of Illinois at Urbana-Champaign
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
5. OOP. 2 Microsoft Objectives “Classes, objects and object-oriented programming (OOP) play a fundamental role in.NET. C# features full support for the.
Design Patterns Ref : Chapter 15 Bennett et al. useful groups of collaborating classes that provide a solution to commonly occuring problems. provide.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Chapter 19 Java Data Structures
Abstract Data Types (ADTs) Data Structures The Java Collections API
Introduction to software design patterns For CSE 3902 By: Matt Boggus.
1 PH Chapter 1 (pp. 1-10) GoF Composite Pattern (pp ) PH Ch 2 through Fundamentals (pp ) Presentation by Julie Betlach 5/28/2009.
Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN AK IT: Softwarekonstruktion.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
1 Copyright © 2014 Atego. Patterns INCOSE MBSE WG – Simon A. Perry - Atego.
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.
Chapter Five An Introduction to Design Patterns Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
101 User Interface Patterns and its applications Tonya Groover Department of Computer Science.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
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.
1 CSC 222: Computer Programming II Spring 2004 See online syllabus at: Course goals:
1 CSC 222: Object-Oriented Programming Spring 2013 Course goals:  To know and use basic Java programming constructs for object- oriented problem solving.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
CSE 403 Lecture 14 Design Patterns. Today’s educational objective Understand the basics of design patterns Be able to distinguish them from design approaches.
Design Patterns Introduction General and reusable solutions to common problems in software design SoftUni Team Software University
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Software Engineering Design & UML.
CSC 480 Software Engineering Design With Patterns.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Interface: (e.g. IDictionary) Specification class Appl{ ---- IDictionary dic; dic= new XXX(); application class: Dictionary SortedDictionary ----
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
FEN 2014UCN Teknologi/act2learn1 Higher order functions Observer Pattern Delegates Events Visitor Pattern Lambdas and closures Lambdas in libraries.
FEN NOEA/IT: Advanced Computer Studies 1 Patterns The concept of patterns originates from architecture (Christopher Alexander, 1977): “Each pattern.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
Data Structures and Collections Principles.NET: –Two libraries: System.Collections System.Collections.Generics FEN 2014UCN Teknologi/act2learn1 Deprecated.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
1 Good Object-Oriented Design Dr. Radu Marinescu Lecture 4 Introduction to Design Patterns.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Computer Science Victoria University of Wellington Copyright: david streader, Victoria University of Wellington Simple Design COMP
CSC 222: Object-Oriented Programming
Chapter 0: Introduction
CSC 222: Object-Oriented Programming
Design Patterns: MORE Examples
The Object-Oriented Thought Process Chapter 15
Design Patterns Introduction
Data Abstraction: The Walls
CSC 222: Object-Oriented Programming
Object Oriented Programming in Java
Advanced Programming Behnam Hatami Fall 2017.
Web Programming Language
Programming Languages
5. OOP OOP © 2003 Microsoft.
Presentation transcript:

Session 07: C# Design Patterns Composite Pattern Iterator Pattern Interfaces in the libraries FEN 2013-04-01 AK - IT: Softwarekonstruktion

Example: Composite-pattern Composite: Graphical editor, Word processor, Inventories etc.. (What will a Show()-method look like on Shape, Circle, Picture etc.) FEN 2013-04-01 AK - IT: Softwarekonstruktion

AK - IT: Softwarekonstruktion public abstract class Shape { private int id; private string color; private int xpos; private int ypos; public void MoveTo(Position newPos){ // PRE none // POST pos'=newPos } public abstract void Show(); // POST the shape is displayed public abstract void Hide(); // POST the shape is hidden   public abstract float Area(); // POST Computes the area with 4 digits. }//end Shape FEN 2013-04-01 AK - IT: Softwarekonstruktion

AK - IT: Softwarekonstruktion public class Circle: Shape{ private int r; //radius public void Show(){ //PRE none //POST the circle is drawn //Must be implemented using appropriate //graphical routines } public void Hide(){ //PRE none //POST the circle is hidden // More operations }//end Circle FEN 2013-04-01 AK - IT: Softwarekonstruktion

Static type Shape – and Picture is a Shape itself! public class Picture : Shape{ List<Shape> pictList; // operations for adding, deleting etc. shapes public void Show(){ //PRE none //POST The composite shape is drawn for(Shape s : pictList) s.Show(); } public float Area() { float result=0; for(int i=0;i<pictList.Count;i++) { result= result+pictList[i].Area(); return result; }//end Picture Static type Dynamic type definesshow() FEN 2013-04-01 AK - IT: Softwarekonstruktion

AK - IT: Softwarekonstruktion Composite Pattern The concept of patterns originates from architecture (Christopher Alexander, 1977): “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice” (Christopher Alexander e. a.: “A Pattern Language”. Oxford University Press, New York, 1977.) FEN 2013-04-01 AK - IT: Softwarekonstruktion

AK - IT: Softwarekonstruktion (OO) Design Patterns A well known and widely accepted concept in software engineering Developed in the early 1990s and published by Gamma e.a. (“Gang of Four”, GoF) in 1995: “(…) design patterns (…) are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.” (Erich Gamma e.a.:”Design Patterns. Elements of Reusable Object-Oriented Software”. Addison-Wesley. 1995.) FEN 2013-04-01 AK - IT: Softwarekonstruktion

The benefits of patterns A pattern captures a proven good design: A pattern is based on experience A pattern is discovered – not invented It introduces a new (and higher) level of abstraction, which makes it easier: to talk and reason about design on a higher level to document and communicate design One doesn’t have to reinvent solutions over and over again Patterns facilitate reuse not only of code fragments, but of ideas. FEN 2013-04-01 AK - IT: Softwarekonstruktion

Patterns as a learning tool It is often said that good skills in software construction require experience and talent …and neither can be taught or learned at school Patterns capture experience (and talent) in a way that is communicable and comprehensible …and hence experience can be taught (and learned) So one should put a lot of effort in studying patterns FEN 2013-04-01 AK - IT: Softwarekonstruktion

AK - IT: Softwarekonstruktion More Examples Organisational Structures demos\RecursiveDirectoryTraverse A directory is a collection of elements which are either files or directories themselves. FEN 2013-04-01 AK - IT: Softwarekonstruktion

AK - IT: Softwarekonstruktion Exercises Exercise 4 on Session07.docx. FEN 2013-04-01 AK - IT: Softwarekonstruktion

AK - IT: Softwarekonstruktion Interfaces An interface may be seen as a purely abstract class. Interface: only method signatures, no implementation! (All methods are abstract) An interface represents a design. Example: Designing an object that can be used for iterating over a data structure. Also as generic public interface IEnumerator { void Reset(); // reset iterator to beginning bool MoveNext(); // advance to next element object Current { get; } // retrieve current element } FEN 2013-04-01 AK - IT: Softwarekonstruktion

Example – Iterator Pattern This piece of client code iterates over any data structure that implements IEnumerator: data structure iterator IEnumerator iter; iter = ...; // get ref to iterator object iter.Reset(); while ( iter.MoveNext() ) MessageBox.Show( iter.Current.ToString() ); See it work! FEN 2013-04-01 AK - IT: Softwarekonstruktion

Different Collections List: Array-based (static) Supports access by position Uses extra memory Expensive when re-allocating LinkedList: Linked list (dynamic, no extra memory) Doesn’t support access by position. HashSet (Dictionary): Array-based (static) Supports fast access (in average) by key – slow in worst case Uses extra memory Expensive when re-allocating SortedSet (SortedDictionary): Search tree (dynamic, no extra memory) Supports fast access (logarithmic) in average and worst case. FEN 2013-04-01 AK - IT: Softwarekonstruktion

In .NET interfaces are used heavily. IComparable ICloneable IDisposable IEnumerable & IEnumerator IList ISerializable IDBConnection, IDBCommand, IDataReader etc. FEN 2013-04-01 AK - IT: Softwarekonstruktion

AK - IT: Softwarekonstruktion Typical Design in APIs Specification in the interface. Implementation in the concrete classes. Operations with common implementation are implemented in the abstract class (no code duplication). Operation specific for the different concrete classes are left abstract in the abstract class. FEN 2013-04-01 AK - IT: Softwarekonstruktion

AK - IT: Softwarekonstruktion 8. Interfaces January 2003 Example: Sorting Goal: To write a generic Sort() method as the one found in System.Array Recall that System.Array class contains a static Sort() method that can sort any kind of array… FEN 2013-04-01 AK - IT: Softwarekonstruktion Denmark .NET Workshop © 2003 Joe Hummel

Step 1: Define the interface Sorting requires a way of comparison of objects: returns < 0 if this object < obj parameter returns == 0 if this object = obj parameter returns > 0 if this object > obj parameter public interface IComparable<T> { int CompareTo(T obj); } FEN 2013-04-01 AK - IT: Softwarekonstruktion

Step 2: Classes must implement the interface 8. Interfaces Step 2: Classes must implement the interface January 2003 Objects That are to be sorted must implement IComparable><> Example: sort Student objects on id public class Student : Icomparable<Student> { private int m_ID; . int IComparable.CompareTo(Student other) return this.m_ID – other.m_ID; } interface Student type Recall from earlier lecture that Student class inherited from Person class… FEN 2013-04-01 AK - IT: Softwarekonstruktion Denmark .NET Workshop © 2003 Joe Hummel

Step 3: Clients program towards the interface 8. Interfaces January 2003 Step 3: Clients program towards the interface Sort assumes that the elements in the array a implement IComparable: public class Array { public static void Sort(Array a) IComparable icobj; for (int i = 0; i < a.Length-1; i++) { for (int j = i+1; j < a.Length; j++) { icobj = (IComparable) a.GetValue(i); if (icobj.CompareTo(a.GetValue(j)) > 0) swap(a, i, j); }//for } Not a very efficient sort, but you get the idea — this method will sort any array of objects, as long as those objects implement IComparable. FEN 2013-04-01 AK - IT: Softwarekonstruktion Denmark .NET Workshop © 2003 Joe Hummel

AK - IT: Softwarekonstruktion Step 4: test! Example: sort an array of Student objects View Code FEN 2013-04-01 AK - IT: Softwarekonstruktion

AK - IT: Softwarekonstruktion Exercises Exercise 5 on Session07.docx. FEN 2013-04-01 AK - IT: Softwarekonstruktion