Photo by David Emrich on Unsplash

DESIGN PATTERNS IN JAVA SERIES

Design Patterns in Java/Android — Prototype

Explanations, Caveats, Examples from Android Framework & Java Libraries, Writing our own Prototype

Suryakant Bharti
Android Saga
Published in
4 min readMar 13, 2022

--

This series on Software Design Patterns will introduce some common design patterns that we use in app development.

  1. Singleton
  2. Builder
  3. Factory
  4. Prototype ← we are here
  5. Adapter
  6. Facade
  7. Observer
  8. Command

Here, we will take examples of existing design patterns in Android framework & Java Libraries and also write our own patterns in Java.

Introduction to Design Patterns

Software Design Patterns are time-tested, reusable solutions to recurring software problems. They define a common language that helps our team communicate more efficiently.

Design patterns usually deals with objects. The patterns differ by their complexity, reusability, purpose and scale of application in the system. All patterns an be categorized mainly into 3 categories based on their purpose:

  • Creational patterns: How the objects are created.
    Eg. Singleton pattern, Builder pattern, Factory pattern
  • Structural patterns: How the objects are composed (assembling objects into larger structures).
    Eg. Adapter pattern, Facade pattern, Decorator pattern
  • Behavioral patterns: How the objects coordinate (communication and assignment of responsibilities between objects).
    Eg. Observer pattern, Strategy pattern, Command pattern

Prototype/Clone Design Pattern

The Prototype is one of the most common design pattern. It is a simple creational design pattern, which allows us to hide the complexity of making new instances from the client. The concept is to copy an existing object rather than creating a new instance from scratch, something that may include costly operations. The existing object acts as a prototype and contains the state of the object (especially when object creation is a heavy process).

Clones of robots are being created by using Prototype Pattern (Image from refactoring.guru)

Examples of Prototype/Clone from Android Framework & Java Libraries

In a standard Android app, many times we may need to copy existing objects without making our code dependent on their classes. We use Prototype/Clone Pattern at the time. Prototype Pattern is so important that Java has dedicated Cloneable interface (since JDK 1.0), which must be implemented class whose object clone we want to create using clone() method.

ArrayList clone() method

ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java.

HashMap clone() method

HashMap is a part of collection framework and is present in java.util package. It provides the basic implementation of the Map interface of Java.

HashSet clone() method

HashSet is a part of collection framework and is present in java.util package. It provides the basic implementation of the Set interface of Java (backed by a hash table).

Bundle clone() method

Bundle is a part of Android framework and is present in android.os package. It provides mapping from String keys to various Android Parcelable values.

Writing our own Prototype in Java

Let us create our own Prototype Pattern now.

Prototype patterns are required, when object creation is time consuming, and costly operation, so we create objects with the existing object itself. One of the best available ways to create an object from existing objects is the clone() method. Clone is the simplest approach to implement a prototype pattern (however, how to copy existing object should be decided based on our business model). Sometimes we need to have such class. Let’s see how to implement such a class.

1. Prototype/Clone Design Pattern

Prototype Interface

Prototype Interface

Running our Prototype Pattern

2. Prototype/Clone Pattern using Cloneable Interface

Prototype Design Pattern should be used:

  • when a system needs to be independent of how its products are created, composed, and represented.
  • when the classes to instantiate are specified at run-time — to avoid building a class hierarchy of factories that parallels the class hierarchy of products.
  • when instances of a class can have one of only a few different combinations of state. It may be more convenient to install a corresponding number of prototypes and clone them rather than instantiating the class manually, each time with the appropriate state.

-> Running our Prototype Pattern

This code can be used to run the code of Prototype Pattern given above.

This article is supposed to serve as a beginner’s guide for the above design pattern.

Give a clap if you liked the article or a leave a comment for any suggestion/discussion/dispute. Thank you!

--

--

Suryakant Bharti
Android Saga

Android Developer (Kotlin, Java, OOPS, Data Structures, Algorithms, Design Patterns) github.com/Suryakant-Bharti