Two part design question for an object that describes a Vehicle.

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1251 Views - Last Post: 05 October 2010 - 09:22 AM Rate Topic: -----

#1 CodeGuru1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 10

Two part design question for an object that describes a Vehicle.

Posted 01 October 2010 - 07:32 AM

Lets say I'm a class that describes a vehicle.

class Vehicle
{
	// Now, lets say I want to have engine info
	Engine Engine;

	// Lets also say that I want to know the make/model
	MakeModel MakeModel;

	void SomeFunc()
	{
		// Suppose I knew what type of engine it was
		Engine = Engine.vtec3;
		// Now, by setting the engine to that type, I should
		// be able to have info like:
		MessageBox.Show(Engine.NumberOfCylinders);
		// How would you design an engine class so that it
		// populates all needed data when set? Imagine there
		// are 20 or so various parameters for each engine type.

		// My second question regards the make/model.
		// I should be able to do either of the following:
		MakeModel = Honda;
		// OR
		MakeModel = Honda.Civic;
		// I'm not sure how to design this pattern in. :(/>
	}
}

class Engine
{
}

class MakeModel
{
}



Can anyone please help point me in the right direction? Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Two part design question for an object that describes a Vehicle.

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,356
  • Joined: 02-June 10

Re: Two part design question for an object that describes a Vehicle.

Posted 01 October 2010 - 07:43 AM

Engine = Engine.vtec3;



You have coded that Engine is a class. But this looks more like an enumeration (enum). You have to make a *new* engine if it is a class.

Engine myEngine = new Engine("vtec3"); //for example


May I suggest a tutorial before you try building your own from scratch. It's obvious you are not familiar with the language of C# or the principals of classes.


Bulding an application - Part 1

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a car sales lot database application, your own web browser.

This post has been edited by tlhIn'toq: 01 October 2010 - 07:47 AM

Was This Post Helpful? 0
  • +
  • -

#3 CodeGuru1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 10

Re: Two part design question for an object that describes a Vehicle.

Posted 01 October 2010 - 07:59 AM

Thank you for the reply.

I understand your point of view, however...

I know that it looks like an enum, it also looks like a static method.

To give you an example of that approach:

class Engine
{
	private Engine(int NewNumberOfCyl)
	{
		NumberOfCyl = NewNumberOfCyl;
	}

	public int NumberOfCyl;

	public static Engine VTEC3
	{
		get
		{
			return new Engine(4);
		}
	}	
}



The problem with that approach is as you add parameters to the engine class, you have an increasingly large constructor. As you can imagine it gets messy pretty quickly.

I've been reading a lot about obscure design paterns I have never used but I can't seem to figure out these two questions. I was hoping a fresh set of eyes might see something obvious I'm missing.

Thanks for your time.
Was This Post Helpful? 0
  • +
  • -

#4 lordofduct  Icon User is online

  • I'm a cheeseburger
  • member icon


Reputation: 2506
  • View blog
  • Posts: 4,615
  • Joined: 24-September 10

Re: Two part design question for an object that describes a Vehicle.

Posted 01 October 2010 - 08:14 AM

1st question, use the factory design pattern:

http://en.wikipedia...._method_pattern

There's no one exact way to implement it. You can have it as a static method interface on the root class. You can have a special class designed to act as the factory. Instead of a constructor dealing with some overly complex initialization... the factory instead sets and modifies all the values to its specific needs.



2nd question, the factory design pattern again
Was This Post Helpful? 0
  • +
  • -

#5 CodeGuru1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 10

Re: Two part design question for an object that describes a Vehicle.

Posted 01 October 2010 - 08:49 AM

Thank you.

Can you give me a short example of how to implement the second?

The way I see it, Honda is a manufacturer, and Civic is a model, that can only apply to the Honda manufacturer. That would make them two different object types.

So, I'm not sure how I can have both of the following work:
MakeModel = Honda;
MakeModel = Honda.Civic;

I think the appropriate way would be:
Make = Honda;
Model = Honda.Civic;

So, do I make Civic an object of type Model that is a static member of the Honda class? If that's the case, then I'd need a class for every type of make, when the class should really just be Make.

I just can't see how to get there. :(
Was This Post Helpful? 0
  • +
  • -

#6 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 982
  • View blog
  • Posts: 969
  • Joined: 30-September 10

Re: Two part design question for an object that describes a Vehicle.

Posted 01 October 2010 - 08:49 AM

Hello there,

I agree with lordofduct on this one. The factory design pattern certainly seems to be the way to go.

In the case of the engines, you would simply create different engine objects, each one representing a different type of engine. You should tie these together by have them derive from an abstract class (or possibly an interface) that will ensure all the different types of engine share the essential characteristics and behaviour that you want each engine to have. These will be your products that your factory will create.

