Design Patterns in OCaml - People.csail.mit.edu

OCaml sacrifices some expressiveness in exchange of type safety. There are .... In the composite pattern, the interface to a tree data structure is simplified by providing the same interface for ... (FLog(Date).t -> (Date.t * float) list) -> t -> string list.
125KB Sizes 4 Downloads 97 Views
Design Patterns in OCaml Antonio Vicente [email protected] Earl Wagner [email protected]

Abstract The GOF Design Patterns book is an important piece of any professional programmer’s library. These patterns are generally considered to be an indication of good design and development practices. By giving an implementation of these patterns in OCaml we expected to better understand the importance of OCaml’s advanced language features and provide other developers with an implementation of these familiar concepts in order to reduce the effort required to learn this language. As in the case of Smalltalk and Scheme+GLOS, OCaml’s higher order features allows for simple elegant implementation of some of the patterns while others were much harder due to the OCaml’s restrictive type system.

1

Contents 1 Background and Motivation

3

2 Results and Evaluation

3

3 Lessons Learned and Conclusions

4

4 Creational Patterns 4.1 Abstract Factory 4.2 Builder . . . . . 4.3 Factory Method . 4.4 Prototype . . . . 4.5 Singleton . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

5 5 6 6 7 8

5 Structural Patterns 5.1 Adapter . . . . . 5.2 Bridge . . . . . . 5.3 Composite . . . . 5.4 Decorator . . . . 5.5 Facade . . . . . . 5.6 Flyweight . . . . 5.7 Proxy . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

8 8 8 8 9 10 10 10

6 Behavior Patterns 6.1 Chain of Responsibility 6.2 Command . . . . . . . . 6.3 Interpreter . . . . . . . . 6.4 Iterator . . . . . . . . . 6.5 Mediator . . . . . . . . . 6.6 Memento . . . . . . . . 6.7 Observer . . . . . . . . . 6.8 State . . . . . . . . . . . 6.9 Strategy . . . . . . . . . 6.10 Template Method . . . . 6.11 Visitor . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . .