# Java: Frameworks

## What is a framework?

Frameworks are large bodies (usually many classes) of prewritten code to which you add your own code to solve a problem in a specific domain. Perhaps you could say that the framework uses your code because it is usually the framework that is in control. You make use of a framework by calling its methods, inheritance, and supplying "callbacks", listeners, or other implementations of the Observer pattern.

Constrast to library. Although sometimes large libraries are referred to as frameworks, this is probably not the most common use of the term.

The distinction between framework and library is defined quite well in Patterns and Software: Essential Concepts and Terminology.

> The difference between a framework and an ordinary programming library is that a framework employs an inverted flow of control between itself and its clients. When using a framework, one usually just implements a few callback functions or specializes a few classes, and then invokes a single method or procedure. At this point, the framework does the rest of the work for you, invoking any necessary client callbacks or methods at the appropriate time and place. For this reason, frameworks are often said to abide by the Hollywood Principle ("Don't call us, we'll call you.") or the Greyhound Principle ("Leave the driving to us.").

## Usefulness of frameworks

A framework will often dictate the structure of your application. Some frameworks even supply so much code that you have to do very little to write your application. This can be good or bad, depending on how easy it is to use. As Wil Shipley says:

> Frameworks are the substance of programming. You build on top of a good one, your program is solid and fast and comes together beautifully. You build on top of a bad one, your life is miserable, brutish, and short.

## Examples in Java

A very common example are GUI frameworks, eg Java's Swing and AWT classes. They have a huge amount of code to manage the user interface, and there is inversion of control because you start the GUI framework and then wait for it to call your listeners.

The Collections classes are sometimes called a framework, perhaps largely because of the size and complexity. But it is more properly referred to as a library because there is no inversion of control -- your programs simply calls methods in these predefined or extended classes.

Basic GUI framework? A criticism of Java is that a lot of explicit programming is still required to get even simple GUI programs running. There is a Java Specification Request, JSR 296: Swing Application Framework to lighten the programmer's burden.

## References

- Patterns and Software: Essential Concepts and Terminology by Brad Appleton (www.cmcrossroads.com/bradapp/docs/patterns-intro.html). This is primarily about patterns but does have a small section discussing the relationship between patterns and frameworks.
- Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries by by Krzysztof Cwalina, Brad Abrams is reportedly a very good book about frameworks, altho it isn't about Java. The following comment from an Amazon review made me think this book might be quite well organized -- perhaps I'll use this categorization too!

  > Guidelines in this book come in four major forms: Do, Consider, Avoid, and Do not. In general, a Do guideline should almost always be followed, a Consider guideline should generally be followed, an Avoid guideline indicates that something is generally not a good idea, and a Do not guideline indicates something you should almost never do. Every guideline includes a discussion of its applicability, and most guidelines include a code example.