View
215
Download
0
Category
Preview:
Citation preview
Introduction (Continued)Design Patterns (I)
Lecture Two
What is a framework
• Like a class library, but more than an ordinary toolkit. The framework components have structural and behavioral relations among them.
• Domain specific:– business, telecom, window systems, databases,
OS kernels, …
What is a framework
• An integrated set of cooperating classes– architectural design of oo system– semi-complete application
• set of abstract class are specialized in application
Frameworks
• Inversion of control– main event loop is often in framework, rather
than in application code– hollywood principle: don’t call us, we’ll call
you– code in framework can invoke code in
application by dynamic binding
Framework examples
• UI– model-view-controller (MVC)
• framework for smalltalk user interfaces [1980]
– macapp - framework for Mac applications [1986]
• Others– MFC - Microsoft foundation classes– Choices - operating system framework– …..
Patterns vs Frameworks
• Patterns– reuse of design
– tells you how to do good design
– language independent
– often covers general domains
– addresses small part of a program architecture
Patterns vs Frameworks
• Framework– reuse of design and code
– is a design (generic implementation)
– language specific
– often covers specific domain
– addresses overall program architecture
State Pattern
• Butterfly Example– Caterpillar– Chrysalis– Adult
• Happy/Gloomy Example
State Pattern
• Book discussion– Applicability:
• state dependent behavior• large conditional statements
– Scheme: structure/participants/collaborations– Benefits
• extensibility• make state transitions explicit• can be shared (flyweight)
State Pattern
• Implementation– Who defines state transitions?– How about table-driven– Manage lifetime of state objects
• relation with Singleton
– How about use dynamic inhertiance
Singleton Pattern
• What is it?
• How to implement?
Homework: Digital Watch
• Demo
• Code explained
Composite Pattern
• Objective: treat primitives and containers uniformly
• java.awt.*
• Example Basket
Composite Pattern
• Consequences– recursive composition– uniform interface– easier to add new components– can make design overly general
Implementation Issues• Explicit parent references• Sharing components• Maximizing the Component interface• Child management operations• Who implements a list of components• Child ordering• Who should delete components• Best structures for storing components
Recommended