Now, you can create a factory class that holds a createEngine method (the factory method), for example. Keeping it quite simple, you could pass a string to the constructor of this factory class when you create the new factory object that tells the factory object which engine you want constructing. Then, you would call the create engine method and, in there, you could have a conditional that selects and returns the required engine based on the string you gave the factory.

I have only been programming for a year, so people feel free to correct me on anything I have said. I am still very much learning myself.

That is how I would consider doing it anyway. Please post back with any queries you have reagrding what I have just said, CodeGuru1. You'd probably be helping me develop my understanding too!

Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,356
  • Joined: 02-June 10

Re: Two part design question for an object that describes a Vehicle.

Posted 01 October 2010 - 09:03 AM

using System;
using System.Collections.Generic;
using System.Text;

namespace myCoolProgram
{
    class Make
    {
        public Model { get; set;}
        public Engine { get; set;}
    }

    class Honda : Make
    {
        public enum Model
        {
            unknown = 0,
            Civic = 1,
            Accord = 2,
            CRV = 3
        }
    }
}

// Or


namespace myCoolProgram
{
    class Make
    {
    }

    class Model
    {
        public int doors { get; set; }
    }

    class Engine
    {
        public int Cylinders { get; set; }
    }

    class Honda : Make
    {
        public class Accord : Model
        {
            public Accord()
            {
                doors = 4;
            }
        }

        public class Civic : Model
        {
            public Civic()
            {
                doors = 2;
            }
        }

        public class FourBanger : Engine
        {
            public FourBanger()
            {
                Cylinders = 4;
            }
        }
    }
}



I prefer the second example.

This way you can Make a new car as such

Honda myNewHonda = new Honda();
myNewHonda.Model = new Accord();
myNewHonda.Engine = new Fourbanger();

This post has been edited by tlhIn'toq: 01 October 2010 - 09:04 AM

Was This Post Helpful? 1
  • +
  • -

#8 CodeGuru1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 10

Re: Two part design question for an object that describes a Vehicle.

Posted 01 October 2010 - 09:11 AM

Ok, very interesting.

This will take me some time to go over. I'll probably spend the weekend going over various implementations. I'll definitely report back.

Thank you!!!
Was This Post Helpful? 0
  • +
  • -

#9 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 982
  • View blog
  • Posts: 969
  • Joined: 30-September 10

Re: Two part design question for an object that describes a Vehicle.

Posted 01 October 2010 - 09:47 AM

Hello again,

You could possibly use the factory pattern again, like lordofduct said, for the make model thing. This time though, you could have more than one factory, all of which derive from a factory abstract class that defines the createModel method signiture. One factory for each of the manufacturers. Then, to create a Honda Civic for example, you would create a honda factory passing in the string 'civic', then you could call the createModel method which uses the string you passed in to create a new HondaCivic object.

Alternatively, you could just create honda objects from the factory, but inside them honda objects, you would have a variable (enumeration perhaps) holding the model of the car, which the factory would set for you. So, in this instance, you would create a honda factory, pass in the model you wanted, call the createModel method, but then, the factory would, instead of creating a brand new HondaCivic object, it would create a new honda object, but set it's model variable to 'civic'.

Just some more ideas for you to consider. Give tlhIn'toq's ideas some serious thought though, as he is far far more experienced than I am :).

Thanks.

This post has been edited by CodingSup3rnatur@l-360: 01 October 2010 - 10:58 AM

Was This Post Helpful? 0
  • +
  • -

#10 CodeGuru1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 10

Re: Two part design question for an object that describes a Vehicle.

Posted 03 October 2010 - 09:00 PM

Ok, I spent the weekend working on this.

The goal here isn't to design reusable code, or objects. Its to organize static data. Normally this would go into a database, but that is not an option in this case.

Lets assume that the Make object contains the following variables and methods:
public int id;
private string Name;
public override string ToString() { return Name; }

Lets assume that Model contains the same variables and methods.

Lets also assume for the life of this project, there may be 100+ makes, and 1000+ models. Think of motors.ebay.com or autotrader.

    class Make
    {
        public int id;
        internal string _name;
        public override string ToString()
        {
            return _name;
        }
    }

    class Honda : Make
    {
        Honda()
        {
            id = 1;
            _name = "Honda";
        }

        public enum Model
        {
            unknown = 0,
            Civic = 1,
            Accord = 2,
            CRV = 3
        }
    }


Ok, the problem with the above is, You cannot have a generic model variable and use it as such:
if (Model == Honda.Civic || Model == Toyota.Prius)

Now, looking at the second example:

    class Make
    {
        public int id;
        internal string _name;
        public override string ToString()
        {
            return _name;
        }
    }

    class Model
    {
        public int id;
        internal string _name;
        public override string ToString()
        {
            return _name;
        }
    }

    class Honda : Make
    {
        Honda()
        {
            id = 1;
            _name = "Honda";
        }

        public class Civic : Model
        {
            Civic()
            {
                id = 1;
                _name = "Civic";
            }
        }
    }


In this case, every time you want to do a comparison, you need to do:
if (Model == new Honda.Civic() || Model == new Toyota.Prius())
which is ugly as you're creating a new object every time just to do a simple check.

Now, in normal cases you could just use an enum and compare it to that, but as I showed before, there are a few more parameters this object must contain.

I'm really at a loss here as I don't know what to do. I need to come up with a solution that is easily readable as our list grows.

It really should look like:
Model == Honda.Civic || Model == Toyota.Prius
And, we should be able to do Honda.ToString(), Honda.id, Honda.Civic.ToString(), Honda.Civic.id, etc...

Is there no easy way to implement this type of pattern?

Am I just not used to using statements like Model == new Honda.Civic() as acceptable design?

So confused...
Was This Post Helpful? 0
  • +
  • -

#11 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 256
  • View blog
  • Posts: 529
  • Joined: 07-July 10

Re: Two part design question for an object that describes a Vehicle.

Posted 04 October 2010 - 07:44 AM

Using the second example of yours (and adding "public" to the constructors), I came up with this.
            Model car = new Honda.Civic();

            if (car.GetType() == typeof(Honda.Accord))
                Console.WriteLine("Car is an Accord");

            if (car.GetType() == typeof(Honda.Civic))
                Console.WriteLine("Car is a Civic");

            if (car.GetType() == typeof(Toyota.Prius))
                Console.WriteLine("Car is a Prius");

            Console.ReadLine();


The only other solution I see to get what you want is to have an enum that contains all of the models. Each derived Make can ensure that it only references the correct model.
Was This Post Helpful? 1
  • +
  • -

#12 CodeGuru1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 10

Re: Two part design question for an object that describes a Vehicle.

Posted 04 October 2010 - 09:53 AM

Robin, that's another great solution, thanks!

Using enum is out of the question. Even though it has a numerical ID, and can ToString(), it leaves no room for adding fields in the future.

Another solution would be to use namespaces, but that seems like a hack.

I think that there may not be a "clean" way to do this.
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,356
  • Joined: 02-June 10

Re: Two part design question for an object that describes a Vehicle.

Posted 04 October 2010 - 10:14 AM

Quote

I think that there may not be a "clean" way to do this.


I'm not sure I follow what you think is un-clean about classes. That is the OOP way of doing this.
Vehicle
Model
Engine

Honda : Vehicle
Civic : Model
SixCyle : Engine

I seemed to have missed where you stated the reason this doesn't work for you.
Was This Post Helpful? 0
  • +
  • -

#14 CodeGuru1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 10

Re: Two part design question for an object that describes a Vehicle.

Posted 04 October 2010 - 10:32 AM

I guess the proper OOP way is to design code that would be used as:
if (Model == new Honda.Civic() || Model == new Toyota.Prius())

when ideally, it should be:
Model == Honda.Civic || Model == Toyota.Prius

This is a static data store, that should be in a database, but in this case it cannot.

One solution that I've come up with to have the following result:
Make Make = Make.Honda;
Model Model = Model.Honda.Civic;

Is to do the following:

    public struct Model
    {
        private Model(Make newMake, string NewName)
        {
            make = newMake;
            name = NewName;
        }

        private Make make;
        private string name;

        public static Model unknown            { get { return new Model(Make.unknown, null); } }

        public static class Honda
        {
            public static Model Civic        { get { return new Model(Make.Honda, "Civic"); } }
        }
    }


It puts the work into the maintenance/implementation of the source based database and cleans it up on the development/implementation side.

I think I prefer this method because it's a static data store.

I'm open to all feedback and suggestions though. :)
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,356
  • Joined: 02-June 10

Re: Two part design question for an object that describes a Vehicle.

Posted 04 October 2010 - 12:14 PM

I'm just wondering why you are so bound and determined to do things differently than the rest of the industry. Are you trying to make your code non-standard and hard to follow so as to secure your long term employment? Are you trying to make your code something that your co-workers don't want to touch?

This
Model Model = Model.Honda.Civic;
going to
public static Model Civic { get { return new Model(Make.Honda, "Civic"); } }

Is effectively no different to to a properly created class
Model myModel = new Honda.Civic();

making a new Honda.Civic should automatically set the name string to "Civic". You shouldn't have to send it as part of your creation process. It opens lots of issues like... SOmeone sending "civic" instead of "Civic" and so on.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2