What is the Model Method Design Pattern?

0

The model method design pattern allows you to define the steps of an algorithm within a single method of a class. This allows you to limit specific operations of an application to a single method.


Each step of the algorithm defined by the model method is an internal method. However, the template method only manages the implementation of some of its internal methods. Subclasses typically implement one or more of the template method’s internal methods.

A subclass can implement an abstract method, but it cannot override the algorithm implemented by a final method.


How the template method works

A typical template method template will have the following structure:

final void templateMethod() {
abstractOperation1();
abstractOperation2();

concreteOperation1();
concreteOperation2();

hook();
}

The first important thing to note is that templateMethod() is final, so no subclass can override this method. The other important thing to note is the three types of methods used by the template method pattern: concrete, abstract, and hook.

The abstract class that hosts the template method implements all of its concrete methods, while the concrete subclasses implement its abstract methods. The hook method usually does nothing by default, but a subclass has the ability to override these methods if needed.

Implementing the Model Method Pattern in Java

The model method template acts as a framework for an application. Thus, you will often see this pattern in software frameworks that provide the blueprint for application development.

For example, you can connect your application to one of many database systems. The steps for connecting and using a database follow a similar pattern:

public abstract class Database {
final void databaseTemplate() {
setDBDriver();
setCredentials();


connect();


createDB();
setData();
readData();


if (userWantsToUpdate()) {
updateData();
}

if (userWantsToDelete()) {
deleteData();
}


closeConnection();
}

abstract void setDBDriver();
abstract void setCredentials();
abstract void createDB();
abstract void setData();
abstract void readData();
abstract void updateData();
abstract void deleteData();

void connect() {
System.out.println("Connecting to database...");
}

void closeConnection() {
System.out.println("Destroying database connection...");
}

boolean userWantsToUpdate() {
return false;
}

boolean userWantsToDelete() {
return false;
}
}

This sample Database class uses the model method pattern to create a model that you can use with any database. To use a database, your application will need to connect to it, then destroy the connection after use. These activities are generally the same for all databases. Therefore, the Database abstract class can implement the relate() and close connection() methods.

The other methods of the model method differ depending on the type of database. For example, a MySQL database stores data in tables, while a MongoDB database stores data in collections. If you want to use a MySQL database in Java, just create a new MySQL class that extends the Database class:

public class MySQL extends Database {
@Override
void setDBDriver() {
System.out.println("Selecting MySQL driver...");
}

@Override
void setCredentials() {
System.out.println("Setting MySQL database Credential...");
}

@Override
void createDB() {
System.out.println("Creating a new table...");
}

@Override
void setData() {
System.out.println("Inserting data into database...");
}

@Override
void readData() {
System.out.println("Retrieving data from database...");
}

@Override
void updateData() {
System.out.println("Updating data in database...");
}

@Override
void deleteData() {
System.out.println("Deleting data from database...");
}
}

The MySQL class implements all the abstract methods of the Database class. It can also override some concrete methods. However, he cannot touch databaseModel() method, which uses the final keyword.

public class Main {
public static void main(String[] args) {
Database mySQLDB = new MySQL();
mySQLDB.databaseTemplate();
}
}

This Main class creates a new MySQL database object and uses the model method to simulate how an application would connect to the database. Executing the main method prints the following output to the console:

MySQL database model method

In the output you will notice that the application never calls the updateData() and delete data() methods. This is why hook methods are important. In an SQL database, you will want to create new tables, insert data, and view your data. However, you may not want to update or delete data. Therefore, hook methods give subclasses the ability to control these critical aspects of the algorithm.

@Override
boolean userWantsToUpdate() {
return true;
}

By simply adding the above code to the MySQL class, the application now updates the data in the database. If you run the Main class again, it will show the following updated output:

Updated MySQL database model method

As you can see the application is now updating the data in the database.

The Benefits of Using the Model Method Design Pattern

A major advantage of the model method pattern is that it promotes software reuse. This design pattern also supports efficient programming. A subclass only needs to implement methods specific to its operation.

Also, an application that uses the model method pattern is more secure because external classes cannot modify its working structure.

Share.

Comments are closed.