PC Review


Reply
Thread Tools Rate Thread

When do we want to privatize the default parameterless constructor?

 
 
csharper
Guest
Posts: n/a
 
      26th Apr 2012
I am not sure if this example will make it clear.

Suppose we have a public class Engine and public class Car:

public class Engine
{
//snip

public void Ignite()
{
//snip
}
}

public class Car
{
private Engine engine;

public Car()
{
}

public Car (Engine engine)
{
this.engine = engine;
}

public void Start()
{
this.engine.Ignite();
}
}

Now, in Program.cs, if I do this:

public class Program
{
Car bmw = new Car();
bmw.Start();
}

I will have a problem, because the Start() method depends on a valid Engine object, whereas the default parameterless constructor Car() neither creates one nor receives one.

So, when I run into such situation, I tend to privatize the default parameterless constructor. In other words, the Car class would look like:

public class Car
{
private Engine engine;

private Car()
{
}

public Car (Engine engine)
{
this.engine = engine;
}

public void Start()
{
this.engine.Ignite();
}
}

This way, I cannot instantiate a car without passing it an Engine. However, I am not sure if this is considered good practice. Any idea? Thank you.
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      27th Apr 2012
On 4/26/2012 2:44 PM, csharper wrote:
> I am not sure if this example will make it clear.
>
> Suppose we have a public class Engine and public class Car:
>
> public class Engine
> {
> //snip
>
> public void Ignite()
> {
> //snip
> }
> }
>
> public class Car
> {
> private Engine engine;
>
> public Car()
> {
> }
>
> public Car (Engine engine)
> {
> this.engine = engine;
> }
>
> public void Start()
> {
> this.engine.Ignite();
> }
> }
>
> Now, in Program.cs, if I do this:
>
> public class Program
> {
> Car bmw = new Car();
> bmw.Start();
> }
>
> I will have a problem, because the Start() method depends on a valid Engine object, whereas the default parameterless constructor Car() neither creates one nor receives one.
>
> So, when I run into such situation, I tend to privatize the default parameterless constructor. In other words, the Car class would look like:
>
> public class Car
> {
> private Engine engine;
>
> private Car()
> {
> }
>
> public Car (Engine engine)
> {
> this.engine = engine;
> }
>
> public void Start()
> {
> this.engine.Ignite();
> }
> }
>
> This way, I cannot instantiate a car without passing it an Engine. However, I am not sure if this is considered good practice. Any idea?


Just remove it completely.

You only need to make it private when there are no other constructors.

Arne

 
Reply With Quote
 
 
 
 
csharper
Guest
Posts: n/a
 
      30th Apr 2012
On Thursday, April 26, 2012 7:07:40 PM UTC-4, Arne Vajhøj wrote:
> On 4/26/2012 2:44 PM, csharper wrote:
> > I am not sure if this example will make it clear.
> >
> > Suppose we have a public class Engine and public class Car:
> >
> > public class Engine
> > {
> > //snip
> >
> > public void Ignite()
> > {
> > //snip
> > }
> > }
> >
> > public class Car
> > {
> > private Engine engine;
> >
> > public Car()
> > {
> > }
> >
> > public Car (Engine engine)
> > {
> > this.engine = engine;
> > }
> >
> > public void Start()
> > {
> > this.engine.Ignite();
> > }
> > }
> >
> > Now, in Program.cs, if I do this:
> >
> > public class Program
> > {
> > Car bmw = new Car();
> > bmw.Start();
> > }
> >
> > I will have a problem, because the Start() method depends on a valid Engine object, whereas the default parameterless constructor Car() neither creates one nor receives one.
> >
> > So, when I run into such situation, I tend to privatize the default parameterless constructor. In other words, the Car class would look like:
> >
> > public class Car
> > {
> > private Engine engine;
> >
> > private Car()
> > {
> > }
> >
> > public Car (Engine engine)
> > {
> > this.engine = engine;
> > }
> >
> > public void Start()
> > {
> > this.engine.Ignite();
> > }
> > }
> >
> > This way, I cannot instantiate a car without passing it an Engine. However, I am not sure if this is considered good practice. Any idea?

>
> Just remove it completely.
>
> You only need to make it private when there are no other constructors.
>
> Arne


Oh yeah, I forgot about that. What a shame. Thanks.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shared email address, can't privatize appointments kamp Microsoft Outlook Calendar 3 1st Dec 2009 09:28 PM
shared email, can't privatize appointments kamp Microsoft Outlook Calendar 2 1st Dec 2009 04:14 PM
Shared email address, can't privatize apptment kamp Microsoft Outlook Calendar 1 1st Dec 2009 02:34 PM
two users my documents now shared: how to re-privatize? =?Utf-8?B?S2FsaQ==?= Windows XP Security 0 3rd Jan 2004 06:26 PM
how to get parameterless procedure ? Lloyd Dupont Microsoft ADO .NET 3 1st Dec 2003 06:30 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:28 AM.