Extensibility Patterns: Extension Access

Similar documents
Modelling with cellular automata

Techniques of Java Programming

SDS developer guide. Develop distributed and parallel applications in Java. Nathanaël Cottin. version

INF Models of concurrency

Design Patterns part I. Design Patterns part I 1/32

A JML Specification of the Design Pattern Visitor

OBEUS. (Object-Based Environment for Urban Simulation) Shareware Version. Itzhak Benenson 1,2, Slava Birfur 1, Vlad Kharbash 1

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

Announcements. John Jannotti (cs32) Design Patterns Feb 13, / 1

1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal

Object Modeling Approach! Object Modeling Approach!

Formal Definition of Computation. August 28, 2013

Blocking Synchronization: Streams Vijay Saraswat (Dec 10, 2012)

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 07/2014 SESSION 8:00-10:00

Note that numerically, with white corresponding to 0 and black to 1, the rule can be written:

Compiling Techniques

Grids of Finite Automata

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 09/06/2014 SESSION 8:30-10:30

Introduction to Scientific Modeling CS 365, Fall 2011 Cellular Automata

Softwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany

Java Programming. Final Examination on December 13, 2015 Fall 2015

Course Announcements. Bacon is due next Monday. Next lab is about drawing UIs. Today s lecture will help thinking about your DB interface.

Cellular Automata. History. 1-Dimensional CA. 1-Dimensional CA. Ozalp Babaoglu

Softwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany

Cellular Automata. and beyond. The World of Simple Programs. Christian Jacob

Introduction to functions

15-251: Great Theoretical Ideas in Computer Science Lecture 7. Turing s Legacy Continues

Junior Villafana. Math 301. Dr. Meredith. Odd Perfect Numbers

public void run ( ) { i f ( this. id == 0) System. out. p r i n t ( 3 ) ; bro. j o i n ( ) ; else System. out. p r i n t ( 2 ) ;

Factory method - Increasing the reusability at the cost of understandability

Object Oriented Software Design (NTU, Class Even, Spring 2009) Final Examination Problem Sheet TIME: 06/16/2009, 14:20 17:20

Design Patterns and Refactoring

Elementary Cellular Automata with

Cellular Automata. ,C ) (t ) ,..., C i +[ K / 2] Cellular Automata. x > N : C x ! N. = C x. x < 1: C x. = C N+ x.

Google Go illustrated on the basis of Fibonacci numbers

CS 6110 Lecture 28 Subtype Polymorphism 3 April 2013 Lecturer: Andrew Myers

ITI Introduction to Computing II

Theory of Computation

More on Methods and Encapsulation

We prove that the creator is infinite Turing machine or infinite Cellular-automaton.

BoardGame: A MiniDraw extension

DM507 - Algoritmer og Datastrukturer Project 1

An object-oriented design process. Weather system description. Layered architecture. Process stages. System context and models of use

P(t w) = arg maxp(t, w) (5.1) P(t,w) = P(t)P(w t). (5.2) The first term, P(t), can be described using a language model, for example, a bigram model:

Agile modeling for INF5150

Pattern-Oriented Analysis and Design (POAD) Theory

COMPUTER SCIENCE TRIPOS

Android Services. Lecture 4. Operating Systems Practical. 26 October 2016

FIT100 Spring 01. Project 2. Astrological Toys

11. Automata and languages, cellular automata, grammars, L-systems

Descriptive Statistics (And a little bit on rounding and significant digits)

Comp 11 Lectures. Mike Shah. July 12, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 12, / 33

Show that the following problems are NP-complete

Prioritized Garbage Collection Using the Garbage Collector to Support Caching

Administrivia. Course Objectives. Overview. Lecture Notes Week markem/cs333/ 2. Staff. 3. Prerequisites. 4. Grading. 1. Theory and application

Characterization of Fixed Points in Sequential Dynamical Systems

Outline Resource Introduction Usage Testing Exercises. Linked Lists COMP SCI / SFWR ENG 2S03. Department of Computing and Software McMaster University

Logic: Bottom-up & Top-down proof procedures

