MelodyMaker
Programmer
Hi,
I would like to know if there's a way to avoid bad repetition like this one (I use PHP):
I have a User class:
Of course, there are also three "remove" functions. It looks like very bad, to me...
As you can see, the methods are very similar and this is very annoying.
So, I decided to create and abstract class called "profileItem" which is a superclass of Photo, Description, and Address Class.
The abstract ProfileItem Class:
The Photo class:
Address and Descriptions classes are similar...
The problem is that I don't know how to integrate them with the user class...I mean, I need, for example an addItem method which can be used for Description, Photo, and Address.
But at some point, there will be a conditional statement, something like:
if ($item instance of "Address") then "the address should be added to $this->addresses
if ($item instance of "Photo") then "the photo should be added to $this->photos
if ($item instance of "Description") then "the description should be added to $this->descriptions
...
Is there a clean, elegant way to do this (maybe with the help of a pattern...don't know)?
Thank you very much in advance!
Davide
I would like to know if there's a way to avoid bad repetition like this one (I use PHP):
I have a User class:
Code:
class User{
private $conf;
private $photos = Array();
private $addresses = Array();
private $descriptions = Array();
...
public function __construct($userData){
$this->conf = Configuration::getInstance();
...
}
public function addPhoto(Photo $photo){
$photoNumber = $this->getPhotosAmount();
if( $photoNumber < $this->conf->userMaxPhotos){
$photo->setIdUser($this->u_idUser);
$photo->setNumber(++$photoNumber);
$this->photos[] = $photo;
return true;
}
return false;
}
public function addDescription(Description $description){
$descNumber = $this->getDescriptionsAmount();
if( $descNumber < $this->conf->userMaxDescriptions){
$description->setIdUser($this->u_idUser);
$description->setNumber(++$descNumber);
$this->descriptions[] = $description;
return true;
}
return false;
}
public function addAddress(Address $address){
$addressNumber = $this->getAddressesAmount();
if( $addressNumber < $this->conf->userMaxAddresses){
$address->setIdUser($this->u_idUser);
$address->setNumber(++$addressNumber);
$this->addresses[] = $address;
return true;
}
return false;
}
...
}
Of course, there are also three "remove" functions. It looks like very bad, to me...
As you can see, the methods are very similar and this is very annoying.
So, I decided to create and abstract class called "profileItem" which is a superclass of Photo, Description, and Address Class.
The abstract ProfileItem Class:
Code:
abstract class AbsProfileItem{
private $number = 1;
private $idUser = 0;
public function getIdUser(){
return $this->idUser;
}
public function setIdUser($idUser){
$this->idUser = $idUser;
}
public function getNumber(){
return $this->number;
}
public function setNumber($number){
if($number > 0){
$this->number = $number;
}
}
}
The Photo class:
Code:
class Photo extends AbsProfileItem {
private $url = "";
private $caption = "";
public function __construct($url,$caption){
$this->url = $url;
$this->caption = $caption;
}
public function getUrl(){
return $this->url;
}
public function setUrl($url){
$this->url = $url;
}
public function getCaption(){
return $this->caption;
}
public function setCaption($caption){
$this->caption = $number;
}
}
Address and Descriptions classes are similar...
The problem is that I don't know how to integrate them with the user class...I mean, I need, for example an addItem method which can be used for Description, Photo, and Address.
But at some point, there will be a conditional statement, something like:
if ($item instance of "Address") then "the address should be added to $this->addresses
if ($item instance of "Photo") then "the photo should be added to $this->photos
if ($item instance of "Description") then "the description should be added to $this->descriptions
...
Is there a clean, elegant way to do this (maybe with the help of a pattern...don't know)?
Thank you very much in advance!
Davide