Software Studio: Abstract Data Types
Software Studio: Abstract Data Types
Software Studio: Abstract Data Types
studio
abstract data types
Daniel Jackson
plugins
can replace ADT with different implementation
eg, can stub network interface
rep independence
can change rep without changing client
choice of rep is an encapsulated design secret
2
in functional language
can hide the rep using closures
an abstract type
var Color = function (r, g, b) {
color.toCSS = function () {
return color;
document.body.style.backgroundColor = c.toCSS();
};
how is
rep
hidden?
Object
> show(blue)
undefined
using this
this.toCSS = function () {
Color
> show(blue)
undefined
prototype method
return Math.sqrt(
sq(c.red() - this.red()) +
sq(c.green() - this.green()) +
sq(c.blue() - this.blue()));
Color
> red.distance(green);
360.62445840513925
a mutable type
name_to_color[name] = color;
};
return name_to_color[name];
};
if(name_to_color.hasOwnProperty(name)) {
var c = name_to_color[name];
distance = c.distance(color);
shortest_distance = distance;
best_match = name;
};
};
};
return best_match;
};
var lego_colors = [
...]
lego_colors.each(function (nc) {
});
Color
"rgb(100,50,150)"
undefined
> n
"Bright violet"
"rgb(107,50,123)"
this.checkRep = function () {
...
Color
> red.checkRep()
true
Color
> red.checkRep()
false
name_to_color[name] = color;
};
...
undefined
10
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.