Stéphane Ducasse«ChapterNr».1 Selected Design Patterns Design Patterns are recurrent solutions to design problems They are pros and cons We already saw:

Slides:



Advertisements
Similar presentations
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
Advertisements

Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997.
March Ron McFadyen1 Composite Used to compose objects into tree structures to represent part-whole hierarchies Composite lets clients treat.
Fall 2009ACS-3913 Ron McFadyen Composite Pattern Problem: How do we treat a composition structure of objects the same way as a non-composite object? Arises.
Stéphane Ducasse«ChapterNr».1 Arthur Riel Design Heuristics from Object-Oriented Design Heuristics by Arthur Riel Read the Heuristics… –Find reasons why.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Some Basic Points on Classes
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Chapter 10 Classes Continued
Stéphane Ducasse«ChapterNr».1 Advanced Classes Indexed Classes Classes as Objects Class Instance Variables and Methods Class Variables Pool Dictionaries.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design Patterns.
S.Ducasse Stéphane Ducasse 1 Selected Design Patterns.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns 1.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
 How are you going to collaborate?  How are you going to divide up work?  How are you going to make sure that changes work with other people’s code?
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
CS 4233 Review Feb February Review2 Outline  Previous Business – My.wpi.edu contains all grades to date for course – Review and contact.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Stéphane Ducasse 1 Singleton.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
S.Ducasse 1 7. Understanding Metaclasses. S.Ducasse 2 Roadmap Metaclasses in 7 points Indexed Classes Class Instance Variables Class Variables Pool Dictionaries.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.
Design Patterns Introduction
Patterns Composite Pattern. Patterns All designers use patterns. Patterns in solutions come from patterns in problems. "A pattern is a solution to a problem.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Stéphane Ducasse 1 Some Advanced Points on Classes.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Module 9. Dealing with Generalization Course: Refactoring.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
S.Ducasse Stéphane Ducasse 1 Some Advanced Points on Classes.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
S.Ducasse 1 Instance Initialization How to ensure that an instance is well initialized? Automatic initialize Lazy initialization Proposing the right interface.
Design Patterns: MORE Examples
Factory Patterns 1.
More Design Patterns 1.
More Design Patterns 1.
Jim Fawcett CSE776 – Design Patterns Summer 2003
Advanced Java Programming
Composite Design Pattern By Aravind Reddy Patlola.
Software Design Lecture : 36.
Presentation transcript:

Stéphane Ducasse«ChapterNr».1 Selected Design Patterns Design Patterns are recurrent solutions to design problems They are pros and cons We already saw: –Factory, Hook, Templates Singleton Composite

Stéphane Ducasse«ChapterNr».2 Alert!!! Design Patterns are invading… Design Patterns may be a real plague! Do not apply them when you do not need them Applying too much or badly design patterns makes software rot Design Patterns make the software more complex –More classes –More indirections, more messages Try to understand when NOT applying them!

Stéphane Ducasse«ChapterNr».3 The Singleton Pattern Intent: Ensure that a class has only one instance, and provide a global point of access to it Problem: We want a class with a unique instance. Solution: We specialize the #new class method so that if one instance already exists this will be the only one. When the first instance is created, we store and return it as result of #new.

Stéphane Ducasse«ChapterNr».4 Possible Design

Stéphane Ducasse«ChapterNr».5 The Singleton Pattern |aLan| aLan := NetworkManager new aLan == LAN new -> true aLan uniqueInstance == NetworkManager new -> true NetWorkManager class instanceVariableNames: 'uniqueInstance ' NetworkManager class>>new self error: ‘should use uniqueInstance’ NetworkManager class>>uniqueInstance uniqueInstance isNil ifTrue: [ uniqueInstance := self basicNew initialize]. ^uniqueInstance

Stéphane Ducasse«ChapterNr».6 The Singleton Pattern Providing access to the unique instance is not always necesssary. It depends on what we want to express. The difference between #new and #uniqueInstance is that #new potentially initializes a new instance, while #uniqueInstance only returns the unique instance (there is no initialization) Do we want to communicate that the class has a singleton?

