Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract...
Transcript of Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract...
![Page 1: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/1.jpg)
Ownership in Design Patterns
Master's ThesisFinal Presentation
Stefan Nägeli07.02.06
![Page 2: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/2.jpg)
Overview
● Status Quo● Pattern Overview● Encountered Problems applying UTS● Pros and Cons compared to other systems● UTS Feature Requests● Conclusion● Remaining Work
![Page 3: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/3.jpg)
Status Quo
● Finished analyzing all patterns in the GoF Design Patterns book.
● Ownership in a Swing GUI application. Core design encapsulates 7 patterns.
● Pattern usage and ownership in the Swing Framework.
![Page 4: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/4.jpg)
Overview - Creational
UTS Ownership Types Ownership Domains
Abstract Factory x x x
Builder x x x
Factory Method x x x
Prototype x x x
Singleton x x x
x = applying ownership enhances pattern and works finex = ownership typing leads to problems or has few benefitsx = ownership typing is not possible or has no benefits
![Page 5: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/5.jpg)
Overview - StructuralUTS Ownership Types Ownership Domains
Adapter x x x
Bridge x x x
Composite x x x
Decorator x x x
Facade x x x
Flyweight x x x
Proxy x x x
x = applying ownership enhances pattern and works finex = ownership typing leads to problems or has few benefitsx = ownership typing is not possible or has no benefits
![Page 6: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/6.jpg)
Overview - BehavioralUTS Ownership Types Ownership Domains
Chain of Resp. x x x
Command x x x
Interpreter x x x
Iterator x x x
Mediator x x x
Memento x x x
Observer x x x
State x x x
Strategy x x x
Templ. Method
Visitor x x x
x = applying ownership enhances pattern and works finex = ownership typing leads to problems or has few benefitsx = ownership typing is not possible or has no benefits
![Page 7: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/7.jpg)
Implementation enhancements
Enhances implementation No enhancements Strongly depends
Abstract Factory Singleton Adapter
Builder Decorator Composite
Factory Method Observer Facade
Prototype Visitor Strategy
Bridge Command Interpreter
Flyweight Chain of Resp.
Iterator Template Method
Mediator
State
Memento
![Page 8: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/8.jpg)
Encountered Problems
● No ownership transfer● No concept of friends● No multiple contexts● Readonly reference leaking
using the Universe type system
![Page 9: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/9.jpg)
1. No ownership transferObject creation determines the ownership context for life-time.
During an object's life-time it may need to reside in different contexts, but only in one at a time.
● Delegating object creation.● The desired ownership structure is not in accordance
with the creation order.● Need to change context upon performing an operation
![Page 10: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/10.jpg)
Problem occurrences
● Abstract Factory● Factory Method● Prototype● Adapter● Composite● Visitor
![Page 11: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/11.jpg)
Example: Abstract Factory
Car
Tire
+createTire()Garage
createTire
Tire
transfer context
![Page 12: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/12.jpg)
Example: VisitorClient
Element
Element Element
Visitor
query
visit
acceptaccept
accept
Visitor
query
visit
transfer context
![Page 13: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/13.jpg)
2. No concept of friendsOnly peer objects may alias each other in a read/write manner and there are no exceptions.
Some objects reside in a single context for life-time but need to be referenced not only by peer but also by a carefully chosen group of other objects.
![Page 14: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/14.jpg)
2. No concept of friends (2)
● Tightly coupled objects in terms of usage, but not related in terms of ownership.
● No notion of a global context.● No possibility to share objects among “friends”.”.● No r/w reference to owner
![Page 15: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/15.jpg)
Problem occurrences
● Singleton● Builder● Observer● State● Strategy● Command● Chain of Responsibility● Composite
![Page 16: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/16.jpg)
Example: Singletonroot
global
SingletonB
A
![Page 17: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/17.jpg)
Example: Observer
-amount : DoubleAccount
-text : StringLabelAmount
AccountManager MainWindow
amountChanged
attach
Application
![Page 18: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/18.jpg)
3. No multiple contexts
● No explicit enforcement of architectural constraints.
All objects owned by the same owner are automatically located in the same context.
There is no way to declare multiple ownership contexts for the same owner.
![Page 19: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/19.jpg)
Problem occurrences
● Mediator
![Page 20: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/20.jpg)
Example: Mediator
21
Mediator
Colleague Colleague
![Page 21: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/21.jpg)
4. Readonly reference leaking
● Certain patterns would benefit from also preventing readonly references.
● x * rep -> readonlyx * rep_strict -> forbidden
Each object may at least have a readonly reference to every other object in the system.
Leaking of readonly references is possible.
![Page 22: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/22.jpg)
Problem occurrences
● Proxy● (Facade)● (Bridge)
![Page 23: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/23.jpg)
Example: Proxy
+askForSecret()+leak() : Secret
-secret : SecretGuard
+getSecret()Secret
Intruder
leak
leaking readonly reference
![Page 24: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/24.jpg)
Pros and Cons of the UTS
compared to other ownership systems
![Page 25: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/25.jpg)
Pros
● Simplicity and intuitive usage● Clear, well-defined concept● Notion of readonly references
![Page 26: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/26.jpg)
Cons
● No global accessible context● No support for declaring multiple contexts● No aliasing exceptions (e.g. links)● No read/write access to owner● No mechanism to prevent readonly aliasing
![Page 27: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/27.jpg)
Feature Requests for the UTS
● Ownership transfer● Global contexts● Possibility to declare objects as friends● Possibility to suppress readonly references● Possibility to declare multiple contexts
![Page 28: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/28.jpg)
Conclusion
● Ownership succeeds to enhance certain pattern implementations.
● Ownership still lacks the necessary flexibility to cope with many common application designs / scenarios.
● To support most SE best-practices it might get necessary to give up static type checking in favor of being able to type more designs.
![Page 29: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/29.jpg)
Remaining Work
● Finish review of ownership feasibility in the Swing demo application and framework.
● Correct and improve the report
![Page 30: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/30.jpg)
Swing Demo Application
● Simple accounting application● Core design encapsulates 7 patterns
- Composite- Command- Visitor- Observer- Singleton- Strategy- Decorator
● Domain: Journal, Booking, Account, Group
![Page 31: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/31.jpg)
Screenshot
![Page 32: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/32.jpg)
Questions?
Comments?
![Page 33: Ownership in Design Patternssct.ethz.ch/projects/student_docs/Stefan_Naegeli/... · Abstract Factory Singleton Adapter Builder Decorator Composite Factory Method Observer Facade Prototype](https://reader034.fdocuments.in/reader034/viewer/2022050214/5f6096d26bcc5c58832fc1e8/html5/thumbnails/33.jpg)
Domain Layer
Presentation Layer
Assets : Group
FlüssigeMittel : Group
Mobiliar : Account
Kassa : Account
MainWindow : JFrame
treeAssets : JTree tableJournal : JTable comboFrom : JComboBoxtextDate : JDatePicker
visitor : AccountCollectVisitor
booking : Booking
journal : Journal
date : GregorianCalender decorator : StructureitemDecorator decorator : StructureitemDecorator decorator : StructureitemDecorator
nameOnlyStrategy nameAmountStrategy
Application
Controller
1*