CS 635 Advanced Object-Oriented Design & Programming
Spring Semester, 2004
Flyweight & Facade
© 2004, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 30-Mar-04
Sharable & Flyweight
Each country, like India or China, has a lot of information
A Nation object for one country may have a lot of data
A program may only have a few references to an India object
Having all references share the same object
Symbol & Interned Strings
Only one instance a symbol with a give character sequence in an image
| a b | a := #cat. b := (‘ca’ , ‘t’) asSymbol. a = b “True” a == b “True – a & b point to same location”
Use objects to represent individual characters of the alphabet
Using objects allows the program to treat characters like any other part of the document - as an object
A character, like “G”, may require a fair amount of information:
The pattern can be used when all the following are true
MyClass* objectPtrA; MyClass* objectPtrB; if ( objectPtrA == objectPtrB ) //testing object identity
Separating state from the flyweight
This is the hard part
Must remove the extrinsic state from the object
Store the extrinsic state elsewhere
The VisualWorks Smalltalk compiler system has 75 classes
Programmers only use Compiler, which uses the other classes
Compiler evaluate: '100 factorial'
| method compiler | method := 'reset "Resets the counter to zero" count := 0.'.
compiler := Compiler new. compiler parse:method in: Counter notifying: nil
Distributed Object Systems
Subsystems are groups of classes, or groups of classes and other subsystems, that collaborate among themselves to support a set of contracts
There is no conceptual difference between the responsibilities of a class and a subsystem of classes
The difference between a class and subsystem of classes is a matter of scale
A subsystem should be a good abstraction
There should be as little communication between different subsystems as possible
The Facade Pattern - Basic Idea
Create a class that is the interface to the subsystem
Clients interface with the Facade class to deal with the subsystem
Consequences of Facade Pattern
It hides the implementation of the subsystem from clients
It promotes weak coupling between the subsystems and its clients
It does not prevent clients from using subsystem classes directly, should it?
Facade does not add new functionality to the subsystem
Copyright ©, All rights reserved.
2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.
Previous    visitors since 30-Mar-04    Next