Has,Contains,Is a part of(UML desingning in C#)
Hi Experts,
I want to implement HAS in C#,I think I should use private nested class for this relation,or whetere other things are possible?
for example a car Has door,motor,pedal,...
Code:
public class car
{
private Motor motor=null;
class Motor{
}
private Pedal pedal=null;
class Pedal{
}
private Door door=null;
class Door{
}
}
am I right?
if I declare Door,Pedal,Motor classes outside the car class,does it make sence?
Re: Has,Contains,Is a part of(UML desingning in C#)
Quote:
Originally Posted by mehdi62b
if I declare Door,Pedal,Motor classes outside the car class,does it make sence?
Let's take the motor for example: can you use it for something else than a car? For instance a byke has also a motor, so perhaps you can reuse the Motor class. You can provide a generic Moror class (perhaps an interface) and derive/implemlent it in two specialized class, CarMotor and BykeMotor. In this case, yes, it makes a lot of sense to put it outside.
You can apply the same reason to the rest of the car's parts.
May I ask what data will the Pedal or Door encapsulate? What fields/methods/properties?
Re: Has,Contains,Is a part of(UML desingning in C#)
Code:
abstract class Motor{
}
public class car
{
private CarMotor motor=null;
class CarMotor:Motor{
}
private Pedal pedal=null;
class Pedal{
}
private Door door=null;
class Door{
}
}
yes,I just wanted to know for Has we should declare them as nested classes or It is not important that much?
Quote:
Originally Posted by cilu
May I ask what data will the Pedal or Door encapsulate? What fields/methods/properties?
I summarized my example
Re: Has,Contains,Is a part of(UML desingning in C#)
I think - you should not use nested classes for this.
1.) Not all modeling concepts must have its counterpart in programing language.
2.) Nested class breaks encapsulation, because it can access private members of its outer class. In your case: pedals will able to manipulate the whole car, althought they should be able to operate only breaking system.
Re: Has,Contains,Is a part of(UML desingning in C#)
I learnt many things about nested classes
thanks friends.
Re: Has,Contains,Is a part of(UML desingning in C#)
Quote:
Nested class breaks encapsulation, because it can access private members of its outer class. In your case: pedals will able to manipulate the whole car, althought they should be able to operate only breaking system.
This is a very good point.
Quote:
I summarized my example
I know that. I was currios what functionality do you want to give to Pedal, for example. If it isn't too much, perhaps it doesn't worth creating another class. But that depends on details you have not provided.
Re: Has,Contains,Is a part of(UML desingning in C#)
Quote:
Originally Posted by boudino
Nested class breaks encapsulation, because it can access private members of its outer class. In your case: pedals will able to manipulate the whole car, althought they should be able to operate only breaking system.
I have a question here,I think nested classes just can access static private memebers of its outer class(not non satatic because we should instansiate it)
this is my code,
Code:
abstract class Motor
{
}
abstract class Pedal
{
}
public class car
{
private CarMotor motor=null;
class CarMotor:Motor
{
int speed=0;
public int Speed
{
get
{
return speed;
}
}
public void IncreaseSpeed(int a)
{
speed+=a;
}
}
private GasPedal pedal=null;
class GasPedal:Pedal
{
bool released;
public bool Released
{
get
{
return released;
}
set
{
released=value;
}
}
}
private Door door=null;
class Door
{
}
public void PushPedal(int a)
{
pedal.Released=false;
motor.IncreaseSpeed(a);
}
}
I have a PushPedal method in car class,it was my designing,ofcource I could declare those nested classes outside the car class but because I just want to let car class use CarMotor class I used nested classes.
I dont know it was a good idea or not?
Re: Has,Contains,Is a part of(UML desingning in C#)
Quote:
Originally Posted by mehdi62b
I have a question here,I think nested classes just can access static private memebers of its outer class(not non satatic because we should instansiate it)
You are wrong. You can access private instance members of outer class:
Code:
public class car
{
private Door door=null;
class Door
{
public void SetTo(car aCar)
{
aCar.door = this;
}
}
}
[QUOTE=mehdi62b]I have a PushPedal method in car class,it was my designing,ofcource I could declare those nested classes outside the car class but because I just want to let car class use CarMotor class I used nested classes.
I dont know it was a good idea or not?[/QOUTE]
I am sorry, but I don't sitll undestrood why do you need nested classes. Having no nested classes, car class will still be able to use CarMotor.
Re: Has,Contains,Is a part of(UML desingning in C#)
:thumb: I thought you meant something like this,
Code:
public class car
{
private Door door=null;
class Door
{
car c=new car();
//other methods
}
}
this will give StackOverFlow exception (but inside a method or if my memebr is satic,no problem)
about nested classes because I dont want to let other classes in my assembelly have an access CarMotor,just car class should access CarMotor.
Re: Has,Contains,Is a part of(UML desingning in C#)
boudino,
you were right!
:wave: