Categories
patterns

Pattern Patter: Anonymous Subclass with Instance Initializer

In JUnit Recipes, JB Rainsberger points out this idiom: static final Set s = new HashSet() {{   add(“this”);   add(“that”);   add(“another”); }};  (JB points to an article by Paul Holser, which cites Dave Astels’ Test-Driven Development as the source.) What’s it do? The new HashSet(){}; part creates an anonymous subclass (a new type of HashSet without […]

Categories
patterns

Pattern Patter: Hide Information in Plain Sight

By B. Douglas Wake and William C. Wake Summary: Sometimes we have information that is potentially useful or important, but is not normally of interest. This pattern discusses when and how to hide this information in plain sight. Context Information is designed using a high format and a powerful (often new) tool. Information is viewed […]

Categories
patterns

Pattern Patter: Renderer

Summary: Use renderers to show sub-views. Context You’ve separated the model of a complex object from its view (as in Observer or Model-View-Controller). The bulk of the view doesn’t change much. Variable items (“cells”) in the model may require a specialized sub-view. Solution Divide the view into two parts. Let the main view display the […]

Categories
patterns

Pattern Patter: “Template Method” in Java

Summary: How to use the “Template Method” in Java frameworks. The Template Method is a pattern commonly used in frameworks to implement the “Hollywood Principle”: “Don’t call us, we’ll call you.” [Design Patterns, Gamma et al.] The key idea is that there is a basic algorithm we want to stay the same, but we want […]

Categories
patterns

Pattern Patter: Backpatching

Summary: Use mostly-sequential output streams by fixing them later. You’re generating a stream of output. Everything is mostly fine, except that you have some information that really belongs at the front of the file or at a few places in the middle. Forces Generating an output stream (mostly sequential). Information occasionally must go in earlier […]

Categories
patterns

Pattern Patter: Templates/Macros

Summary: You want to extend a language without writing a new compiler.  Forces Working in a particular language. Want to extend it for convenience. Subroutines are not a convenient mechanism: either they’re unavailable or don’t fit syntactically. Example C is extended with a macro language. Pro*C (Oracle) extends C with Oracle features. Resolution Use a […]

Categories
patterns

Pattern Patter: Program Generator

Write a program to write programs. “I’d rather write programs to write programs than write programs.”  Dick Sites [DEC], quoted by Jon Bentley in More Programming Pearls. Context Have a high-level specification of a program; want to be able to run it directly. Forces High-level specifications can’t be executed directly. An interpreter might be too slow. […]

Categories
patterns

Pattern Patter: Table-Driven Design

Summary: Code that’s tedious to write, but must be changed easily, can often be accommodated with a table-driven design.  Forces Code is most easily written “ad-hoc”. There’s often a structural aspect that is constant even though the details change. Ad-hoc code is bulky because it repeats the “constant” part. Example Language grammars undergo numerous small […]

Categories
compilers patterns

Pattern Patter: JIT Compiler

Build a compiler that compiles just before (or during) runtime to get benefits of both compilers and interpreters. [Architectural Pattern] Context Language implementation. Forces An interpreter has advantages over a compiler: It’s usually the fastest, easiest way to implement a language. The representation used by the interpreter can be space-efficient. The representation used by the […]

Categories
patterns

Pattern Patter: Interface-Abstract-Default

Summary: Provide three “classes”: an interface, an abstract class, and a default implementation. [Design Pattern] Context You are developing a framework of cooperating classes. Forces You want to make things easy for the user, so you want to provide implementations. But you want your design clean, and you want the user to provide implementations where […]