MURA Office hours Thurs at 12-1pm in CIT 546 Contact for more info.

Training Path FNT IT Infrastruktur Management

Research Report 326 ISBN ISSN

Notes on Pumping Lemma

MTAT Software Engineering

Workshop 1a: Software Measurement. Dietmar Pfahl

Chain of Responsibility

Turing s Legacy Continues

2 - Strings and Binomial Coefficients

UML. Design Principles.

Undecidability COMS Ashley Montanaro 4 April Department of Computer Science, University of Bristol Bristol, UK

Implementing Per Bak s Sand Pile Model as a Two-Dimensional Cellular Automaton Leigh Tesfatsion 21 January 2009 Econ 308. Presentation Outline

Unit 8: Sequ. ential Circuits

S.R.S Varadhan by Professor Tom Louis Lindstrøm

Theory of Computation Prof. Kamala Krithivasan Department of Computer Science and Engineering Indian Institute Of Technology, Madras

Maps performance tips On server: Maintain DB connections, prepared statements (per thread/request!)

A Cellular Automata Approach to Population Modeling

Lecture 5: Sep. 23 &25

Software Engineering

Real Time Operating Systems

Automata Theory (2A) Young Won Lim 5/31/18

THE AUSTRALIAN NATIONAL UNIVERSITY Second Semester COMP2600 (Formal Methods for Software Engineering)

Introduction to Artificial Life and Cellular Automata. Cellular Automata

Exercise 1 (15 points)

CISC 4090: Theory of Computation Chapter 1 Regular Languages. Section 1.1: Finite Automata. What is a computer? Finite automata

8. TRANSFORMING TOOL #1 (the Addition Property of Equality)

Report of Dragable Notebook

ST-Links. SpatialKit. Version 3.0.x. For ArcMap. ArcMap Extension for Directly Connecting to Spatial Databases. ST-Links Corporation.

Properties of Arithmetic

ITI Introduction to Computing II

BINARY MORPHOLOGY AND CELLULAR AUTOMATA

Binary Decision Diagrams and Symbolic Model Checking

Distributed Systems Part II Solution to Exercise Sheet 10

Formal Languages. We ll use the English language as a running example.

Python. Tutorial. Jan Pöschko. March 22, Graz University of Technology

Compiling Techniques

COMPUTER SCIENCE TRIPOS

Natural Language Processing Prof. Pushpak Bhattacharyya Department of Computer Science & Engineering, Indian Institute of Technology, Bombay

ECEN 651: Microprogrammed Control of Digital Systems Department of Electrical and Computer Engineering Texas A&M University

Formal Languages. We ll use the English language as a running example.

How Spatial Layout and Architecture Influence Everyday Work of Theoretical Physicists The Example of the mpipks

CSE505, Fall 2012, Final Examination December 10, 2012

Transcription:

