Lecture 13
Recall:
encapsulated list class
can't traverse the list node-to-node
repeatedly calling ith => O(n2) time
SE Topic: Design Patterns
certain programming problems arise often
keep track of good solutions to these problems reuse and adapt them
If you have this problem, then this technique may solve it
Our problem: traversing a list without resulting in O(n2) time while keeping nodes secure
Solution: Iterator Pattern
Iterator Pattern
Create a class that manages access to nodes
Abstraction of a pointer
Walk the list without exposing the actual pointers
Iterating through an array in C-style
Want class that acts as pointer p
Now what does client's code look like?
Shortcut: automatic type deduction
Automatic Type Deduction
Applying the shortcut:
An even shorter shortcut:
Range Loops
available for any class with:
methods
begin
andend
that produce iteratorsthe iterator must support
!=
, prefix++
, unary*
Encapsulation continued
Encapsulation with Constructors
List client can create iterators directly by doing:
Violates encapsulation
client should be using end()
We could make Iterator's constructor private, but two problems arise:
Client cannot call
List::Iterator
List cannot call
Iterator
Problem: How do we allow client to create iterators without breaking encapsulation? Solution: Make List
a friend of Iterator
Friends
Gives class privileged access to another class
Solution explained:
Make List a friend
give List privileged access to Iterator by making List a friend
So now List can still create iterators, but client can only create iterators by calling begin/end
Rule of Thumb with Friends: Give your classes as few friends as possible - weakens encapsulation
Accessor/Mutator Methods
Provides access to private fields
Why not just put int x and y in public?
advantage to this is if you want x and y to have certain restrictions, but not complete restriction
Dealing with private fields for theoperator<<
?
operator<<
?operator<< needs to access x and y fields, but the operator function cannot be a member (needs to be outside the class)
We have two options in this case:
if
getX
,getY
defined, then use these methods to access x and yif you don't want to provide getX and getY - make
operator<<
a friend function:
System Modelling (UML)
Visualizing the structure of the system (abstractions and relationships among them) to aid design and implementation Popular Standard: UML (Unified Modelling Language)
Modeling Classes
Name
**Vec
Fields (optional)
-x: Integer, -y: integer
Methods (optional)
+getX: Integer, +getY: Integer
**Visibility: - means private, + means public, # means protected
Relationship: Composition of classes
Composition (OWNS-A)
Embedding an object inside another
Example:
Embedding Vec inside Plane is called composition.
Relationship shown in example:
A Plane OWNS-A Vec (Owns 2 of them)
If A OWNS-A B, then typically: B has no identity outside A (no independent existence)
If A is destroyed, B is destroyed
If A is copied, B is copied (deep copy)
eg. A car owns four wheels - a wheel is part of a car
destroy the car => destroy the wheels
copy the car => copy the wheels
Typical Implemention: typically as composition of classes
Modelling: Plane
UML
Last updated