The active object
design pattern
A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" b ...
decouples method execution from method invocation for objects that each reside in their own
thread
Thread may refer to:
Objects
* Thread (yarn), a kind of thin yarn used for sewing
** Thread (unit of measurement), a cotton yarn measure
* Screw thread, a helical ridge on a cylindrical fastener
Arts and entertainment
* ''Thread'' (film), 2016 ...
of control. The goal is to introduce
concurrency
Concurrent means happening at the same time. Concurrency, concurrent, or concurrence may refer to:
Law
* Concurrence, in jurisprudence, the need to prove both ''actus reus'' and ''mens rea''
* Concurring opinion (also called a "concurrence"), a ...
, by using
asynchronous method invocation
In multithreaded computer programming, asynchronous method invocation (AMI), also known as asynchronous method calls or the asynchronous pattern is a design pattern in which the call site is not blocked while waiting for the called code to fini ...
and a
scheduler for handling requests.
The pattern consists of six elements:
* A
proxy
Proxy may refer to:
* Proxy or agent (law), a substitute authorized to act for another entity or a document which authorizes the agent so to act
* Proxy (climate), a measured variable used to infer the value of a variable of interest in climate re ...
, which provides an interface towards clients with publicly accessible methods.
* An interface which defines the method request on an active object.
* A list of pending requests from clients.
* A
scheduler, which decides which request to execute next.
* The implementation of the active object method.
* A
callback
Callback may refer to:
* Callback (comedy), a joke which refers to one previously told
* Callback (computer programming), executable code that is passed as a parameter to other code
* Callback (telecommunications), the telecommunications event tha ...
or
variable for the client to receive the result.
Example
Java
An example of active object pattern in
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
.
Firstly we can see a standard class that provides two methods that set a double to be a certain value. This class does NOT conform to the active object pattern.
class MyClass
The class is dangerous in a multithreading scenario because both methods can be called simultaneously, so the value of val (which is not atomic—it's updated in multiple steps) could be undefined—a classic race condition. You can, of course, use synchronization to solve this problem, which in this trivial case is easy. But once the class becomes realistically complex, synchronization can become very difficult.
To rewrite this class as an active object, you could do the following:
class MyActiveObject
Java 8 (alternative)
Another example of active object pattern in Java instead implemented in Java 8 providing a shorter solution.
public class MyClass
See also
*
Concurrent object-oriented programming
*
Actor model
The actor model in computer science is a mathematical model of concurrent computation that treats ''actor'' as the universal primitive of concurrent computation. In response to a message it receives, an actor can: make local decisions, create mor ...
*
Futures and promises
In computer science, future, promise, delay, and deferred refer to constructs used for synchronizing program execution in some concurrent programming languages. They describe an object that acts as a proxy for a result that is initially unknown ...
*
Live distributed object
References
External links
Ultra-high performance middleware based on Disruptor Active Object in C++14Active Object implementation in C++11
Software design patterns
Concurrency (computer science)
Articles with example Java code
{{Compu-prog-stub