17

What is the goal of the Active object pattern? Can you show me any abstract example to understand it easily?

Bence Pattogato
  • 3,752
  • 22
  • 30
  • 2
    Active-object is the [Command Pattern](https://sourcemaking.com/design_patterns/command), implemented concurrently. – jaco0646 Jan 16 '17 at 13:29
  • See https://devmethodologies.blogspot.com/2017/11/active-object.html for an example of converting code using mutex to use Active Object. – Andrew W. Phillips Feb 02 '19 at 22:31

1 Answers1

26

The patern's static object diagram

The Active object pattern's goal is to separate the method calling from method execution. It is like a waiter in a restaurant, who just hands the orders from the customers to the chef.

When a customer orders some food from the waiter

the customer is the Client, the waiter is the Proxy

he writes it up on a paper (obviously doesn't start to cook it),

the paper is the MethodRequest, the table's number on the paper is the Future object

gives the paper to the chef, who decides which cook should prepare it (who has time).

the chef is the Scheduler who has a list of papers (ActivationList) and the cooks are the Servant-s

When the meal is ready, the cook places it on the serve bar and the waiter brings it to the customers table.

Client reads the method's result, from the Result object.

Bence Pattogato
  • 3,752
  • 22
  • 30
  • 2
    The restaurant analogy is good but not perfect. I think it would be better to say that the customer gives the *recipe* to the waiter for the cook to execute. In other words the client supplies the code (as a pointer to a function, lambda or closure dep. on the language) for the servant to execute. – Andrew W. Phillips Feb 02 '19 at 22:40
  • 2
    Also the proxy and scheduler are embellishments, not essential. The core of the idea is simply a queue of closures executed on different thread(s) to that of the client. – Andrew W. Phillips Feb 02 '19 at 22:41
  • 2
    @AndrewW.Phillips the client does not provide the executive code, only the request and maybe some params ("extras"). The recipe on how to execute, the business logic, is not provided - which is cruical. That would be injection. Although it is commonly used for asynchronous decoupling, it's initially more about separation of domains. – Zacharias Apr 30 '19 at 07:47
  • 1
    @Zacharias I think the whole point of active object (or command pattern) is that the client provides the code. You can call that injection if you like. – Andrew W. Phillips May 01 '19 at 00:46
  • 2
    @AndrewW.Phillips I'm sorry, but you are wrong. Command patterns should not allow injection of code either, you rather provide the parameters to be used. Commands/active objects are requested by the client, but provided by a factory, which knows solely about the executive code. If the caller provides executive code, it's called injection. – Zacharias May 02 '19 at 15:45
  • @AndrewW.Phillips, this thread is quite old, but nevertheless, I will write my comment, You are completely wrong. The whole point of an active object is to execute tasks on separate threads, which is very important on current processors, with several cores. What code the task executes does not matter for this design pattern. – Kalki70 Mar 17 '23 at 20:43
  • Thanks Zacharias and @Kalki70 - I am wrong again! I think I'll give up on these pattern things. – Andrew W. Phillips Jul 12 '23 at 12:51