- Perl ›
- Object Oriented ›
- "Monotype" and "Human type"
"Monotype" object and "Human type" object
In object-oriented programming, objects are not "things". There are two types of objects: "things" type objects and "people" type objects.
1. "Things" type object
A "thing" type object is an object that represents a "thing" . The following is this object, but it has title and price as attributes.
my $book = Book->new(title =>'a', price => 2300);
Such "things" type objects are constructors that set book properties such as titles and prices . You can think of a "thing" type object as something very close to a hash . Monotype objects represent structured data.
my $book = {title =>'a', price => 2300};
2. "Human" type object
A "human" type object is an object that represents "human" . Below are the objects that parse the file. It has encoding (circular coding method) and err_raise (raises an exception if parsing fails) as attributes.
After that, a method called parse that parses the file will be called.
my $file_parser = FileParser->new(encoding =>'utf8', err_raise => 1); $file_parser->parse($file);
Such "human" objects often specify in the constructor the options used by subsequent methods . Think of a "human" object as something very close to a function .
my $opt = {encoding =>'utf8', err_raise => 1}; parse_file ($file, $opt);
3. Class design guidelines
- First, be aware of whether you want to create a "thing" type object or a "human" type object.
- If you want to represent data, create a "thing" typeclass
- If you want to call a method with options, create a "human" typeclass
- If convenience is superior, it may be a hybrid class in which the "thing" type is mixed with the "human" type.