Target-Action
Encyclopedia
The term target–action design paradigm
Design paradigm
The term Design paradigm derives from the rather ambiguous idea of paradigm originating in Sociology of Science, which carries at least two main meanings:...

 refers to a kind of software architecture
Software architecture
The software architecture of a system is the set of structures needed to reason about the system, which comprise software elements, relations among them, and properties of both...

, where a computer program
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...

 is divided up into objects
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...

 which dynamically establish relationships by telling each other which object they should target and what action or message to send to that target when an event occurs. This is especially useful when implementing graphical user interface
Graphical user interface
In computing, a graphical user interface is a type of user interface that allows users to interact with electronic devices with images rather than text commands. GUIs can be used in computers, hand-held devices such as MP3 players, portable media players or gaming devices, household appliances and...

s, which are by nature event-driven
Event-driven programming
In computer programming, event-driven programming or event-based programming is a programming paradigm in which the flow of the program is determined by events—i.e., sensor outputs or user actions or messages from other programs or threads.Event-driven programming can also be defined as an...

.

Advantages

The target–action approach to event-driven systems allows far more dynamism when compared to other, more static approaches, such as by subclassing. That is because subclassing is a relatively stiff way to program: a programmer must lay out the internal interconnection logic of a program at design time and this cannot be changed later, unless the program is stopped, reengineered, and rebuilt. On the other hand, target-action based programming can change these completely at run-time, thus allowing the program to create new interrelationships and novel behavior by itself.

A prime example of this approach is the OpenStep
OpenStep
OpenStep was an object-oriented application programming interface specification for an object-oriented operating system that used a non-NeXTSTEP operating system as its core, principally developed by NeXT with Sun Microsystems. OPENSTEP was a specific implementation of the OpenStep API developed...

 API, which partly thanks to being based on the dynamic Objective-C
Objective-C
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...

 language, has much of its graphical user interface implemented by using the target-action paradigm. Consider the following example, written in Objective-C:

[button setTarget: self];
[button setAction: @selector(doSomething)];

Now when the button
Button (computing)
In computing, a button is a user interface element that provides the user a simple way to trigger an event, like searching for a query at a search engine, or to interact with dialog boxes, like confirming an action.-Description:A typical button is a rectangle or rounded rectangle, wider than it is...

 identified by the button variable
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...

 is pressed, the runtime system will try to send a message named doSomething to the object in which this code has been invoked. It is also very well possible to determine the message to be sent at run-time:

[button setTarget: self];
[button setAction: NSSelectorFromString([textField stringValue])];

Here the message which is to be sent is determined by consulting a text field's string value (the string of text which the user typed into a text field). This string is afterwards converted into a message (using the NSSelectorFromString function) and passed to the button as its action. This is possible because, under Objective-C, methods are represented by a selector, a simple string describing the method to be called. When a message is sent, the selector is sent into the ObjC runtime, matched against a list of available methods, and the method's implementation is called. The implementation of the method is looked up at runtime, not compile time.

Disadvantages

Because of the extreme dynamism and freedom of behavior given to programs designed with the target-action paradigm, it can happen that the program designer incorrectly implements a part of the interconnection logic and this can lead to sometimes hard to trace bugs. This is due to the lack of compile-time control provided by the compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...

 which cannot see the interconnections. Thus interconnection consistency control is left entirely to the programmer.

The result of an incorrectly connected target-action binding can differ based on how the particular system in which the program is implemented regards this:
  • it can ignore the condition and perform nothing (such as in the button example above – if the target of a button doesn't respond to its action message, pushing the button simply does nothing)
  • it can consider such a state to be a bug
    Software bug
    A software bug is the common term used to describe an error, flaw, mistake, failure, or fault in a computer program or system that produces an incorrect or unexpected result, or causes it to behave in unintended ways. Most bugs arise from mistakes and errors made by people in either a program's...

    , in which case it will most likely cause the program to crash
    Crash (computing)
    A crash in computing is a condition where a computer or a program, either an application or part of the operating system, ceases to function properly, often exiting after encountering errors. Often the offending program may appear to freeze or hang until a crash reporting service documents...

     or invoke a run-time exception

See also

  • The OpenStep
    OpenStep
    OpenStep was an object-oriented application programming interface specification for an object-oriented operating system that used a non-NeXTSTEP operating system as its core, principally developed by NeXT with Sun Microsystems. OPENSTEP was a specific implementation of the OpenStep API developed...

     API
    • OPENSTEP
      OpenStep
      OpenStep was an object-oriented application programming interface specification for an object-oriented operating system that used a non-NeXTSTEP operating system as its core, principally developed by NeXT with Sun Microsystems. OPENSTEP was a specific implementation of the OpenStep API developed...

       – an operating system by NeXT Software Inc. (now Apple Inc.) largely based on the OpenStep API
    • GNUstep
      GNUstep
      GNUstep is a free software implementation of Cocoa Objective-C libraries , widget toolkit, and application development tools not only for Unix-like operating systems, but also for Microsoft Windows. It is part of the GNU Project.GNUstep features a cross-platform, object-oriented development...

       – a free implementation of the OpenStep API
    • Cocoa
      Cocoa (API)
      Cocoa is Apple's native object-oriented application programming interface for the Mac OS X operating system and—along with the Cocoa Touch extension for gesture recognition and animation—for applications for the iOS operating system, used on Apple devices such as the iPhone, the iPod Touch, and...

       – an OpenStep derived API used on Mac OS X
      Mac OS X
      Mac OS X is a series of Unix-based operating systems and graphical user interfaces developed, marketed, and sold by Apple Inc. Since 2002, has been included with all new Macintosh computer systems...

  • Objective-C
    Objective-C
    Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...


External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK