Framework Patterns



Framework Patterns

Framework features

  • Framework approach of solving recursive problems in software is called “Old Code Calls New Code”. 
  • Framework has two important features 1) Kernel of framework 2) Hot spots.
  • Kernel of framework is immutable. The features of kernel cannot be altered and are called frozen spots of framework.
  • Hot spots in a framework are points of flexibility of a framework. They can be abstract classes that have no implementation and must be customized in order to be instantiated.

  •  Actually Hotspot may take the form of a) Hook Method b) Class c) Application.
  • Once framework is instantiated, frameworks kernel calls the hot spots (customized classes or methods) using the call-back mechanism.

  • Hotspots are the extensible points. Using hotspots, framework implements the design principle “Encapsulate what varies”.

Types of Frameworks

  1. White-box frameworks
  2. Black-box frameworks
  3. Backbone frameworks

White-Box Frameworks

Important patterns that help to build White-Box Frameworks (Skeleton applications)

1.       Template method pattern

  • Template method pattern defines the high level algorithm and allows sub classes to provide the implementation for one or more steps. 
  • It defines the skeleton of an algorithm, some steps will be deferred.
  • Template method pattern lets sub classes redefine certain steps of an algorithm without changing high level structure of solution algorithm.
  • The abstract class contains the template method.
  • The Template method makes use of primiteOperations to implement high level policy/algorithm.
  • Template method (high level logic) is decopuled from actual implementation of primitive operations.

MFC Message and Command Routing is a white-box framework.

  • Every Windows-based program needs a message pump to handle windows messages generated by user actions and system itself. 
        MSG msg;
        while (GetMessage(&msg, NULL, 0, 0)) 
        {
             TranslateMessage(&msg);
             DispatchMessage(&msg);  // send to window proc
        }
  • But a realistic message loop will not be as simple as that, since there are most common scenarios like a) Handling messages of modeless dialogs b) Handling accelerators etc.
  • MFC framework does this by itself; the message loop is encapsulated in CWinApp::Run
  • CWinApp::Run is the template method and it uses primitive operations like :- OnIdle, PumpMessage, PreTranslateMessage, DispatchMessage etc.
  • If required we can customize PreTranslateMessage.
  • When DispatchMessage is called, internally AfxWndProc gets invoked, which calls CWnd WindowProc, and WinowProc looks into the MESSAGE MAP Table and invokes callback message handlers implemented by developer.

  •  So the message handler functions (hot spots or extensible points) are the primitive operations that the High level message routing algorithm uses by call-back mechanism.

Black-Box Frameworks

  • The extensible components or hotspots will be external binary components.
  • But the extensible components should have a well defined interface.These extensible external components will be integrated into the framework.
  • Framework developers first define the interface of the hotspot. And developers who want to instantiate the framework write pluggable components implementing those interface(Contract). Then these custom pluggable components will be integrated with the framework.
  • In black-box frameworks we will have concrete and ready to use classes and services.
  • In black-box frameworks, components hide their internal implementation.
  • Black box frameworks will be configured (details of plug-ins or hotspots) to be instantiated.

Important patterns that help to build Black-Box Frameworks

1.       Strategy pattern (with Plug-and-Play mechanism)

  • Strategy pattern defines a family of algorithms, encapsulates each one and make them interchangeable.
  • Strategy pattern lets the algorithm vary independently from  clients that use it.
  • Strategy pattern allows "Plug-And-Play"-ing.
  •  The Context maintains the reference to a strategy object. It will be configured with the name of Concrete strategy class. And that concrete strategy will be loaded at compile-time.
  • The appropriate strategy(when a dll exporting Strategy interface is registered with Context) can be loaded dynamically.
Examples of Black-Box Frameworks

ADO.NET Data Providers

The ADO.NET Data Provider model provides a common managed interface in the .NET Framework for connecting to and interacting with a data store.  Third-party vendors provide the assemblies/binaries that expose that standard interface. Application using ADO.NET will be configured with the provider assembly details and appropriate plug-in will be loaded.

WinLogon , GINA and Network provide

  • Winlogon, the GINA, and network providers are the parts of the interactive logon model.
  • Interactive logon process is entirely controlled by Winlogon, but uses customizable GINA dll( for displaying custom dialogs and handling user interactions).
  • Winlogon sends secure attention sequence (SAS) events to GINA, so that appropriate UI is displayed.
  • Winlogon actually calls methods exposed by GINA like WlxNegotiate, WlxInitialize, WlxLoggedOutSAS, WlxLoggedOnSAS, WlxSasNotify etc corresponding to SAS event Winlogon encounters allowing third party GINA to provide custom UI and add additional steps in interactive logon process.
  • Winlogon also uses the network providers plug-ins. Following a successful logon, Winlogon calls network providers so they can collect credentials and authenticate the user for their network
  • This is a quite good example of Black-box framework, where the contract between framework kernel(Winlogon) and hotspots(GINA, Network providers) is well defined. Once third-party GINA is registered with Winlogon, Winlogon loads the plug-in and uses it for successful completion of interactive logon.
 

No comments:

Post a Comment