Construction Principles and Design Patterns. Flyweight, Bridge, Builder

Similar documents
Software Engineering

Programming and Software Development CTAG Alignments

Decomposition into Parts. Software Engineering, Lecture 4. Data and Function Cohesion. Allocation of Functions and Data. Component Interfaces

Motivation Definitions EAI Architectures Elements Integration Technologies. Part I. EAI: Foundations, Concepts, and Architectures

XXI. Object-Oriented Database Design

Aspect-Oriented Programming

2 (18) - SOFTWARE ARCHITECTURE Service Oriented Architecture - Sven Arne Andreasson - Computer Science and Engineering.

Java (12 Weeks) Introduction to Java Programming Language

Patterns in. Lecture 2 GoF Design Patterns Creational. Sharif University of Technology. Department of Computer Engineering

Service-Oriented Architectures

An Introduction to Software Architecture

Java Application Developer Certificate Program Competencies

Glossary of Object Oriented Terms

Software Design. Design (I) Software Design Data Design. Relationships between the Analysis Model and the Design Model

Introduction to programming

The C Programming Language course syllabus associate level

Systems Integration: Co C mp m onent- t bas a e s d s o s ftw ft a w r a e r e ngin i eeri r n i g

Service Oriented Architecture

Evaluating OO-CASE tools: OO research meets practice

Distributed systems. Distributed Systems Architectures

The Service Revolution software engineering without programming languages

Advanced Analysis and Design

Object Oriented Design

Architectural patterns

Java Programming (10155)

