October 24, 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 »

The Composition Approach

The inheritance approach is a simple approach to enabling hot spots inside a framework, but because developers must know what data and methods are available inside the parent class as well as their interrelations in order to implement the abstract method, they often are required to have a very detailed knowledge of a framework in order to use it.

For example, in the NewYorkBusiness class, as easy as the implementation of CalculateStateTax and CalculateFedTax may seem, in order to implement these two methods, the developer must know that there is a protected float variable called income and that its value must have been set somewhere else prior to invoking either of the CalculateXxxTax methods. Also, exposing the internal details of the child class diminishes the encapsulation of the parent class, which can lead to the uncontrolled access and modification of the class's internal state by developers that are beyond the intent of parent class's designer. The following code segment shows the filling of the "hot spot" in an inheritance approach:

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

As you can imagine, as the method grows more complicated, developers may need to reference more data and methods inside the parent class and understand what the consequences are of setting the values of such data and calling such methods to change the overall state of the object.

Requiring developers to know such detailed information about the parent class stretches their learning curve for the framework and is burdensome to developers using the framework. One way to keep developers from having to learn the internal details of framework components is to define the hot spots as a set of interfaces that are well connected with the framework. Developers can create components by implementing such interfaces, which can then be plugged into the framework to customize its behaviors.

Pluggable Components

To enable hot spots through pluggable components, the application framework must first define the interface for the hot spot. An interface describes a set of methods a class must implement to be considered compatible with the interface. The interface describes what the methods inside the class look like, such as the method names, the number of parameters, and the parameter types, but not how they should be implemented. The following is an example of an interface definition in C#:

public interface ICalculateTax{   float CalculateStateTax();   float CalculateFedTax();   float Income   {      get;set;   }}

You can then create application components that support ICalculateTax by creating a concrete class and implementing every method/property defined in the interface, such as the NewYorkBusiness class shown as follows:

public class NewYorkBusiness : ICalculateTax{   private float income;   public float Income   {      get { return income; }      set { income = value; }   }   public float CalculateStateTax()   {      return income * 0.1F;   }   public float CalculateFedTax()   {      return income * 0.2F;   }}

Because NewYorkBusiness implements the ICalculateTax interface, it now becomes compatible with or pluggable to any hot spot in the framework that can work with the ICalculateTax interface. With the help of interfaces, application developers can compose the custom behaviors into the underlying framework by loading the pluggable application components in the hot spots of the framework. Figure 6 illustrates how the composition approach enables the hot spots in the application framework.



Click here for a larger image.

Figure 6. A pluggable application component

In using the composition approach to enable the hot spot, developers will need to create pluggable application components that have matching interfaces with the hot spot in the framework. The developer can then plug the component into the hot spot by binding the application component and the framework component together.

This composition approach for enabling hot spots is based on yet another GOF design pattern called "strategy." You will learn more about the strategy pattern in Chapter 5.

Now let's convert the tax example so that the hot spot is enabled through the composition approach. First, we need to modify the BasicBusiness component. Instead of making it an abstract class, this time we will make it a concrete class. The following code snippet shows the new BasicBusiness component:

public class BasicBusiness{   public void ReportTax (ICalculateTax calculator)   {      float sTax = calculator.CalculateStateTax();      float fTax = calculator.CalculateFedTax();      bool ok = CheckBankBalance(sTax +fTax);      if (!ok)      {         FileBankruptcy();      }      else      {         SendMoneyToGov(sTax + fTax);      }   }}

The ReportTax method now takes an input parameter of ICalculateTax type. This input parameter will provide the custom tax calculation mechanism, which is also a hot spot in the BasicBusiness framework component. As you can see, by plugging in the custom application component, we effectively "fill up" the hot spot with application-specific business logic/knowledge.

The following example shows how we can plug the application component into the framework from the application code:

ICalculateTax nyBusiness = new NewYorkBusiness();ICalculateTax caBusiness = new CaliforniaBusiness();BasicBusiness basic= new BasicBusiness();basic.ReportTax(nyBusiness);basic.ReportTax(caBusiness);

In the previous example, each ReportTax call will result in a different outcome, since the framework component is bound to a different ICalculateTax component on each call.

In order for the framework component to load the pluggable object, you can either use the approach described in the previous example or store the application component's type information inside a configuration file, then load the appropriate component through reflection and plug it right into the framework component dynamically.

Identifying and enabling common spots and hot spots are the central themes of application framework development. Depending on how you enable such spots, you create either a white-box framework, a black-box framework, or a gray-box framework.





Page 2 of 4



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel