in reply to Role::Tiny: When to use apply_roles_to_object?
One example might be you have a Backend class where your app sends all its data. You might instantiate the backend object like this:
my $backend = Backend->new(); Role::Tiny->apply_roles_to_object( $backend, qw( Backend::Storage::PostgreSQL Backend::Log::Debug Backend::Interf +ace::TCP ), );
And now your backend object will store its data in PostgreSQL, and spit out debugging information to its log file, and listen on a TCP port for instructions.
If you have eight different possibilities for storage (different types of database, XML files, JSON files, etc), 4 different options for logging, and 4 different interfaces (TCP, Unix Sockets, Command Line, and HTTP), that gives 128 possible combinations. You don't really want to have to create classes for Backend::PostgreSQL_Debugging_TCP, Backend::MySQL_Debugging_TCP, Backend::PostgreSQL_Debugging_HTTP, etc. Instead you just compose the combination of roles you want.
(In this particular example, there's an argument for making more use of delegation instead of composition, but I digress.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Role::Tiny: When to use apply_roles_to_object?
by LanX (Saint) on May 03, 2019 at 17:57 UTC | |
by tobyink (Canon) on May 04, 2019 at 07:39 UTC | |
by LanX (Saint) on May 04, 2019 at 16:36 UTC | |
by 1nickt (Canon) on May 03, 2019 at 18:42 UTC | |
|
Re^2: Role::Tiny: When to use apply_roles_to_object?
by karlgoethebier (Abbot) on May 05, 2019 at 11:39 UTC |