Click here to Skip to main content
15,897,122 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have to implement this interface ICritterFactory but I am so confused. I know that I have to make a new class that has the same method but I'm trying to figure out how to do so.

The following is a definition of ICritterFactory:
SQL
public interface ICritterFactory
{
ICritterBrain[] CreateCritterBrains();
}


CreateCritterBrains, returns an array of objects that implement the interface ICritterBrain.

Edit:-

I have made this class to implement the interface.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CritterInterfaces;

namespace CritterWorld
{
    public class Createcritter : ICritterFactory
    {
        ICritterBrain[] array1 = new ICritterBrain[2];
        ICritterBrain[] CreateCritterBrains()
        {
            return array1;
        }
    }
}


is it right or am I missing something?
Posted
Updated 4-May-11 9:33am
v2

1. Broadly looks good to me. Does it compile? Have you tested it?

2. But you may want to consider whether your interface / class methods / member variables are public / protected / private - if in doubt make things explicit.

3. And while your array1 is good as an array its elements will be null until you assign objects to them. I assume ICritterBrain is also an interface - so you won't be able to instantiate it directly and will need a class (or classes) that derive from it.
 
Share this answer
 
Comments
blink0 4-May-11 15:53pm    
Thanks.

I think you got the info I am looking for.
I made a small change:
public ICritterBrain[] CreateCritterBrains()
and now it builds.

Regarding your Point(3).
Well I am making another class to implement ICritterBrain.
It's definition is as follows:
public interface ICritterBrain
{
string Name { get; }
string Creator { get; }
Image[] Images { get; }
ICritter Body { set; }
void Birth();
void Think();
void NotifyBlockedByTerrain();
void NotifyBumped(IWorldObject otherCritter);
void NotifyEaten();
void NotifyCloseToFood(IWorldObject food);
void NotifyCloseToCritter(IOtherCritter otherCritter);
}

Is it ok for me to make a new class that implements ICritterBrain and then store values in the objects returned by Createcritter ?

like
array1[1].Name = "Dave";

Many thanks.
NuttingCDEF 4-May-11 16:05pm    
OK, so you'll have something like:

public class MyCritterBrain : ICritterBrain
{
implementation of ICritterBrain members + anything else you need including a MyCritterBrain constructor
}

class SomeClass
{
public void AMethod()
{
Createcritter MyCreatecritter = new Createcritter (); // note that you need to define a Createcritter constructor for this

ICritterBrain[] MyCritterBrains = MyCreatecritter.CreateCritterBrains();

// could put these 2 lines in your implementation of CreateCritterBrains
MyCritterBrains[0] = new MyCritterBrain();
MyCritterBrains[1] = new MyCritterBrain();

MyCritterBrains[0].Name = "Dave";
MyCritterBrains[1].Name = "Jane";

etc.
}

}
blink0 4-May-11 16:06pm    
Thanks a lot mate.Cheers :)
The problem of the code sample in question is that interface method is not exposed to the class user.
There are two forms:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CritterInterfaces;
 
namespace CritterWorld
{

    public class CritterFactoryGood : ICritterFactory
    {
        ICritterBrain[] someArray = new ICritterBrain[2];
        ICritterBrain[] ICritterFactory.CreateCritterBrains() //explicit interface name
        {
            return someArray;
        }
    } //CritterFactoryGood


    public class CritterFactoryQuestionable : ICritterFactory
    {
        ICritterBrain[] someArray = new ICritterBrain[2];
        public ICritterBrain[] CreateCritterBrains() //public
        {
            return someArray;
        }
    } //CritterFactoryQuestionable

} //namespace CritterWorld


First form as in CritterFactoryGood is better because it is safer, does not create redundant access and encourage separation of interface and class references or structures. Consider this:

ICritterFactory ifactory = new CritterFactoryGood();
var result1 = ifactory.CreateCritterBrains();
CritterFactoryGood factoryInstance = new CritterFactoryGood();
//will not compile:
var result2 = factoryInstance.CreateCritterBrains();


With CritterFactoryQuestionable, both variants would compile. Also, with first form, a class can have two properties methods with identical signature and names implementing two different interfaces, importantly, two indexed "this" properties.

—SA
 
Share this answer
 
Comments
Pete O'Hanlon 5-May-11 4:15am    
Propose as answer.
Sergey Alexandrovich Kryukov 5-May-11 11:37am    
Thank you, Pete.
--SA
You need to add the interface as an implementation by using : ICritterFactory and adding the method in. Here's how you'd add it:
C#
public class CritterFactory : ICritterFactory // This is added to the 
//class definition
{
  public ICritterBrain[] CreateCritterBrains() // Implements method from the factory.
  {
  }
}
 
Share this answer
 
v2
Comments
blink0 4-May-11 15:35pm    
Thanks Pete.
I have done something similar.Is it right?
Pete O'Hanlon 4-May-11 15:43pm    
Seems reasonable.
Sergey Alexandrovich Kryukov 4-May-11 16:34pm    
Pete, I vote 4 this time because you show only one of the forms -- a public one.
For both forms, please see my Answer.
--SA
Pete O'Hanlon 4-May-11 16:43pm    
That's fine. You've given a good answer there. I'll just pop over and 5 your answer.
Sergey Alexandrovich Kryukov 4-May-11 21:28pm    
Thank you, Pete.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900