Jump to content

Active object

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 130.255.152.163 (talk) at 08:19, 22 February 2015. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

The active object design pattern decouples method execution from method invocation for objects that each reside in their own thread of control.[1] The goal is to introduce concurrency, by using asynchronous method invocation and a scheduler for handling requests.[2]

The pattern consists of six elements:[3]

  • A proxy, 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 or variable for the client to receive the result.

Example

C++

An example of active object pattern in C++11Cite error: A <ref> tag is missing the closing </ref> (see the help page).

class OriginalClass
{
    private double val = 0.0;

    //
    void doSomething()
    {
        val = 1.0;
    }

    //
    void doSomethingElse()
    {
        val = 2.0;
    }
}

class BecomeActiveObject
{
    private double val = 0.0;
    private BlockingQueue<Runnable> dispatchQueue
            = new LinkedBlockingQueue<Runnable>();

    //
    public BecomeActiveObject()
    {
        new Thread(
                new Runnable()
                {
                    @Override
                    public void run()
                    {
                        while (true)
                        {
                            try
                            {
                                dispatchQueue.take().run();
                            } catch (InterruptedException e)
                            {   // okay, just terminate the dispatcher
                            }
                        }
                    }
                }
        ).start();
    }

    //
    void doSomething() throws InterruptedException
    {
        dispatchQueue.put(
                new Runnable()
                {
                    public void run() { val = 1.0; }
                }
        );
    }

    //
    void doSomethingElse() throws InterruptedException
    {
        dispatchQueue.put(
                new Runnable()
                {
                    public void run() { val = 2.0; }
                }
        );
    }
}

See also

References

  1. ^ Douglas C. Schmidt; Michael Stal; Hans Rohnert; Frank Buschmann (2000). Pattern-Oriented Software Architecture, Volume 2: Patterns for Concurrent and Networked Objects. John Wiley & Sons. ISBN 0-471-60695-2.
  2. ^ Bass, L., Clements, P., Kazman, R. Software Architecture in Practice. Addison Wesley, 2003
  3. ^ Lavender, R. Greg; Schmidt, Douglas C. "Active Object" (PDF). Retrieved 2007-02-02.