Page 1 of 1

Generic Item and Collection classes

Posted: Fri Jan 21, 2011 9:44 am
by Technical
I'm quite new to this concept, so can you please tell me is this a good realisation?

IItem Interface:

Code: Select all

interface IItem
{
	function Load();
	function Create();
	function Fill();
	function GetId();
	function Exist(Identifier $Identifier);
	function Exists();
}
Item Class:

Code: Select all

class Item implements IItem
{
	protected $Id;
	protected $Exists = FALSE;
	function __construct()
	{
		$Count = func_num_args();
		if($Count == 1 && func_get_arg(0) instanceof Identifier)
		{
			$this->Load(func_get_arg(0));
		} else {
			call_user_func_array(array($this, 'Create'), func_get_args());
		}
	}
	function Load() { }
	function Create() { }
	function Fill() { }
	function GetId()
	{
		return $this->Id;
	}
	function Exist(Identifier $Identifier)
	{
		$this->Id = $Id->Value;
		$this->Exists = TRUE;
	}
	function Exists()
	{
		return $this->Exists;
	}
}
ICollection Interface:

Code: Select all

interface ICollection
{
	function Listed();
	function Parameters();
	function Exists($Id);
	function Length();
}
Collection Class:

Code: Select all

class Collection implements ICollection
{
	public $Items = array();
	function __construct()
	{
		$Count = func_num_args();
		if($Count == 1 && func_get_arg(0) instanceof Identifiers)
		{
			$this->Listed(func_get_arg(0));
		} else {
			call_user_func_array(array($this, 'Parameters'), func_get_args());
		}
	}
	function __set($Name, $Value) { }
	private function Listed(Identifiers $Identifiers) { }
	private function Parameters() { }
	function Exists($Id)
	{
		return !empty($this->Items[$Id]);
	}
	function Length()
	{
		return count($this->Items);
	}
}

Re: Generic Item and Collection classes

Posted: Fri Jan 21, 2011 1:45 pm
by greyhoundcode
I'm asking only out of curiosity, but why have an interface with private methods?

Re: Generic Item and Collection classes

Posted: Fri Jan 21, 2011 2:23 pm
by John Cartwright
greyhoundcode wrote:I'm asking only out of curiosity, but why have an interface with private methods?
I've actually never come across anyone implementing an interface with anything other than public methods. It is usually up to the interface to determine the public methods, and the programmer to determine the inner workings.

I guess it would make sense if your classes are abstracted and you need to ensure they have certain internal methods, but I just don't see any reason to have to define private methods in your interface.

As to the classes themselves, I'm quite confused. What are the Listed(), Parameters(), Exist(), and Exists() methods for?

Re: Generic Item and Collection classes

Posted: Fri Jan 21, 2011 3:26 pm
by greyhoundcode
John Cartwright wrote:I guess it would make sense if your classes are abstracted and you need to ensure they have certain internal methods, but I just don't see any reason to have to define private methods in your interface.
That's what I was thinking. In C# for instance (unless I'm much mistaken) it isn't legal to define interface members as anything other than public - but I'm always interested to hear the thinking/rationale.

(Edit)
To clarify, rationale of why the OP decided to place private members in the interface, that is.

Re: Generic Item and Collection classes

Posted: Fri Jan 21, 2011 11:21 pm
by Technical
John Cartwright wrote:
greyhoundcode wrote:I'm asking only out of curiosity, but why have an interface with private methods?
I've actually never come across anyone implementing an interface with anything other than public methods. It is usually up to the interface to determine the public methods, and the programmer to determine the inner workings.

I guess it would make sense if your classes are abstracted and you need to ensure they have certain internal methods, but I just don't see any reason to have to define private methods in your interface.

As to the classes themselves, I'm quite confused. What are the Listed(), Parameters(), Exist(), and Exists() methods for?
Actually, I meant they are abstract classes, I never use them as they are, only for inheritance.

As for those methods:

1) Listed() method creates collection with specified IDs. For example, if I want to get information about all authors:

Code: Select all

$Authors = new UserCollection(new Identifiers($ContentCollection->Authors));
2) In the other hand, Parameters() fills collection with items by specified parameters, like count or search string:

Code: Select all

$UserCollection = new UserCollection(0, 10, 'abc'); // 10 users with 'abc' search string
3) Exist() method forces item to set $Exists property to TRUE, thus ensure it loaded from database. It used when collection is being filled. You can't afford 20 queries to fill collection with 20 items, right?

4) Exists() method is pretty obvious I think. For Item it returns TRUE if the Item object was loaded from database. For Collection it checks if there is item with specified ID.

Re: Generic Item and Collection classes

Posted: Fri Jan 21, 2011 11:36 pm
by Technical
Can you please tell what is exactly wrong? I already made these classes abstract.

Re: Generic Item and Collection classes

Posted: Sun Jan 23, 2011 11:15 am
by Jonah Bron
greyhoundcode wrote:... In C# for instance (unless I'm much mistaken) it isn't legal to define interface members as anything other than public ...
Same in Java.

Re: Generic Item and Collection classes

Posted: Mon Jan 24, 2011 1:17 am
by Technical
Fixed