Stéphane Ducasse«ChapterNr».7 Implementation Issues Singletons may be accessed via a global variable (ex: NotificationManager uniqueInstance notifier). SessionModel>>startupWindowSystem “Private - Perform OS window system startup” Notifier initializeWindowHandles.... oldWindows := Notifier windows. Notifier initialize.... ^oldWindows Global Variable or Class Method Access –Global Variable Access is dangerous: if we reassign Notifier we lose all references to the current window. –Class Method Access is better because it provides a single access point. This class is responsible for the singleton instance (creation, initialization,...).

Stéphane Ducasse«ChapterNr».8 Implementation Issues Persistent Singleton: only one instance exists and its identity does not change (ex: NotifierManager in Visual Smalltalk) Transient Singleton: only one instance exists at any time, but that instance changes (ex: SessionModel in Visual Smalltalk, SourceFileManager, Screen in VisualWorks) Single Active Instance Singleton: a single instance is active at any point in time, but other dormant instances may also exist. Project in VisualWorks, ControllerManager.

Stéphane Ducasse«ChapterNr».9 Implementation Issues classVariable or class instance variable classVariable –One singleton for a complete hierarchy Class instance variable –One singleton per class

Stéphane Ducasse«ChapterNr».10 Access? In Smalltalk we cannot prevent a client to send a message (protected in C++). To prevent additional creation we can redefine new/new: Object subclass: #Singleton instanceVariableNames: ‘uniqueInstance’ classVariableNames: ‘’ poolDictionaries: ‘’ Singleton class>>new self error: ‘Class ‘, self name, ‘ cannot create new instances’

Stéphane Ducasse«ChapterNr».11 Access using new: not so good idea Singleton class>>new ^self uniqueInstance The intent (uniqueness) is not clear anymore! New is normally used to return newly created instances. The programmer does not expect this: |screen1 screen2| screen1 := Screen new. screen2 := Screen uniqueInstance

Stéphane Ducasse«ChapterNr».12 Favor Class Behavior When a class should only have one instance, it could be tempting to define all its behavior at the class level. But this is not good: Class behavior represents behavior of classes: “Ordinary objects are used to model the real world. MetaObjects describe these ordinary objects” Do not mess up this separation and do not mix domain objects with metaconcerns. What’s happens if later on an object can have multiple instances? You would have to change a lot of client code!

Stéphane Ducasse«ChapterNr».13 The Composite Pattern A Case study: Queries. We want to be able to Specify different queries over a repository q1 := PropertyQuery property: #HNL with: #< value: 4. q2 := PropertyQuery property: #NOM with: #> value: 10. q3 := MatchName match: ‘*figure*’ Compose these queries and treat composite queries as one query (e1 e2 e3 e4... en)((q1 and q2 and q4) or q3) -> (e2 e5) composer := AndComposeQuery with: (Array with: q1 with: q2 with: q3)

Stéphane Ducasse«ChapterNr».14 A Possible Solution

Stéphane Ducasse«ChapterNr».15 Composite Intent: Compose objects into tree structure to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly

Stéphane Ducasse«ChapterNr».16 In Smalltalk Composite not only groups leaves but can also contain composites In Smalltalk add:, remove: do not need to be declared into Component but only on Composite. This way we avoid to have to define dummy behavior for Leaf

Stéphane Ducasse«ChapterNr».17 Composite Variations Use a Component superclass (To define the interface and factor code there) Consider implementing abstract Composite and Leaf (in case of complex hierarchy) Only Composite delegates to children Composites can be nested Composite sets the parent back-pointer (add:/remove:)

Stéphane Ducasse«ChapterNr».18 Composite Variations Can Composite contain any type of child? (domain issues) Is the Composite’s number of children limited? Forward –Simple forward. Send the message to all the children and merge the results without performing any other behavior –Selective forward. Conditionally forward to some children –Extended forward. Extra behavior –Override. Instead of delegating