Download presentation
Presentation is loading. Please wait.
1
Week 7 Recap CSE 115 Spring 2007
2
Not a Holder, but a Proxy be We actually wanted to build a Proxy for our program What is the difference? Holder is a “bucket” simply holding onto a value. Proxy implements the same interface as the object being used and can react to the same method calls.
3
Talking to Triangles Wanted the same buttons to talk to triangles as well as bouncing balls. We can change the proxy’s instance variable to have type IBouncer (the interface type) and then we can assign to that variable anything that implements that interface.
4
Declared Type A variable actually has two types. The first is the declared type, the type given when the variable is first declared. VarType varname;
5
Actual Type The second is the actual type, the type of the object assigned to the reference. vartype = new OtherType();
6
How can they be different? Much of the time, the declared type and actual type of a variable are actually the same (as they have been all semester so far). However, the declared type and actual type can be different only when the actual type is a subtype of the declared type.
7
Subtype? Yes, when a class implements an interface, it becomes a subtype of the interface. Therefore, we will usually see the following: Interface var = new ClassThatImplementsInterface();
8
What happens in the program? When the declared type of a variable differs from the actual type: The only methods that can be called are those of the declared type. The methods that get executed are those in the actual type.
9
Polymorphism This is polymorphism. The same “object” responds to the same message in different ways depending on its actual type.
10
More Patterns! Null Object Pattern Used when we want to express the idea of a “nothing” or “null” object in the system. A null object can be instantiated, but it does nothing if methods are called upon it.
11
Composite Pattern Creates a structure that allows clients to treat a single object or a group of objects in exactly the same way. In our example, the CompositeShape implements IBouncer and has two instance variables, each whose type is IBouncer.
12
Composite Pattern We can create a composite of three triangles and two bouncing balls by simple creating a CompositeShape and passing in as parameters an instance of TriangleTriple and an instance of BouncingBallPair.
13
Composite Pattern Claim: We can make a composite of two sets of TriangleTriples and one set of bouncing balls without making any changes to the CompositeShapes class How can this be done?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.