CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    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?

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    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

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  5. #5
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Lightbulb Re: Has,Contains,Is a part of(UML desingning in C#)

    I learnt many things about nested classes
    thanks friends.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Has,Contains,Is a part of(UML desingning in C#)

    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.

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    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?
    Last edited by mehdi62b; March 10th, 2005 at 05:24 AM.

  8. #8
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Question 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.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  9. #9
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Has,Contains,Is a part of(UML desingning in C#)

    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.

  10. #10
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Lightbulb Re: Has,Contains,Is a part of(UML desingning in C#)

    boudino,
    you were right!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured