e-Zest members share technology ideas to foster digital transformation.

Design Pattern Combination – Strategy with Factory Method

Written by Madhura Oak | Aug 7, 2013 11:51:11 AM

When we implement certain design patterns, they are usually used in combination with other design pattern. Though we have a catalog of design patterns such as Gang of Four (GoF), we do not have a formal specification which mandates the use of such a design pattern along with its related pattern.

In this blog, I’m going to write about one such design pattern – Strategy. It is always useful to implement Strategy along with Factory Method. Both Strategy and Factory Method are Gang of Four (GoF) design patterns.

Strategy design pattern allows selection of an algorithm’s behaviour during runtime. An example of Strategy is shown in Figure 1.

[imageframe style="border" bordercolor="" bordersize="4px" stylecolor="" align=""]

[/imageframe]

Figure 1. Strategy design pattern

The DocumentExtractor class will need a code shown in Listing 1 to call the appropriate implementation of StatementExtractor based on the type of document.

StatementExtractor extractor = null;
if(type.equals(“BankAccountStatement”)) {
   extractor = new BankAccountStatementExtractor();
}
else if(type.equals(“CreditCardStatement”)) {
   extractor = new CreditCardStatementExtractor();
}
extractor.extract(statement);

Listing 1. Call the appropriate implementation based on document type

When a Strategy design pattern is implemented, such code to use the appropriate implementation class is always required. Instead of embedding this code directly in the class which calls the algorithm, a better approach is to decouple this code into another class which implements Factory method.

Thus, the design in Figure 1 can be modified by adding the Factory method design pattern as shown in Figure 2.

Figure 2. Strategy with Factory Method design pattern

The code to use the appropriate implementation of StatementExtractor based on document type is now present in StatementExtractorFactory as shown in Listing 2. The factory class is used by DocumentExtractor as shown in Listing 3.

public class StatementExtractorFactory {

     private static Map <String,StatementExtractor> extractors =
                 new HashMap<>();
      static {
            extractors.put(“BankAccountStatement”,
                     new BankAccountStatementExtractor());
            extractors.put(“CreditCardStatement”,
                     new CreditCardStatementExtractor());
      }
      public static StatementExtractor getInstance(String type) {
            return extractors.get(type);
      }
}

Listing 2. StatementExtractorFactory

public class DocumentExtractor {
     public void extractDocument() {
       …
       StatementExtractor extractor =
              StatementExtractorFactory.getInstance(type);
       extractor.extract(statement);
       …
    }
}

Listing 3. DocumentExtractor

A Factory class can create new instances in a multithreaded application.

Thus, the Strategy design pattern should always be implemented with Factory Method design pattern.