Software Design Patterns Email: Walaagad@cis.asu.edu.eg Lecture 1 Design Patterns Prof. Walaa Khaled 1 Textbooks • Eric Freeman; Elisabeth Freeman, O'Reilly, Head First Design Patterns: your brain on design patterns, • Design Patterns: Elements of Reusable Object-Oriented Software, Erich Gamma, Ralph Johnson, John Vlissides, Richard Helm Course Work and Marking Scheme • Midterm: 15% •Class work: 15% •Project 20% •Final: 50% Design Patterns Prof. Walaa Khaled 2 Course Content • Creational Patterns –Factory –Abstract –Singleton –Builder • Structural Patterns –Decorator –Adapter –Façade –Proxy –Bridge • Behavioral Patterns –Strategy –Observer –Command –Template Design Patterns Prof. Walaa Khaled 3 Perquisites to understand Design Patterns • Classes • Encapsulation • Inheritance • Polymorphism • Interfaces • Abstraction Design Patterns Prof. Walaa Khaled 4 What are Design Patterns “Best practice to solve common software problems” “Solutions in the form of templates that may be applied to real-world problems Design Patterns Prof. Walaa Khaled 5 6 Single Responsibility Principle (SRP) The class should solve only one problem. It should have a single reason to change Design Patterns Prof. Walaa Khaled 7 Open/Closed Principle (OCP) The class should be open for extension and closed for modifications. Design Patterns Prof. Walaa Khaled 8 Liskov Substitution Principle If you substitute any type with one of its subtypes, the behavior should not change. Design Patterns Prof. Walaa Khaled 9 Interface Segregation Principle Avoid making a general interface containing all methods. Design Patterns Prof. Walaa Khaled 1 0 Dependency Inversion Principle Higher Level Classes should not know the implementation of low-level classes but depends on abstraction. Design Patterns Prof. Walaa Khaled 1 1 UML (Unified Model Language) • UML is a general-purpose, developmental, modeling language in the field of software engineering that is intended to provide a standard way to visualize the design of a system • Its primary purpose during analysis workflow: to create a vocabulary that is used by both the analyst and users • It describes the structure of the system in terms of classes and objects Design Patterns Prof. Walaa Khaled 1 2 What is Not represented in a UML class diagram? • Details of how the classes interact with each other. • Algorithmic details; how a particular behavior is implemented Design Patterns Prof. Walaa Khaled 1 3 Class representation The picture of a class contains the following 3 boxes: • Class name in top box. – Use italics for an abstract class name. – Attributes (optional) in middle box. – Should include all fields of the object. • Operations / Methods (optional) in bottom box. – May omit trivial (get/set) methods. – Should not include inherited methods. Design Patterns Prof. Walaa Khaled 1 4 Class Attributes Syntax of defining class attributes in a class diagram is: visibility name : type [count] = default_value • visibility. – “ + “ public “ # “ protected “ – “ private “ / “ derived • Notes: –Underline static attributes. –Derived attributes are not stored, but can be computed from other attribute values. Design Patterns Prof. Walaa Khaled 1 5 Class methods Syntax of defining class methods in a class diagram is: visibility name (parameters) :return type • visibility. – “ + “ public “ # “ protected “ – “ private “ / “ derived • Notes: –Underline static methods. –Omit return_type on constructors and when return type is void Design Patterns Prof. Walaa Khaled 1 6 Relationship between classes There are three kinds of relationships in UML: •Generalizations • an inheritance relationship. • Inheritance between classes. • Aggregations. • whole --> superclass part --> supclass Composition. Design Patterns Prof. Walaa Khaled 1 7 Generalization Relationship Design Patterns Prof. Walaa Khaled 1 8 Aggregation Relationship • Aggregation is a strong form of association in which an aggregate objects made of constituent parts. C B A • Aggregation is a transitive relation: –if A is a part of B and B is a part of C then A is also a part of C • Aggregation is an antisymmetric relation: B A –If A is a part of B then B is not a part of A. Design Patterns Prof. Walaa Khaled 1 9 Aggregation Relationship • Aggregation adds semantic connotations: • An aggregation specifies a whole-part relationship between an aggregate (a whole) and a constituent part, where the part can exist independently from the aggregate. • Aggregations are denoted by a hollow-diamond adornment on the association. ﻣﻔرغ Design Patterns Prof. Walaa Khaled 2 0 Composition Relationship • Composition is a form of aggregation with additional constraints: • A constituent part can belong to at most one assembly (whole). • it has a coincident lifetime with the assembly. • Deletion of an assembly object triggers automatically a deletion of all constituent objects via composition. • They live and die as a whole. Design Patterns Prof. Walaa Khaled 2 1 Composition Relationship • Compositions are denoted by a filled-diamond adornment on the association. solid Design Patterns Prof. Walaa Khaled 2 2 What are Design Patterns • Design Patterns are evolved from the features of object oriented programming as reusable solutions to the problems that we encounter every day of programming. • They are generally targeted at solving the problems of object generation and integration. • These generalized patterns act as templates that can applied to the real world problems Design Patterns Prof. Walaa Khaled 2 3 What are Design Patterns • Implementing design patterns in the applications are proven and tested. • Writing the code aligning with design patterns will make your application reliable, scalable and maintainable. Design Patterns Prof. Walaa Khaled 2 4 Types of Design Patterns Important There are three types of Design Patterns • Creational –Deals with the object creation and initialization. –Ex. Singleton, Factory, Abstract Factory,… • Structural –Deals with class and object composition. –Adapter , Bridge, … • Behavioral –It characterizes the way in which classes or objects interact and distribute responsibility. –Interpreter, chain of responsibilities, Command, … Design Patterns Prof. Walaa Khaled 25 23 Pattern Design Patterns Space Design Patterns Prof. Walaa Khaled 26