CSC/ECE 517 Fall 2010/ch7 7d ID: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 51: Line 51:
The configuration option is either set globally or it might be also provided as an input parameter to the indirection table. Changing the configuration option means retrieving a reference to another resource and accessing this resource with the given resource key instead. That means that a different value is retrieved based on a different configuration option but with identical resource key.
The configuration option is either set globally or it might be also provided as an input parameter to the indirection table. Changing the configuration option means retrieving a reference to another resource and accessing this resource with the given resource key instead. That means that a different value is retrieved based on a different configuration option but with identical resource key.


[[Image:test.png]]
[[Image:Fig1.jpg]]

Revision as of 05:12, 2 December 2010

Introduction

GRASP(General Responsibility Assignment Software Patterns) in object oriented design gives guidelines for assigning responsibility to classes and objects. Some of the guidelines include the following:

  1. To assign a responsibility to a class
  2. To avoid or minimize additional dependencies
  3. To maximise cohesion and minimise coupling
  4. To increase reuse and decrease maintenance
  5. To maximise understandability

There are nine GRASP patterns: Creator, Controller, Pure Fabrication, Information Expert, High Cohesion, Indirection, Low Coupling, Polymorphism and Protected Variations. In this chapter we will look at the Indirection design pattern with some examples.

What is Indirection Pattern?

Indirection pattern reduces the coupling between the data objects by setting up an intermediary object which performs the mediation between the objects. The intermediary creates an indirection between the other components. Indirection is utilized by GoF proxy, Bridge, and Mediator patterns. Architectural concepts developed currently for software design and development demand the decomposition of a system into independent modules with well-defined interfaces. This leads to decoupling of data from the rest of the system by hiding the data access details in an abstract interface. To achieve this, it is good to use an intermediate object to mediate between other objects. Indirection pattern describes how the data resources can be exchanged in communication without disrupting the existing application. One of the best applications of Indirection pattern is to embed multilingual texts in language-independent data or into software applications. An high level example would be to support multi-languages like English, Spanish, French for user manual for a software system.

When to use Indirection Pattern?

Indirection Pattern is used when the relation between an application or some parent data and its resources should be stored both explicitly and flexibly at the same time, so that it is possible to exchange these resources quickly and without adapting the application itself.

Example: MVC Framework

Model-View-Controller is an example for the indirection pattern often used by applications that need the ability to maintain multiple views of the same data. By indirection pattern, responsibilities like handling event call backs can be taken care by controller and the view can operate its interface logic without any changes. A controller is an intermediary class whose job is to coordinate the events. It sees to it the messages are sent to the correct expert in the model.

Advantages

  1. Because of this separation, multiple views and controllers can interface with the same model. Even new types of views and controllers that never existed before can interface with a model without forcing a change in the model design.
  2. Events typically cause a controller to change a model, or view, or both. Whenever a controller changes a model’s data or properties, all dependent views are automatically updated. Similarly, whenever a controller changes a view, for example, by revealing areas that were previously hidden, the view gets data from the underlying model to refresh itself.

Law of Demeter and Indirection Pattern

The prime objective of Indirection Pattern is to avoid high coupling. This is done to limit the knowledge possessed by the client objects regarding the internal structure of the server objects. This is achieved by imposing constraints on method invocations that includes the invocations by:

  1. Current object (self)
  2. Parameter of the method
  3. An attribute of self
  4. An element of a collection that is an attribute of self
  5. A local object of the method.

Indirection Pattern and Software Development

Experience from long used software systems show that data and their underlying data models are the most important important aspect of architecture and systems design. This is primarily because of longer existence of data. Data exists significantly longer than the software system that intended to use this data. Also, this data is processed by many different systems. Hence, a consistent definition and usage of data resources is required to retain control over this data over time. A good approach is to separate this data from the accessing applications. This is otherwise referred to as information hiding. This separation is achieved by referencing a resource file and exchanging it should the resource file be altered or substituted. The elements in the resource file are found by identifier inside the file.Using this concept, we decouple the resources from the application, but there is an issue that looms large as the application is still bound to the resources. If the user wants to store different variations of the same resource and choose the correct resource on the basis of contextual configuration, user might face some problems.Some of these problems are like an application should be presented to the user in a different language depending on the language of the operating environment. If the relation to the variations of the resource was given in the application, there would be multiple references to the variations of the resource. It is required that the application be altered always when a new language is added so as to establish the references to the new language resource as well.

The core objective of a good software design is to achieve high decoupling of the applications from their resources – based on a concept that enables a higher-level as well as a lower-level abstraction of the software implementation details. In order to support the decoupling while still maintaining the relations,the following points are to be observed:

  1. Data resources need to be separated from the software applications processing them.In other words, data resources must be independent from the applications using them. The reasons for doing so might be to either allow these resources to be easily swapped for alternate resources based on the context or these resources should live longer than their environment.
  2. Dependencies between resources and applications should be avoided.
  3. Data resources that are provided from different sources and at a different point in time can be attached to an already existing application without any adaptations.
  4. If the relation to the resources is given in the application then the application should not be altered in case of resource changes. Such changes may arise because additional resources are added or a resource is moved or renamed.

The perfect solution to the problem above after taking into consideration the key points during the software design is to use Indirection Pattern. The solution involves introducing an indirection table between the application and the separated data resources. This indirection table maps specific values of a configuration option to a reference of one of the separated resources.The indirection table maintains references to all resources that are addressed via the indirection table.These resources are structured identically so that they map resource keys to their concrete values. In any application dealing with this concept, an optimal algorithm that looks up the adequate resource by means of a configuration option in the indirection table. For each defined configuration option value, a reference to a resource is retrieved. This resource shall be used by the algorithm to look up the required value for a given resource key. Figure 1 shows the concept described so far. The configuration option is either set globally or it might be also provided as an input parameter to the indirection table. Changing the configuration option means retrieving a reference to another resource and accessing this resource with the given resource key instead. That means that a different value is retrieved based on a different configuration option but with identical resource key.