Design Patterns and Frameworks Dipl.-Medieninf. Christian Piechnick INF 2080 christian.piechnick@tu-dresden.de Exercise Sheet No. 5 Software Technology Group Institute for SMT Department of Computer Science Technische Universität Dresden 01062 Dresden Extensibility Patterns: Extension Access Task 1: Lively Iteration Cellular automata [1] are mathematical machines consisting of a grid of individual so-called cells, each of which can be in one of k states (typically called colours ). A cellular automaton evolves in so-called generations: The state of all cells is updated simultaneously following rules based on the colours of the cells in a certain neighbourhood of the currently evolving cell. Different types of cellular automata exist; they vary in the shape and size of the grid, the number of possible colours per cell, the size and shape of a cell s neighbourhood, and the evolution rules. 1a) Task: Design and implement a class CellGrid that represents a grid of cells of a cellular automaton. Provide an interface for access to the individual cells, but hide the structure of the grid (number of dimensions, structure and size of neighbourhood, etc.) from clients. What design pattern can you use? How do you use it? Solution: We use Iterator to allow clients to access each cell in the grid without having to disclose anything about the structure of the grid. Everything the client needs to know about an individual cell is made available through the iterator s interface: public interface C e l l I t e r a t o r { Move t h e i t e r a t o r to t h e next c e l l, i f any. @return true, i f t h e r e s t i l l i s a c e l l to be considered, f a l s e i f a l l c e l l s have been t r e a t e d. public boolean next ( ) ; @return t h e c o l o u r o f t h e c u r r e n t c e l l encoded as a non n e g a t i v e number public int getcolour ( ) ; @return t h e number o f n e i g h b o u r s o f a c e r t a i n c o l o u r o f t h e c u r r e n t c e l l public int getneighbours ( int withcolour ) ; Determine t h e c o l o u r o f t h e c u r r e n t c e l l in t h e next g e n e r a t i o n o f t h e automaton public void setcolourinnextgeneration ( int f u t u r e C o l o u r ) ; 1

CellGrid then provides access to individual cells only through an instance of CellIterator: A g r i d o f c e l l s in a c e l l u l a r automaton. This g r i d can be o f any s i z e and shape. A c e l l g r i d s t o r e s two g e n e r a t i o n s o f a grid, namely t h e c u r r e n t g e n e r a t i o n and an image o f t h e next g e n e r a t i o n. {@see #nextgeneration ( ) can be invoked to make t h e f u t u r e g e n e r a t i o n t h e c u r r e n t one. public interface C e l l G r i d { @return an i t e r a t o r o f a l l c e l l s in t h e c u r r e n t g e n e r a t i o n o f t h i s g r i d. public C e l l I t e r a t o r a l l C e l l s ( ) ; Switch to t h e next g e n e r a t i o n. public void nextgeneration ( ) ; 1b) Task: Using the CellGrid class from above, design and implement a class GridChanger that realises Conway s Game of Life [2]. Life is played on a two-dimensional grid, with cells that are either black ( dead ) or white ( alive ). The neighbourhood of a cell consists of the 8 cells directly adjacent to it. For each cell, the colour of the cell in the next generation depends on the cell s current colour and on the number of living (i.e., white) cells in its neighbourhood: The cell dies (i.e., turns black in the next generation) if it is alive and there are less than 2 or more than 3 living cells in the neighbourhood. The cell survives (i.e., stays white in the next generation) if it is currently alive and there are 2 or 3 living cells in the neighbourhood. The cell is born (i.e., turns white in the next generation) if it is currently dead and there are exactly 3 living cells in its neighbourhood. The cell stays dead (i.e., stays black in the next generation) if it is dead and there are more or less than 3 living cells in its neighbourhood. Solution: public class GridChanger implements Runnable { private C e l l G r i d c e l l G r i d ; public GridChanger ( C e l l G r i d c e l l G r i d ) { super ( ) ; this. c e l l G r i d = c e l l G r i d ; @Override public void run ( ) { while ( true ) { try { Thread. s l e e p ( 5 0 0 ) ; catch ( I n t e r r u p t e d E x c e p t i o n e ) { s t e p ( ) ; public void s t e p ( ) { for ( C e l l I t e r a t o r c i = c e l l G r i d. a l l C e l l s ( ) ; c i. next ( ) ; ) { i f ( c i. getcolour ( ) == 1) { switch ( c i. getneighbours ( 1 ) ) { case 0 : case 1 : 2

case 4 : case 5 : case 6 : case 7 : case 8 : c i. setcolourinnextgeneration ( 0 ) ; break ; default : c i. setcolourinnextgeneration ( 1 ) ; else { c i. setcolourinnextgeneration ( ( c i. getneighbours ( 1 ) == 3)? 1 : 0 ) ; c e l l G r i d. nextgeneration ( ) ; 1c) Task: Use additional patterns (Strategy, Observer, Interpreter,... ) to implement a completely generic cellular automaton grid. Parametrize size and structure of the grid, size and structure of a cell s neighbourhood, number of colours, and the rules for determining a cell s colour in the next generation. This task is of added complexity. We may not discuss it in the exercise, but I will be happy to comment on any solution of yours that you send me. Solution: Unfortunately, solution hint is not available. Bibliography 1. Eric W. Weisstein. Cellular Automaton. From MathWorld A Wolfram Web Resource. http://mathworld.wolfram.com/cellularautomaton.html 2. Eric W. Weisstein. Life. From MathWorld A Wolfram Web Resource. http://mathworld.wolfram.com/gameoflife.html Task 2: Extensible Insurance Contracts Insurance contracts are very long-lived documents that are treated by many different people for many different reasons during their life-time. In a software system for the management of insurance contracts, each of these clients needs a custom interface to the insurance-contract object. Sometimes the same person needs to treat very different types of insurance contracts in the same manner. Occasionally, new types of treatment need to be added dynamically, without affecting any of the pre-existing code. 2a) Task: Understand the design pattern Extension Object [1]. What are its elements? How do they collaborate to support solving the above problems? Solution: Unfortunately, solution hint is not available. 2b) Task: Use the Extension Object pattern to design and implement an insurance contract management system. Support the following roles: Initialization: The contract object has just been created and needs to be filled with the correct data. 3

Conclusion: The contract has been accepted and needs to be signed by all parties. Termination: The contract s duration has passed, all the money goes to the company :-) Solution: however. The following code provides the basic structure, implementing only the initialization role, / InsuranceContract. ja v a H i s t o r y 28.10.2005 Z s c h a l e r Created. package de. tudresden. i n f. wwwst. e x t e n s i o n o b j e c t ; import java. u t i l. Map ; import java. u t i l. TreeMap ; An insurance c o n t r a c t. @author Z s c h a l e r @since 28. 10. 2005 public class I nsurancecontract { Final v a l u e o f t h e c o n t r a c t. private double m dfinalvalue ; Monthly payments. private double m dmonthlydue ; Duration o f t h e c o n t r a c t in years. private long m lduration ; Name o f t h e person f o r whom t h e c o n t r a c t has been i s s u e d. private S t r i n g m sowner ; Create a new empty c o n t r a c t. public I nsurancecontract ( ) { super ( ) ; class I n i t R o l e extends ICRoleAdapter implements I C I n i t R o l e { public void setparameters ( S t r i n g sowner, double dfinalvalue, long l D u r a t i o n ) { m sowner = sowner ; m dfinalvalue = dfinalvalue ; m lduration = l D u r a t i o n ; // C a l c u l a t e monthly payments and don t f o r g e t t h e revenue! m dmonthlydue = ( dfinalvalue + 1 0 0 0 0. 0 0 ) / ( l D u r a t i o n 1 2 ) ; public double getmonthlydue ( ) { return m dmonthlydue ; ; // Set up t h e r o l e s o f t h i s c o n t r a c t addrole ( INITIALIZE, new I n i t R o l e ( ) ) ; 4

Tag i n t e r f a c e common to a l l r o l e s an insurance c o n t r a c t may p l a y. @author Z s c h a l e r @since 28. 10. 2005 public interface ICRole { @return t h e c o n t r a c t whose r o l e t h i s i s. public I nsurancecontract getcontract ( ) ; A r o l e f o r i n i t i a l i z i n g a c o n t r a c t. @author Z s c h a l e r @since 28. 10. 2005 public interface I C I n i t R o l e extends ICRole { Set t h e c o n t r a c t s parameters. public void setparameters ( S t r i n g sowner, double dfinalvalue, long l D u r a t i o n ) ; Get t h e monthly payment o f t h i s c o n t r a c t as computed by { @link #setparameters ( String, double, l o n g ). public double getmonthlydue ( ) ; Helper c l a s s. @author Z s c h a l e r @since 28. 10. 2005 public abstract class ICRoleAdapter implements ICRole { public I nsurancecontract getcontract ( ) { return InsuranceContract. this ; Exception thrown when a r e q u e s t e d r o l e i s not a v a i l a b l e. @author Z s c h a l e r @since 28. 10. 2005 public s t a t i c class UnsupportedRoleException extends Exception { public UnsupportedRoleException ( S t r i n g srolename ) { super ( Unsupported r o l e : < + srolename + > ) ; The s e t o f r o l e s supported by t h i s c o n t r a c t. private Map<String, ICRole> m mproles = new TreeMap<String, ICRole> ( ) ; Get a r o l e with a c e r t a i n name. public ICRole g e t I n t e r f a c e ( S t r i n g r o l e ) throws UnsupportedRoleException { ICRole r e s u l t = m mproles. get ( r o l e ) ; i f ( r e s u l t == null ) { 5

throw new UnsupportedRoleException ( r o l e ) ; return r e s u l t ; R e g i s t e r a r o l e with a c e r t a i n name. protected void addrole ( S t r i n g srolename, ICRole r o l e ) { m mproles. put ( srolename, r o l e ) ; 2c) Task: What do you have to do to support another role Incident (i.e., the incident against which the insurance is held, has occurred)? Solution: All that s required is a new implementation of ICRole with the corresponding functionality. As soon as this has been registered with the insurance-contract object, the new role can be used. 2d) Task: How can different document types (for example, insurance contracts, but also letters, etc.) support the same role? Solution: By providing an interface IExtensible the only purpose of which it is to provide access to extensions through a getrole object. If roles are given globally unique identifiers, any object that implements this interface can then be used in the same way, independent of its class. This even permits virtual dynamic class changes. An implementation of this is in Microsoft s most basic COM interface IUnknown. This interface provides two features: reference counting and access to other interfaces of a COM object using globally unique interface identifiers. Bibliography 1. Gamma, E. 1997. Extension object. In Pattern Languages of Program Design 3, R. C. Martin, D. Riehle, and F. Buschmann, Eds. Addison-Wesley Software Pattern Series. Addison-Wesley Longman Publishing Co., Boston, MA, 79 88. Also at http://st.inf.tu-dresden.de/lehre/ws06-07/dpf/gamma96.pdf A nice tutorial also exists at http://www.design-nation.net/en/archives/000488.php Task 3: Discussion of Patterns 3a) Task: Enumerate 4 different types of Proxies, and tell what they do. Solution: 1. A remote proxy provides a local representative for an object in a different address space. 2. A virtual proxy creates expensive objects on demand. 3. A protection proxy controls access to the original object. Protection proxies are useful when object should have different access rights. 4. A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed. Typical uses include: counting the number of references to the real object for garbage collection checking for locking 6

This list has been taken directly from the GOF book. 3b) Task: Compare Adapter and Bridge. Enumerate commonalities and differences. Solution: Adapter and Bridge have a few things in common: Both enhance flexibility by introducing an additional indirection into the access to an object. Both hand on commands from an interface that the object originally did not implement. The key difference between the two patterns can be found in their intents. Adapter focuses on resolving incompatibilities between two existing interfaces. It does not care for the implementation of these interfaces, nor does it care about the possible development of class hierarchies. Adapter couples two classes developed independently. Their cooperation has originally not been planed or predicted. Users of Bridge, however, know from the beginning that there will be multiple implementations for the same abstraction. 3c) Task: Compare Bridge and TemplateMethod. Enumerate commonalities and differences. Solution: Template Method and Bridge both declare methods in abstract superclasses and implement them in subclasses. The key difference is that in the abstract class of Template Method, one method is implemented. This breaks the complete separation of abstraction and implementation intended by Bridge. Abstraction and implementation can no longer be refined independently. Template Method uses operations from the same object only. Bridge uses operations from another object. This can lead to runtime errors in weakly typed languages (e.g., SmallTalk). 3d) Task: Enumerate the cases in which Visitor can be employed. Characterize the advantages of the pattern. Solution: Visitor can be used to advantage, when an object structure with many classes must be traversed, and class specific operations are to be executed on each object. Visitor helps group into one class operations that belong together semantically, even though they work on different classes. This helps avoid cluttering data classes with lots of operations for different use cases. All operations of the same use case are encapsulated in one Visitor. Additionally, Visitor is perfect when the data structure changes rarely, but new operations are added frequently. Visitor is often used together with Composite. 3e) Task: Compare TemplateMethod and Strategy. What are commonalities, what are differences? Solution: In Template Method, the algorithm is fix, but some parts are variable. On the other hand, in Strategy, the algorithm is variable, and the client is fix. 7