February 25, 2021
Hot Topics:

Working With Design Patterns: Prototype

  • By Jeff Langr
  • Send Email »
  • More Articles »

Listing 6: The Material hierarchy.

// Material.java
abstract public class Material {
   private final String title;
   private final String classification;
   private final String author;
   private final String year;
   private String branch;
   private int copyNumber;

   public Material(String classification, String title,
                   String author, String year, int copyNumber) {
      this.classification = classification;
      this.title = title;
      this.author = author;
      this.year = year;
      this.copyNumber = copyNumber;
      branch = "checked out";

   abstract public Material copy();

   public String getAuthor() {
      return author;

   public String getClassification() {
      return classification;

   public String getTitle() {
      return title;

   public String getYear() {
      return year;

   public int getCopyNumber() {
      return copyNumber;

   public String getBranch() {
      return branch;

// Book.java
public class Book extends Material {
   public Book(String classification, String title,
               String author, String year,
         int copyNumber) {
      super(classification, title, author, year, copyNumber);

   public Book copy() {
      return new Book(getClassification(), getTitle(),
                      getAuthor(), getYear(),
                      getCopyNumber() + 1);

// Movie.java
public class Movie extends Material {
   public static enum Format {
      DVD, BluRay, VHS

   private Format format;

   public Movie(String classification, String title, String author,
         String year, int copyNumber, Format format) {
      super(classification, title, author, year, copyNumber);
      this.format = format;

   public Format getFormat() {
      return format;

   public Movie copy() {
      return new Movie(getClassification(), getTitle(),
                       getAuthor(), getYear(),
                       getCopyNumber() + 1, getFormat());
Note: I did not choose to use the clone method in Java for this implementation, a design choice that I made based on the current construction design. If you do implement clone, make sure you read up on all the ramifications of doing so, including discussions of shallow versus deep copies. Joshua Bloch's book Effective Java is a good source.

The focus of prototype is the bolded line in the Catalog addCopy method (refer to Listing 5) that tells an existing material to return a copy of itself. This concise single line allows the Catalog to be closed to any existing Material or Material subclass changes and also to additions of new Material subclasses. The prototype pattern also adheres to other basic notions of good OO design: It is polymorphic, and fulfills the dependency inversion and single responsibility principles.

Click here for a larger image.

Figure 1: Prototype

About the Author

Jeff Langr is a veteran software developer with over a quarter century of professional software development experience. He's written two books, including Agile Java: Crafting Code With Test-Driven Development (Prentice Hall) in 2005. Jeff is contributing a chapter to Uncle Bob Martin's upcoming book, Clean Code. Jeff has written over 75 articles on software development, with over thirty appearing at Developer.com. You can find out more about Jeff at his site, http://langrsoft.com, or you can contact him via email at jeff at langrsoft dot com.

Page 3 of 3

This article was originally published on April 2, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date