Software Quality Factors OOA, OOD, and OOP Object-oriented techniques enhance key external and internal software quality factors, e.g., 1. External (v

COSC 3351 Software Design. Recap for the first quiz. Edgar Gabriel. Spring For the 1 st Quiz

Software Engineering. Software Engineering. Component-Based. Based on Software Engineering, 7 th Edition by Ian Sommerville

Moving from CS 61A Scheme to CS 61B Java

Design by Contract beyond class modelling

Component visualization methods for large legacy software in C/C++

OKLAHOMA SUBJECT AREA TESTS (OSAT )

An Aspect-Oriented Product Line Framework to Support the Development of Software Product Lines of Web Applications

Patterns in Software Engineering

Component Based Software Engineering: A Broad Based Model is Needed

ARIZONA CTE CAREER PREPARATION STANDARDS & MEASUREMENT CRITERIA SOFTWARE DEVELOPMENT,

Requirements engineering

WebSphere Business Modeler

Fourth generation techniques (4GT)

KWIC Exercise. 6/18/ , Spencer Rugaber 1

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

How To Understand The Difference Between Business Process And Process Model In Java.Java.Org (Programming)

Business-Driven Software Engineering Lecture 3 Foundations of Processes

JMulTi/JStatCom - A Data Analysis Toolkit for End-users and Developers

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANKULATHUR DEPARTMENT OF COMPUTER APPLICATIONS SUBJECT : MC7502 SERVICE ORIENTED ARCHITECTURE

A Meeting Room Scheduling Problem

Programming Language Constructs as Basis for Software Architectures

High-level Design. What is software architecture?

Visual Basic. murach's TRAINING & REFERENCE

Lecture 1: Introduction

Architectural Patterns. Layers: Pattern. Architectural Pattern Examples. Layer 3. Component 3.1. Layer 2. Component 2.1 Component 2.2.

FreeForm Designer. Phone: Fax: POB 8792, Natanya, Israel Document2

Dynamic Adaptability of Services in Enterprise JavaBeans Architecture

CEC225 COURSE COMPACT

Contents. Introduction and System Engineering 1. Introduction 2. Software Process and Methodology 16. System Engineering 53

Texas Essential Knowledge and Skills Correlation to Video Game Design Foundations 2011 N Video Game Design

Case Studies of Running the Platform. NetBeans UML Servlet JSP GlassFish EJB

Software Development Kit

Chapter 13: Program Development and Programming Languages

Pattern. seconda parte. Types of patterns. ...other good guidance... some GRASP. design patterns (software design) Types of software patterns

Data Modeling Basics

WebSphere Business Monitor

Excerpts from Chapter 4, Architectural Modeling -- UML for Mere Mortals by Eric J. Naiburg and Robert A. Maksimchuk

Distributed Objects and Components

Unit I Page No. 1 System Development Object Basics Development Life Cycle Methodologies Patterns Frameworks Unified Approach UML

Automatic generation of fully-executable code from the Domain tier of UML diagrams

Tool Support for Model Checking of Web application designs *

ARCHITECTURAL DESIGN OF MODERN WEB APPLICATIONS

25.1 Translational Frameworks (MDA with transformations)

A Framework for Software Architecture Visualization and Evaluation

Model Driven Interoperability through Semantic Annotations using SoaML and ODM

SOA, case Google. Faculty of technology management Information Technology Service Oriented Communications CT30A8901.

Engineering Design. Software. Theory and Practice. Carlos E. Otero. CRC Press. Taylor & Francis Croup. Taylor St Francis Croup, an Informa business

Software Engineering: Reflections on an Evolving Discipline

Fundamentals of Java Programming

Automated Virtual Cloud Management: The need of future

A standards-based approach to application integration

CS506 Web Design and Development Solved Online Quiz No. 01

New Generation of Software Development

EVALUATION. WA1844 WebSphere Process Server 7.0 Programming Using WebSphere Integration COPY. Developer

Customer Bank Account Management System Technical Specification Document

What do you think? Definitions of Quality

SOFT 437. Software Performance Analysis. Ch 5:Web Applications and Other Distributed Systems

Tutorial on Writing Modular Programs in Scala

Programming with the Microsoft.NET Framework Using Microsoft Visual Studio 2005 (VB)

UML Class Diagrams. Lesson Objectives

Run-time Variability Issues in Software Product Lines

A Dynamic, Runtime-Extensible, Client-Managed Service Framework

BUSINESS RULES CONCEPTS... 2 BUSINESS RULE ENGINE ARCHITECTURE By using the RETE Algorithm Benefits of RETE Algorithm...

Questions? Assignment. Techniques for Gathering Requirements. Gathering and Analysing Requirements

Event-based middleware services

A Process for ATLAS Software Development

Architecture. Reda Bendraou

Concepts and terminology in the Simula Programming Language

dynamic Learning Content Management System

Transcription:

Construction Principles and Design Patterns Flyweight, Bridge, Builder 1

The Flyweight Design Pattern: Structure Use: To avoid employing a large number of objects with similar state When objects with intrinsic state can be re-used by different clients 2

The Flyweight Design Pattern: Example(I) Machine1 Machine3 Machine2 Robot1 Machine4 Robot2 Intersection of type A Intersection of type B 3

The Flyweight Design Pattern: Example(II) IntersectionStore getintersection(coordinates) Intersection resolveconflict(newpath,activepaths) Intersection_A resolveconflict(...) Intersection_B resolveconflict(...) PathFinder genpath(j:job):path segments:grid FactoryFloor activepaths:path[] New intersection types can be easily added! 4

The Bridge Design Pattern: Structure Used when: An abstraction should be decoupled from its implementation The abstraction and its implementation should be (independently) extensible by subclassing 5

The Bridge Design Pattern: Example (I) PathFinder genpath(j:job):path for (int i=0;i<ja.length; i++) ret[i]=genpath(ja[i]); ShortestLength genpath(j:job):path ShortestTime genpath(j:job):path MultiplePathFinder genpaths(ja:job[]):path[] mergepaths() Spurious subclassing: ShortestLengthMLP genpath(j:job):path ShortestTimeMLP genpath(j:job):path 6

The Bridge Design Pattern: Example (II) PathFinder genpath(j:job):path impl PathFinderImpl genpathimpl(j:job):path impl.genpathimpl(j); ShortestLength genpathimpl(j:job):path ShortestTime genpathimpl(j:job):path MultiplePathFinder genpaths(ja:job[]):path[] mergepaths() 7

The Builder Design Pattern: Structure Used when The construction of a product is independent of the parts Different representations for the product can be employed 8

The Builder Design Pattern: Example (I) LayoutParser readlayout() nexttoken() builder FloorBuilder placemachine(name,size) placerobotpath(coords) t = nexttoken(); // if t is a machine... builder.placemachine(n,s); FullMatrixFloor placemachine(name,size) placerobotpath(coords) getfmfloor():fmfloor SparseMatrixFloor placemachine(name,size) placerobotpath(coords) getsmfloor():smfloor XMLLayoutParser nexttoken() TXTLayoutParser nexttoken() SparseMatrixParser nexttoken() Hook Method and Hook Object with the same template method 9

The Builder Design Pattern: Example (II) Usage: SMFloor sparse_floor; FloorBuilder sm_builder = new SparseMatrixFloor(); LayoutParser layin = new XMLLayoutParser(sm_builder, xml_file); layin.readlayout(); sparse_floor=sm_builder.getsmfloor(); Change of built object at runtime: FMFloor full_floor; FloorBuilder fm_builder = new FullMatrixFloor(); LayoutParser layin = new SparseMatrixParser(fm_builder, sparse_floor); layin.readlayout(); full_floor = fm_builder.getfmfloor(); 10

Modularization and Software Architectures 11

Contents The notion of software module Desired characteristics of modules Specification of modules (ADS, ADT) Description of software architectures Analysis of software architectures 12

The Software Module 13

Definition A module (software component) is defined as a piece of software with a programming interface. One distinguishes between the interface of a module and its implementation. The possible interaction of several modules is determined by their programming interfaces. The programming interface is the export interface, which indicates what operations and data a module makes available to other modules. The import interface indicates what a module uses from other modules. In the sequel, by interface we mean export interface unless explicitly noted otherwise. 14

Module as a means of abstraction M. Reiser and N. Wirth (1992) describe the module concept as follows: The module provides mechanisms for: (1) structuring of a program into independent units; (2) declaration of variables that keep their value for the duration the module is active (that is, in memory) these variables are called global to the module; (3) export of variables and procedures to be used in other modules. The module therefore provides facilities for abstractions. 15

Module as expression means for modeling By a skillful factorization of functions, procedures and data, one can form abstractions which correspond to entities from the material world as modeled in a software system. Examples: a bank account, the GPS navigation unit of a helicopter, the file system of a PC, the interaction elements of a graphic user interface. 16

17 Example: Module for storing error messages

Module interface of ErrorLogger (in Modula-2) DEFINITION MODULE ErrorLogger; FROM ErrorStream IMPORT ErrorMsg, ErrorType; /* Import interface */ PROCEDURE AddErrorMsg( em: ErrorMsg); PROCEDURE PrintAllErrorMsgs(); PROCEDURE PrintErrorMsgsOfCertainType( et: ErrorType); PROCEDURE ClearAll();... END ErrorLogger. 18

Module implementation of ErrorLogger (in Modula-2) IMPLEMENTATION MODULE ErrorLogger; VAR errors: ARRAY [0..cMaxNoOfStoredErrors 1] OF ErrorMsg; PROCEDURE AddErrorMsg( em: ErrorMsg) BEGIN... /* insert em at the next free space in the field errors */ END AddErrorMsg;... END ErrorLogger. ARRAY => statically established upper limit for the number of storable error messages 19

Advantage of the separation of interface from implementation The implementation of the module can be improved or changed without changing the interface. For example, in the module ErrorLogger the array structure could be replaced by a concatenated list. The concept of information hiding (see the following section) contributes to a stable interface of a module. 20

Desired Characteristics of Modules 21

Stable and Understandable Module Interfaces by Information Hiding (I) The design principle Information Hiding goes back to David L. Parnas (1972). Thus, modules are to be designed in such a way that the data structures are hidden from the user. Access to data and its manipulation is possible only over access procedures, which are aforementioned in the module interface. 22

Stable and Understandable Module Interfaces by Information Hiding (II) A generalization of the information hiding principle is the requirement that in the design of modules one takes care to hide as many details of the implementation as possible behind the module interface, in order not to confront the user of a module with unnecessary details and complexity. This can go beyond just hiding the data structures. 23

Example: Module Throttle for controlling a butterfly valve (I) interface Throttle { bool TurnThrottleOnOff(bool onoff); bool SetThrottlePosition(float angle); float GetThrottlePosition(); } // 0..90 grades 24

25 Example: Module Throttle for controlling a butterfly valve (II)

Balance between coupling and cohesion (I) By module coupling we understand the dependence and interaction between modules, which is specified on one hand statically by the import interface and on the other hand dynamically by calls of procedures and functions and/or by the access to data. The module coupling is to be minimized. By module cohesion we understand the degree to which data and operations which logically belong together are bound to the same module. The module cohesion is to be maximized. 26

Balance between coupling and cohesion (II) One could bring the coupling to a minimum, by defining only one module. If only one module is present, this is coupled with no other module, hence the coupling is minimal. However such a module would have also a minimum cohesion (except in trivial cases), since all aspects of the system are mixed in the module. The other extreme would be that each function and procedure is enclosed in a separate module. That would lead to a very strong coupling of the modules. One cannot talk about cohesion in this setup. 27

28 Example for improved modularity

Rules for cohesion maximization (I) The data structures (instance variables in classes) and the operations (=functions/procedures/methods) should be in close relationship to each other. Distinct groups of operations, working on different data, are an indicator for the fact that logically unrelated aspects were included in a module. Splitting up the module contributes to the improvement of the cohesion. The module interface should not contain redundant operations. This is applicable if several slightly different operations are specified for the same functionality. In addition, each operation should be conceived such that it uses a small number of parameters. 29

Rules for cohesion maximization (II) A consistent and expressive naming schema is to be used. This applies in particular to the names of modules as well as to the names of the operations defined in the interfaces. Examples of well selected and consistent naming schemas are modern object-oriented class libraries like the.net and Java libraries. Global data objects should be avoided 30

Heuristic for achieving an adequate coupling The individual module should be well understandable in itself. In other words, for understanding a module it should not be necessary to look at other modules. A similar statement applies to testing modules. The larger is the ensemble of modules which is needed to test a module, the stronger is the coupling of the respective module with the other modules. 31

Evaluation of modularization quality (I) The software architecture analysis method (SAAM) aims to examine the coupling and cohesion for low complexity systems. There is no generally valid metric that can evaluate the two characteristics objectively. 32

Evaluation of modularization quality (II) The correct balance of coupling and cohesion in the structuring of software is therefore an art, which requires high qualification and much experience. As in Architecture, examples can help to create awareness and feeling for good modularity. Contrary to Architecture, unfortunately only few good examples of software architectures are available in the open literature. 33

Improving Cohesion in the Butterfly Valve Example interface Throttle { bool TurnThrottleOnOff(bool onoff); bool SetThrottlePosition(float angle); // 0..90 Grad float GetThrottlePosition(); } Low cohesion: 34

35 Split up the module Throttle while maintaining the interface

Module Specification 36

Module as Abstract Data Structure (ADS) A module is defined without specifying a type Example: Module ErrorLogger as defined above DEFINITION MODULE ErrorLogger; FROM ErrorStream IMPORT ErrorMsg, ErrorType; /* Import interface */ PROCEDURE AddErrorMsg( em: ErrorMs); PROCEDURE PrintAllErrorMsgs(); PROCEDURE PrintErrorMsgsOfCertainType( et: ErrorType); PROCEDURE ClearAll();... END ErrorLogger. 37

Module as Abstract Data Type (ADT) I A module is defined as a type and one can form as many instances of it as desired. In Modula-2: Module ErrorLogger with opaque type ErrorLogger. DEFINITION MODULE ErrorLoggers; FROM ErrorStream IMPORT ErrorMsg, ErrorType; TYPE ErrorLogger; PROCEDURE NewErrorLogger( VAR el: ErrorLogger); PROCEDURE AddErrorMsg( VAR el: ErrorLogger, em: ErrorMsg); PROCEDURE PrintAllErrorMsgs( el: ErrorLogger); PROCEDURE PrintErrorMsgsOfCertainType( el: ErrorLogger, et: ErrorType); PROCEDURE ClearAll( VAR el: ErrorLogger);... END ErrorLoggers. 38

Module as Abstract Data Type (ADT) II Producing instances with NewErrorLogger: calcerrors, inputerrors: ErrorLogger; NewErrorLogger( calcerrors); NewErrorLogger( inputerrors); AddErrorMsg( calcerrors, any message);... PrintAllErrorMsgs( calcerrors); PrintAllErrorMsgs( inputerrors); 39

ADT in OO Languages (e.g. in C#) I using System; using System.Collections; namespace ErrorLibrary { public class ErrorLogger { private IList errorlist; Interface and implementation in the same file! public ErrorLogger() { } errorlist= new ArrayList(); } } public void AddErrorMsg(ErrorMsg em) { errorlist.add(em); }... 40

ADT in OO Languages (e.g. in C#) II ErrorLogger calcerrors, inputerrors; calcerrors = new ErrorLogger(); inputerrors = new ErrorLogger();... try {... } catch (FloatingPointException fe) { calcerrors.adderrormsg(new ErrorMsg("floating point exception,...); //... means more information } 41

42 ADT in OO Languages III

ADT in OO Languages IV as UML class diagram: 43

Definition of ADS and ADT in programming languages Oberon (- 2): supports both concepts by language constructs Java and C#: Classes for the definition of ADT. Syntactic support of the definition of ADS by static instance variables and methods. Packages and name spaces let several classes combine into a unit. 44

Modules in current component standards Component standards differ among other things in the syntax, how the interface is defined by components: CORBA (Common Object Request Broker Architecture): CORBA IDL (Interface Description LANGUAGE); maps to C++ JavaBeans: Interface is defined in Java Web services: XML-based WSDL (Web Services Description Language) 45

Description of Software Architectures 46

Definition of Software Architecture The assembly of all the components (modules) of a software system together with their interactions. 47

Architectural styles In the 90s, The Software Engineering Institute (SEI) of the Carnegie Mellon University in Pittsburgh, Pennsylvania, considerably contributed to the establishment of architectural styles for the description of software architectures. SEI originally suggested a dedicated notation for architecture description; since 2003, SEI has used also the UML for that. 48

Examples of well described software architectures Project Oberon The Design of an Operating System and Compiler by Wirth and Reiser (Addison-Wesley 1992). The informal description is supplemented by schematic representations, screenshots, and source text. Design Patterns of Gamma et al. (Addison-Wesley 1995) 49

Overview of SEI architectural styles Architectural style Data-centered Data-flow Call & Return Virtual Machine Independent Components Characteristics Repository Architecture Blackboard Architecture Batch/Sequential Architecture Pipes&Filters Architecture Top-Down Architecture Network Architecture (Object oriented) Layered Architecture Interpreter Architecture Rule-based Architecture Event-driven Architecture 50

Data-centered (I) Legend: Component (UML package) Bidirectional data flow In a Repository architecture the data is passive. A Blackboard architecture has quasi-active data, which informs the clients interested in changes. The Blackboard architecture style is similar to the Observer design pattern (Gamma et al., 1995). 51

Data-centered (II) Advantage: Clients are independent from each other. Thus, a client can be changed, without affecting the others. Also further clients can be added. This advantage pales if the architecture is changed in such a way that clients are coupled closely (thus deviating from the recommended architecture style), for example in order to improve the performance of the system. 52

Data-centered (III) Issues that must be addressed: Data consistency - synchronization of read/write operations Data security, access control Single point of failure 53

Data-flow style: Batch/Sequential The style describes a succession of transformations of input data. Data flow-oriented architecture parts are particularly characterized by reusability and modifiability. In the Batch/Sequential form, each transformation procedure must be terminated before the next one begins. 54

Data-flow style: Pipes&Filters In the Pipes&Filters form, data is incrementally transformed. That is, the data is divided into smaller units and these units are processed by filters. Pipes are stateless and transport the data from filter to filter in such a way that each filter autonomously determines when it needs the next element (input) of the data stream from the preceding filter. The difference between Pipes&Filters and Batch/Sequential is not evident in a UML representation. 55

Data-flow style: Advantages and Disadvantages The main advantage of data-flow is the low complexity of interactions between components. The processing modules are black boxes. The data-flow-oriented architecture style is unsuitable for modeling interactive applications. A further disadvantage is the frequently insufficient performance and efficiency. If filters need the entire input stream as context, appropriate buffers must be used. That affects the memory efficiency negatively. The data-flow style is well suited for modeling physical systems. It is used for example in the tool Simulink (from MathWorks). 56