Explain "Active-object" pattern
Asked Answered
A

1

17

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

Allowance answered 16/1, 2017 at 12:21 Comment(2)
Active-object is the Command Pattern, implemented concurrently.Cook
See devmethodologies.blogspot.com/2017/11/active-object.html for an example of converting code using mutex to use Active Object.Personification
A
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.

Allowance answered 16/1, 2017 at 12:21 Comment(7)
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.Personification
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.Personification
@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.Imaginative
@Imaginative 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.Personification
@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.Imaginative
@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.Kareem
Thanks Zacharias and @Kareem - I am wrong again! I think I'll give up on these pattern things.Personification

© 2022 - 2024 — McMap. All rights reserved.