Skip to main content

CSE 331: Design Patterns 1

lecture

Course motto: Your job is to deliver code that you know works.

In the guestbook with a book for each user, a handler passed the login from a request's address where the user who sent the request belonged. Both were a String, so javac accepted the call, and any user could delete from any book. The fix gave users a type of their own, User, whose constructor is private. The only way to get a User is to call User.from on a request. So we read one class, once, to know that every User came from a request, and javac checks at every compile that delete is handed a User. That fix is a design pattern, the static factory method. The lecture covers that pattern, then the Factory Method and the singleton, with a look at interning.

Definition: design pattern

A design pattern is a solution to a design problem that comes up again and again, written down so that others can reuse it.

It has four parts: a name, the problem it solves, the solution, and the consequences of using it, both what it gains and what it costs.1


  1. Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software (1995), section 1.1. ↩