<img alt="" src="https://secure.leadforensics.com/150446.png " style="display:none;">

Design Pattern Combination – Strategy with Factory Method

Madhura Oak Aug 07, 2013

Java Factory Method Strategy Design Patterns GoF Technology

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=""]

Strategy design pattern

[/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.

Strategy with Factory Method design pattern

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.

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.