April 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Dissection of an Application Frameworks, Part 2

  • May 24, 2005
  • By Xin Chen
  • Send Email »
  • More Articles »
[Editor's Note: This piece picks up where the article Dissection of an Application Frameworks left off.]

The Inheritance Approach

The inheritance approach is driven by two important object-oriented concepts:

  • Hook methods
  • Template methods

Hook Methods

A hook method is a placeholder that will be filled by the application-specific logic. It is the manifestation of a hot spot concept in an actual class. The terms "hook method" and "hot spot" are interchangeable in many publications. However, a hot spot doesn't necessarily take the form of a method; it can also be in the form of a class or application, although the method form is the most common manifestation of a hot spot. Although a hook method can also appear in a concrete class (as we will see later in this section), it often appears in the form of an abstract method inside an abstract class.

An abstract class, by definition, is a class that contains abstract methods. An abstract method defined in the abstract class doesn't contain its method implementation. In order to use an abstract class, a class must inherit from the abstract class and implement the abstract methods in the parent class. Because of this feature, the derived class has the opportunity to inject some customized logic to make it behave according to some specific business requirement. The following example shows how abstract methods work:

public abstract class BasicBusiness{   protected float income;   //the template method   public void ReportTax()   {      float sTax = CalculateStateTax();      float fTax = CalculateFedTax();      bool ok = CheckBankBalance(sTax + fTax);      if (!ok)      {         FileBankruptcy();      }      else      {         SendMoneyToGov(sTax + fTax);      }   }   protected abstract float CalculateStateTax();   protected abstract float CalculateFedTax();}

The BasicBusiness class is an abstract class containing three methods: ReportTax, CalculateStateTax, and CalculateFedTax; the latter two are abstract methods. BasicBusiness is used to report income tax to state and federal government, and it contains the fictitious business-domain knowledge about the action taken at the time of tax filing. ReportTax determines whether the bank balance can cover the total tax. If the balance can cover it, a check will be sent out to the government; otherwise, the account holder will file for bankruptcy.

The ReportTax method depends on two pieces of information in order to determine whether to pay the tax or file for bankruptcy: the tax amount to be paid to the state government and the tax amount to be paid to the federal government. The creators of the BasicBusiness component have no knowledge of the tax law that applies to each situation. For example, depending on the location and type of business, different tax brackets may apply. Since BasicBusiness doesn't know how to calculate the tax amount, it will leave such tasks to someone who knows. The two abstract methods will act as placeholders to be filled later with code that generates the final tax amounts. Although CalculateStateTax and CalculateFedTax are abstract methods, they can be called just like regular methods. As you can see in the previous sample, the CheckBankBalance method takes the return values of two abstract methods as its parameters even though the two abstract methods have not yet been implemented.

Of course, someone has to implement the CalculateStateTax and CalculateFedTax abstract methods before the ReportTax method can provide any meaningful functionality. In fact, because BasicBusiness contains the abstract methods, it can't be used by instantiating it. Instead, you will need to create a concrete class that derives from the BasicBusiness class and implements its two abstract methods. The following example shows a class that extends BasicBusiness and implements its two abstract methods:

public class NewYorkBusiness : BasicBusiness{   //implementation of abstract method   protected override float CalculateStateTax()   {      return income * 0.1F;   }   //implementation of abstract method   protected override float CalculateFedTax()   {      return income * 0.2F;   }}

NewYorkBusiness is a concrete class that provides the tax calculation methods for New York State. With the custom implementation in the NewYorkBusiness class, ReportTax defined in BasicBusiness can perform some meaningful actions, as shown in the next example:

BasicBusiness nyBusiness = new NewYorkBusiness();//ReportTax now will use the tax calculation algorithm defined for//New York.nyBusiness.ReportTax();BasicBusiness caBusiness = new CaliforniaBusiness();//ReportTax now will use the tax calculation algorithm defined for//California.caBusiness.ReportTax();

Abstract methods are a very powerful concept in both application development and framework development. Imagine that the BasicBusiness class is a framework component and the NewYorkBusiness is an application component. BasicBusiness leaves the hot spots (CalculateStateTax and CalculateFedTax) for the application to fill. Each application will fill those spots according to its specific requirements.

Although the application provides the implementation for the abstract method, it often doesn't invoke the abstract methods directly. Abstract methods are often invoked through the template method.

Template Methods

A template method, one of the GOF design patterns, describes a skeleton or process flow for certain operations rather than prescribing how each operation is carried out. In the example of our BasicBusiness component, ReportTax is a template method. The ReportTax method describes what steps are involved in reporting tax, but it does not describe how every step is performed, since some of the methods it references haven't been implemented. The template method emphasizes how the coordination between different objects/methods is carried out. In framework development, template methods contain the business-domain knowledge on how different methods should work together, whereas abstract methods provide a means of custom method implementation that is referenced in the template method. It is important to realize that template methods and hook methods embody two different concepts; Figure 5 illustrates the relationship between an abstract method (or hook method) and a template method in a framework.



Click here for a larger image.

Figure 5. Template method and abstract method

The left side of the figure represents a framework component, which contains the concrete template method that contains the business expertise and knowledge for a specific business domain. The framework component also has a number of hot spots, represented by the abstract methods. The arrows that point from the template method to the abstract methods indicate that the template method calls several abstract methods for their application-specific behaviors.

On the right side of the figure, an application component extends the framework component and implements the abstract methods in the framework component with the application-specific business logic and knowledge. Because the application framework inherits the functionalities of its framework component parent, its overridden methods (or hook methods) and the template method in its parent class together should be able to provide a combination of business-domain expertise/knowledge and application-level business logics.

Hot spots can be enabled not only through the hook and template methods as we just saw, but also through the concept of pluggable objects in a composition approach.

Note: A term has been used in several technical papers to describe this framework approach as the "Hollywood principle" or the "don't call me, I'll call you principle." Such terms describe the characteristic that a template method in the framework calls the application code instead of the application code calling the framework. However, this terminology is somewhat misleading. The reason such terms are used to describe a framework is that they stress that it is the template method in the framework that calls the implemented abstract methods and controls the process flow in the application. In most cases, the application always calls the framework. Although it may appear that a template method is calling the application code, it is the application code that first invokes the template method.




Page 1 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel