CSE 331: Software Design and Implementation, Autumn 2026
Your job is to deliver code that you know works.
Definition: design pattern
A design pattern is a reusable solution to a recurring design problem.
Creational
Structural
Behavioral
Wednesday today December 2
Gamma, Helm, Johnson, and Vlissides, Design Patterns (1995)
true?4System.out.println(Boolean.valueOf(true) == Boolean.valueOf(true));
5System.out.println(Integer.valueOf(100) == Integer.valueOf(100));
6System.out.println("hello" == new String("hello"));
7System.out.println("hello" == new String("hello").intern());$ java Sharing true true false true
Integer.valueOf and String.intern do itnew makes a new object even when an equal one existsInteger.valueOf promises only -128 to 127: compare with equals19public Message(String author, String owner, String text) {8String asker = "bob@example.com";
9String owner = "ada@example.com";
10System.out.println(new Message(asker, owner, "hi ada"));
11System.out.println(new Message(owner, asker, "hi ada"));$ java Signing bob@example.com signs ada@example.com's book: hi ada ada@example.com signs bob@example.com's book: hi ada
25public static class Builder {
32 public Builder author(String author) {
33 this.author = author;
34 return this;
35 }
51 public Message build() {
52 if (author == null || owner == null || text == null) {
53 throw new IllegalStateException("a part is not set");
54 }
55 return new Message(this);
56 }11System.out.println(new Message.Builder()
12 .author(asker).owner(owner).text("hi ada").build());
13System.out.println(new Message.Builder()
14 .author(owner).owner(asker).text("hi ada").build());$ java Signing bob@example.com signs ada@example.com's book: hi ada ada@example.com signs bob@example.com's book: hi ada ⋮
15System.out.println(new Message.Builder()
16 .author(asker).text("hi ada").build());$ java Signing ⋮ Exception in thread "main" java.lang.IllegalStateException: a part is not set ⋮
10System.out.println(new Message(asker, owner, "hi ada"));11System.out.println(new Message.Builder()
12 .author(asker).owner(owner).text("hi ada").build());javac checks in each calljavac checks that three Strings are passedjavac checks that each method gets a Stringbuild() checks, at run timeauthor(owner)Message.Builder is onebuild()build() can check themBloch, Effective Java, 3rd edition (2018).
empty() return an ArrayDeque?4public static HelpQueue empty() {
5 return new ListQueue();5 return new java.util.ArrayDeque<String>();$ javac HelpQueues.java HelpQueues.java:5: error: incompatible types: ArrayDeque<String> cannot be converted to HelpQueue ⋮ 1 error
ArrayDeque never says implements HelpQueueDequeQueue, an adapter6public class DequeQueue implements HelpQueue {
7 private ArrayDeque<String> deque =
8 new ArrayDeque<String>();
14 public void join(String netid) {
15 if (deque.contains(netid)) {
16 throw new IllegalArgumentException(netid);
17 }
18 deque.addLast(netid);
19 }ArrayDeque in a field, and forwards each call to itjavac accept it without the check?15 if (deque.contains(netid)) {
16 throw new IllegalArgumentException(netid);
17 }
18 deque.addLast(netid);$ javac DequeQueue.java $ java Twice [ada, ada]
join of a student throwsDequeQueue is onejavac checks only its types| Behavior | Interface | |
|---|---|---|
| adapter | same | changed |
| decorator | changed | same |
| proxy | same | same |
LoggingQueue, a decorator4public class LoggingQueue implements HelpQueue {
5 private HelpQueue inner;
7 public LoggingQueue(HelpQueue inner) {
8 this.inner = inner;
9 }
15 public void join(String netid) {
16 inner.join(netid);
17 System.out.println("log: join " + netid);
18 }HelpQueue in a field, and forwards each call to itHelpQueue, even a wrapped one10hour(new LoggingQueue(new ListQueue()));
11hour(new LoggingQueue(new FastQueue()));
12hour(new LoggingQueue(new LoggingQueue(new ListQueue())));$ java Wrapped log: join ada log: next ada ⋮ log: join ada log: join ada log: next ada log: next ada
LoggingQueue?4public class LoggingQueue implements HelpQueue {
5 private HelpQueue inner;
7 public LoggingQueue(HelpQueue inner) {
8 this.inner = inner;
9 }
15 public void join(String netid) {
16 inner.join(netid);
17 System.out.println("log: join " + netid);
18 }javac checks about a wrapperHelpQueue's four methods, with their typesinner has each method the wrapper calls on itHelpQueue's specificationLoggingQueue without inner.join(netid) compilesDequeQueue without its check compiled, and let ada in twiceLoggingQueue is one== to what it wraps2public class LoggingListQueue extends ListQueue {
3 @Override
4 public void join(String netid) {
5 super.join(netid);
6 System.out.println("log: join " + netid);
7 }
15}hour(new LoggingListQueue()) prints the same linesFastQueue? No: only a ListQueueOfficeQueue?2public class LoggingListQueue extends ListQueue {2public class LoggingOfficeQueue extends OfficeQueue {$ javac LoggingOfficeQueue.java LoggingOfficeQueue.java:2: error: OfficeQueue() has private access in OfficeQueue public class LoggingOfficeQueue extends OfficeQueue { ^ 1 error
LoggingListQueue |
LoggingQueue |
|
|---|---|---|
| Logs | a ListQueue |
any HelpQueue |
| Writes | join and next |
all four methods |
| Relies on | how ListQueue is written |
HelpQueue's specification |
| Chosen | when the class is written | when an object is wrapped |
Subtyping, "Is a Square a Rectangle?"
Bloch, Effective Java, 3rd edition (2018)
15for (Path file : files.sorted().toList()) {
16 String name = file.getFileName().toString();
17 String login = name.replaceFirst("[.]txt$", "");
18 books.put(login, new SavedBook(file));
19}SavedBook reads its whole file when it is made$ java Startup reading ada@example.com.txt reading bob@example.com.txt started [ada@example.com: hello, bob@example.com: hi ada] [ada@example.com: hello, bob@example.com: hi ada]
LazyBook stands in for a SavedBook6class LazyBook implements Book {
7 private final Path file;
8 private SavedBook real = null;
30 @Override
31 public List<String> messages() throws IOException {
32 return real().messages();
33 }
34}Book, now an interfacereal() reads the file once18 private SavedBook real() throws IOException {
19 if (real == null) {
20 real = new SavedBook(file);
21 }
22 return real;
23 }real() reads the file the first time it is calledBooks makes a LazyBook for each file18books.put(login, new SavedBook(file));18books.put(login, new LazyBook(file));$ java Startup started reading ada@example.com.txt [ada@example.com: hello, bob@example.com: hi ada] [ada@example.com: hello, bob@example.com: hi ada]
javac accept Book without throws?14List<String> messages() throws IOException;14List<String> messages();$ javac *.java LazyBook.java:31: error: messages() in LazyBook cannot implement messages() in Book ⋮ overridden method does not throw IOException 1 error
javac checks each method's types and exceptionsLazyBook is a virtual proxyBooks changes; a file is read only if it is usedBook declares IOExceptionjavac checks nothingjavac checks each part's name and typejavac checks their types, not the specificationjavac refuses a subclass of OfficeQueueListQueue's methods call one anotherCreational
Structural
Behavioral
Wednesday today December 2