I would like you to share your experience on the matter of using some common object by different components of a library (or an application). I'd like to understand, which practice is better, which way is more "ecological".
Let's imagine that we have some complex library that consists of a bunch of classes: Library, Library::Foo, Library::Foo::Bar and many other ones. An application that uses the library initializes a Library-classed object, at some stage this object initializes some quantity of Library::Foo-classed objects, they initialize Library::Foo::Bar-classed objects and so on.
And now let's imagine that there is the Library::Logger class that is being used by all components. It's being initialized only once by the constructor of the Library class and all other sub-classes need it for their logging purposed.
The question is: how to let them all have the reference to this object?
I see the following ways to acheive it:
Passing it to the constructor
When a Library-based object initializes a Library::Foo-classed object, it passes the reference to the logger to the newly-created object as a parameter:
$self->_set_logger(Library::Logger->new());
my $logger = $self->_get_logger;
my $foo = Library::Foo->new(logger => $logger);
So the Library::Foo-classed object could get the logger and even pass it to a Library::Foo::Bar-classed object when it's needed:
my $logger = $self->_get_logger;
my $bar = Library::Foo::Bar(logger => $logger);
Accessing it as a method
When a Library-based object initializes the Library::Foo-based object, it passes the reference to itself to the newly-created object:
$self->_set_logger(Library::Logger->new());
my $foo = Library::Foo->new(papa => $self);
So the Library::Foo-based object could access the logger this way:
$my logger = $self->_get_papa->_get_logger;
my $bar = Library::Foo::Bar(papa => $self);
It breaks encapsulation, but it also lets the Library::Foo-based object to get other data from its "papa".
Using a global variable
Also we can store the reference to the logger in some global variable, so any other object could access it this way:
$logger = $Library::_LOGGER;
Well, it really sucks, I know... :)
...
So, what do you think, which way seems to be better? What alternative way would you propose? How do you solve this problem for yourself? Would you pass the data to the "child" manually or would you let it to get the data from the "parent"?
Many thanks in advance!
There is no answer to your problem. It realy depends on your exact use-case. If (just guessing) you only want to have exactly one Logger in your Library, I think using the software pattern of a "singleton" might exactly fit. E.g.
package Library::Logger;
use strict;
my $logger;
sub new {
unless ($logger) {
.... initializing $logger ...
}
return $logger;
}
and in each of your sublclasses you can access it by
Library::Logger->new()->...
HTH
Georg
Related
I am writing a file manager for my Perl application. Information about each file is kept as an object. When I remove a file, I'd like to change the corresponding object's class to RemovedFile. For this class, call to any valid method of "File" would return a fatal error and a stack trace. This would be to catch cases where some stale reference to this object is kept (while it shouldn't).
I thought about two ways I could implement this:
"RemovedFile" inherits from "File" and redefines all its methods with a call to fatal error. Downside to this is that if I add a new method to "File" I need to add it to "RemovedFile" as well.
Adding a call to some empty method to every method of "File". "RemovedFile" would redefine this one method to report fatal error. (See code below for an example of what I mean). Downside to this is that every method of "File" would have to be bothered with calling the "remove_guard" in the beginning which IMO is not very clean.
# Inside File class:
sub any_method_of_file_class {
$self->_removed_guard();
#rest of code
}
sub _removed_guard {
#do nothing
}
# Inside RemovedFile class redefine only _removed_guard:
sub _removed_guard {
$self->{logger}->fatal_with_stack_trace();
}
I wanted to ask if there is any better way to implement this kind of behaviour in Perl?
For example, could I use some tricks to first list and then dynamically redefine all methods of a parent class without specifying their exact names?
You can define the RemovedFile class without any relation to the original File class. In RemovedFile, use AUTOLOAD to handle any method.
If you want to redefine (override) all the methods of the parent class, don't inherit from the parent class. You can use the same interface, but you don't need the connection to some module you are going to completely ignore.
I think I'd probably have a factory method in File that returns a new object for RemovedFile:
my $removed_file_obj = $file->remove;
That new class only knows what it needs to know about removed files. The remove can do whatever cleanup you require.
Then, when you are dealing with lists of objects, some of which may be File and some of which may be RemovedFile, filter the ones you want. This is outside of the class definitions because the class only defines the behavior of the objects and not how we employ the objects.
Here's one way to check by the object type, maybe even with the new isa feature:
use v5.32;
use experimental qw(isa);
foreach my $file ( #files ) {
# next if $file->isa( 'RemovedFile' );
next if $file isa 'RemovedFile';
...
}
But, you probably shouldn't check what something is. Check what it can do. Since you haven't inherited a bunch of methods that don't do anything, can should return false for that:
foreach my $file ( #files ) {
next if $file->can( 'some_method_not_in_RemovedFile' );
...
}
I wish I knew the reference or document that told me that this is the best way to create a new object in Perl:
sub new {
my $package = shift;
my $class = ref($package) || $package
This is how I've been creating objects for years but now I'm wondering why go to the trouble? With the exception of some edge cases why not simply do the following:
sub new {
shift; # get rid of the object or package name
my $class = __PACKAGE__;
Are there any issues with simply doing using __PACKAGE__ when there's no special reason to try to detect what namespace the 'new' method is being called in?
Inheritance
# classA.pm
package ClassA;
sub new { $pkg = ref($_[0]) || $_[0] ; bless { foo => 42 }, $pkg }
# classB.pm
package ClassB;
use parent 'ClassA';
sub foo { ... }
# main.pl
use ClassA;
use ClassB;
$B = ClassB->new();
print $B->foo();
In this example, ClassB inherits methods from ClassA, including its constructor. But we still want to identify the object as belonging to ClassB, so the constructor in ClassA must respect the name of the reference passed to its constructor.
Easier and safer than
$B = bless { ClassA->new(), "ClassB" }; # or
$B = bless { ClassB->new(), "ClassA" };
or adding a pass-though constructor in ClassB.
package ClassB;
use parent 'ClassA';
sub new { bless { ClassA::new(#_), __PACAKGE__ } }
The class name is already the first argument to the constructor, so why not use that? You don't need to reach for anything and if you decide that the situation is more complex than you originally envisioned, you haven't artificially caused a speed bump. This code works with or without inheritance:
sub new {
my( $class, #args ) = #_;
...
bless {...}, $class;
}
Consider with anything that you program how much you'd have to change if the code situation changed. Maybe you add a couple of extra steps that you don't immediately need, but that keeps the code flexible for those times when you realize you actually needed those cases. In your case, you actually have to do extra work to ignore the invocant argument that Perl specifically provides to tell you which class is trying to mak a new object.
For example, you create your class, it works for you and does its job well. It works so well that you share it and someone else uses it and they are mostly happy with it for awhile until they need this one little modification. They should be able to subclass your module (so, no changes to it or for you) to extend or override a method. But, your code doesn't allow that because you blessed the object in a more restrictive way that didn't add any benefit.
That first bit of code in your question is using the first argument too, but it's not really a prescription for constructors. It's doing an extra thing by allowing an already existing object create a new one. The ref extracts the blessed package name from the object and uses that for the new object.
I don't particularly like that way of creating new objects and I think the interface will likely confuse people. What should happen when you call new on an existing object? Do you clone the existing one? If so, there are better names, such as clone (Ruby uses dup). Do you get a completely fresh object? If so, why do you go through an existing object to get one completely unrelated to it?
There was a time that many examples of OO Perl showed that same sort of constructor, and it was copy and pasted into many places. But we've learned a lot since then. Maybe you have a good answer why you'd allow that, but so far I haven't heard a compelling one.
I have a class:
class Hello {
function doSomething(&$reference, $normalParameter) {
// do stuff...
}
}
Then I have a controller:
class myController {
function goNowAction() {
$hello = new Hello();
$var = new stdClass();
$var2 = new stdClass();
$bla = $hello->doSomething($var, $var2);
}
}
The "goNow" action I call using my tests like so:
$this->dispatch('/my/go-now');
I want to mock the "doSomething" method so it returns the word "GONOW!" as the result. How do I do that?
I've tried creating a mock
$mock = $this->getMock('Hello ', array('doSomething'));
And then adding the return:
$stub->expects($this->any())
->method('discoverRoute2')
->will($this->returnValue("GONOW!"));
But I'm stumped as to how to hook this up to the actual controller that I'm testing. What do I have to do to get it to actually call the mocked method?
You could create a mock for the reference, or if it is just a simple reference as your code shows, send a variable. Then the normal mock call may be called and tested.
$ReferenceVariable= 'empty';
$mock = $this->getMock('Hello ', array('doSomething'));
$stub->expects($this->any())
->method('discoverRoute2')
->will($this->returnValue("GONOW!"));
$this->assertEquals('GONOW!', $stub->doSomething($ReferenceVariable, 'TextParameter'));
Your example code does not explain your problem properly.
Your method allows two parameters, the first being passed as a reference. But you create two objects for the two parameters. Objects are ALWAYS passed as a reference, no matter what the declaration of the function says.
I would suggest not to declare a parameter to be passed as a reference unless there is a valid reason to do so. If you expect a parameter to be a certain object, add a typehint. If it must not be an object, try to avoid passing it as a reference variable (this will lead to confusing anyways, especially if you explicitly pass an object as a reference because everybody will try to figure out why you did it).
But your real question is this:
But I'm stumped as to how to hook this up to the actual controller that I'm testing. What do I have to do to get it to actually call the mocked method?
And the answer is: Don't create the object directly in the controller with new Hello. You have to pass the object that should get used into that controller. And this object is either the real thing, or the mock object in the test.
The way to achieve this is called "dependency injection" or "inversion of control". Explanaitions of what this means should be found with any search engine.
In short: Pass the object to be used into another object instead of creating it inside. You could use the constructor to accept the object as a parameter, or the method could allow for one additional parameter itself. You could also write a setter function that (optionally) gets called and replaces the usual default object with the new instance.
What's the best practice for implementing Singletons in Perl?
You can use the Class::Singleton module.
A "Singleton" class can also be easily implemented using either my or state variable (the latter is available since Perl 5.10). But see the #Michael's comment below.
package MySingletonClass;
use strict;
use warnings;
use feature 'state';
sub new {
my ($class) = #_;
state $instance;
if (! defined $instance) {
$instance = bless {}, $class;
}
return $instance;
}
If you're using Moose, then MooseX::Singleton. Its interface is compatible with Class::Singleton.
Singleton Summary:
Most of the time a normal object will work.
Be careful with singletons.
Localize interaction as much as possible
While singletons are a nice idea, I tend to just implement a normal object and use it. If it is critical that I only have one such object, I'll modify the constructor to throw a fatal exception when the second object is created. The various singleton modules don't seem to do much besides add a dependency.
I do this because it is easy, it works, and when in some weird future I need to work with a second object in my app, the changes are minimized.
I also like to localize interaction with my 'singleton' objects--keep interaction in as few places as possible. So instead of every object having direct access to the singleton, I mediate all interaction through my "Application" object. Whenever possible, the application object gets data from the 'singleton', and passes it as a parameter to the method in the other objects. Responses from other objects may also be munged and passed to the 'singleton'. All this effort helps when I need to make changes in the 'singleton' object, and when I want to reuse other objects in another app that may not need or be able to use the original 'singleton' object.
I'd like to create several instances of a class in CodeIgniter. I have created my class as a library, but cannot figure out the syntax to use to create more than one instance.
From the CodeIgniter users guide:
CI Users Guide: Loader Class
Assigning a Library to a different object name
If the third (optional) parameter is
blank, the library will usually be
assigned to an object with the same
name as the library. For example, if
the library is named Session, it will
be assigned to a variable named
$this->session.
If you prefer to set your own class
names you can pass its value to the
third parameter:
$this->load->library('session', '',
'my_session');
Session class is now accessed.
using:
$this->my_session
I think that's what you're looking for.
I know this thread is long passed, but it was one of the questions I came across while looking for my answer. So here's my solution...
It's PHP. Create your class as a library, load it using the standard CI Loader Class, but use it like you would in a regular PHP script.
Build your class:
class My_class {
var $number;
public function __construct($given_number){
$number = $given_number;
}
public function set_new_num($given_number){
$number = $given_number;
}
}
Load it:
// This will load the code so PHP can create an instance of the class
$this->load->library('My_class');
Then instantiate and use the object where needed:
$num = new My_class(24);
echo $num->number;
// OUTPUT: 24
$num->set_new_num(12);
echo $num->number;
// OUTPUT: 12
The only time I use $this->my_class is to make calls to static functions that I code.
Sorry for reviving this topic but I think I might have something reasonable to add.
You can do this to add multiple instances of a class. I don't know if it violates Codeigniter standard usage anyhow but seems more Codeigniterish than loading a library (which creates $this->library_name which isn't used) and then making 2 MORE instances with the "new" keyword.
$this->load->library( 'my_library', '', 'instance1' );
$this->load->library( 'my_library', '', 'instance2' );
$this->instance1->my_class_variable = 1;
$this->instance2->my_class_variable = 2;
echo $this->instance1->my_class_variable; // outputs 1
echo $this->instance2->my_class_variable; // outputs 2
I use this in my code to generate different menus. I have a "menu" class and different instances for each menu, with different menu items in each.