C# classes - composition

I created  {Class Car} which contains {class Engine}. A car has an engine...so I tried to represent "Has a " relationship between  {Class Car} and {class Engine}...in other words I tried to represent "composition" design pattern (it's definitely not inheritance).
The code is shown below.

 Is there a way to improve (optimize) the code to better represent "composition" design pattern ??

-------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CarTest
{

    class Car
    {
        private string color;
        protected Engine _theEngine;

        // 1st constructor
        public Car(string color)
        {
            this.color = color;
        }
       
        // 2nd constructor
        public Car(string color, Engine _engine)
        {
            this.color = color;

            _theEngine = _engine;
        }

        public string DescribeCar()
        {
            return "This car is " + Color;
        }

        public string Color
        {
            get { return color; }
            set { color = value; }
        }

// -------------------------------------------
        //public class Engine
        internal class Engine
        {
            private int horsePower;

            public Engine(int _horsePower)
            {
                horsePower = _horsePower;
            }

            public void DescribeEngine()
            {
                Console.WriteLine("Cool Engine; horsePower =" + horsePower);
            }

            public int HorsePower
            {
                get { return horsePower; }
                set { horsePower = value; }
            }
        } // end of class Engine

// -------------------------------------------

    } //end of class Car
       
   
    class Program
    {
        static void Main(string[] args)
        {
            Car car;
            car = new Car("Red");
            Console.WriteLine(car.DescribeCar());

            Car.Engine e= new Car.Engine(3000);
            e.DescribeEngine();
           
            // Create 2nd instance
            Car.Engine eng = new Car.Engine(9000);
            Car MyCar = new Car("Silver", eng);

            Console.WriteLine(MyCar.DescribeCar());
            eng.DescribeEngine();


            Console.ReadLine();
        }
    }
}

-------------------------------------------------

Thanks
niceguy971Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sudhakar PulivarthiProject Lead - EngineeringCommented:
Hi,

Engine class is tightly coupled with Car, but in nature - Engine is a base class and has some specific properties which pertain to car and not all class of vehicles will have same engine properties.

Hence Engine class should be an abstracted class and a Car Engine derived class will have to be used in Car class for better design. This helps future extensibility as you need to extend for different class of vehicle.
0
multithreadingCommented:
Yes, there is a way to improve this class relationship: Get rid of the Engine class. This car does not "have" an engine. It "has" horsepower. Real cars have engines. This is not a real car.

When you start designing the classes for your domain, making a class for each noun is the right starting point. It makes perfect sense that you started by thinking of having an Engine class, which would be referenced by your Car class, because Cars have engines. That is the right starting point, but it isn't the right ending point in this case.

If you have a class which is really just a glorifed immutable scaler value, with no behavior and no mutable state, it should be a candidate for removal. Real engine classes might have formulas for their performance response, fuel consumption at different speeds, number of cylinders, torque, etc. etc. If you had a situation like that it would make sense to keep the Engine class. When designing class relationships the question is not really "what do cars have"? The real question is "what do cars have that I care about in this solution".

Now, setting aside the question of whether the Engine class should exist or not, there are problems in your code that would be picked up by a compiler. For instance
protected Engine _theEngine;

Open in new window

only allows access from the class or a derived class, but you are accessing that field from class Program. There is no sense in desk checking for things that the compiler will pick up in this venue, so I'll stop there.
0
niceguy971Author Commented:
Hi multithreading,

So If I add some fields and methods to the Engine class... for example a method to calculate fuel consumption at different speeds; number of cylinders; engine_model and so on..in such case it makes sense to keep the Engine class correct??
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

multithreadingCommented:
Yes. You don't need allof those to justify the class, but at least something more than "horsepower".
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jonnidipCommented:
>> ..in such case it makes sense to keep the Engine class correct??
I agree with you, by keeping different "things" separated.
A car logically contains an engine, and obviously an engine does not have only "hp".
It all depends on your environment, on your amount of data.
If you have only a few "car models" you don't need to make it complex.
But if you think to expand your needs, then I would suggest you to keep using classes in this way.

Regards.
0
niceguy971Author Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.