CSC/ECE 517 Fall 2010/ch7 7d ID

From Expertiza_Wiki
Jump to navigation Jump to search

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.

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.

http://hillside.net/europlop/europlop2007/workshops [1]

The application references to the indirection table and provides a key (“Key 2”). When resolving this reference, the configuration option (configuration option 2) is used to look up the corresponding resource in the indirection table (Resource 2). In this resource, the key is found that contains the desired value (“Value 2b”). This value is returned to the application and is used as value of the given reference. A reference in the application consists of two parts, a part that gives the name of the indirection table and, if required, a key to locate the resource in the resource file. If the resource has no sub-element but is used as a whole the key can be omitted. The indirection table contains references to the resources. That is, each configuration option refers to exactly one resource. If the resources themselves are not used as a whole then they should be sub structured. Sub structured resources contain key-value-pairs as it is shown in the above figure.


Advantages of Indirection Pattern

  1. This pattern enables decoupling the application from its resources. This means the resources can be exchanged, altered, or updated more flexibly.
  2. There are no direct dependencies between resources and their applications that must be maintained. Due to the generic access associated with the data resources and the indirection step, there is a well-defined separation of the implementation of the application and the resource data.
  3. Decoupled resources can be maintained by different sources without additional effort. This makes it possible that the resources are supplied by different sources and at varying point in times. These resources can be attached easily without having to adapt the overall application.
  4. The application that maintains relations to the independent data resources do not need to be adapted in case of changes to the data resources, in general. In the worst case, only the handling of the indirection table and the setting of the outside configuration option has to be adapted – the rest of the application remains untouched.

Disadvantages of Indirection Pattern

  1. Dynamic aspects are not covered – the intelligent binding of possibly already existing data resources shall be addressed instead since the structuring of data is an additional challenge that is often neglected.
  2. The indirection table has to be maintained with up-to-date references to the resources and the resources must be consistent to each other in terms of the keys that can be retrieved inside.
  3. Finding the correct resource in the indirection table might be an overhead.
  4. There are some rules concerning the structure of the resources and the access rules to these resources which have to be defined once and obeyed by all participating resources and applications. On the one hand, each of the applications that access the resource data needs to know how the resource access has to be conducted and how to access the resources. On the other hand, it is necessary that all resources have to be structured in the same way to make sure that they are interchangeable.

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.

Image from www.cs.toronto.edu [www.cs.toronto.edu/~wl/csc309/handouts/mvc-rest-gdata.pdf]

Advantages of MVC Framework - An indirection pattern

  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.

References

  • Craig Larman, "Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development (3rd Edition)".
  • Birthe Böhm, Norbert Gewald hillside.net.
  • www.icaen.uiowa.ed.
  • Andy Oram and Greg Wilson, "Beautiful Code: Leading Programmers Explain How They Think".
  • Delegation pattern.