Click here to Skip to main content
15,888,579 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
AnswerRe: Handling errors - What is a good balance? Pin
RobCroll18-May-12 2:48
RobCroll18-May-12 2:48 
AnswerRe: Handling errors - What is a good balance? Pin
Ron Beyer24-May-12 17:40
professionalRon Beyer24-May-12 17:40 
QuestionSimple composition - Your thoughts? Pin
Eytukan14-May-12 23:15
Eytukan14-May-12 23:15 
AnswerRe: Simple composition - Your thoughts? Pin
Nagy Vilmos14-May-12 23:38
professionalNagy Vilmos14-May-12 23:38 
GeneralRe: Simple composition - Your thoughts? Pin
Eytukan14-May-12 23:43
Eytukan14-May-12 23:43 
AnswerRe: Simple composition - Your thoughts? Pin
Pete O'Hanlon14-May-12 23:48
mvePete O'Hanlon14-May-12 23:48 
GeneralRe: Simple composition - Your thoughts? Pin
Nagy Vilmos15-May-12 1:25
professionalNagy Vilmos15-May-12 1:25 
GeneralRe: Simple composition - Your thoughts? Pin
Pete O'Hanlon15-May-12 1:56
mvePete O'Hanlon15-May-12 1:56 
Nagy Vilmos wrote:
If the rocket must have a booster, then I agree it must be a part of the
constructor to ensure its presence.

And that's the rub. What constitues a valid instance of the class? In the admittedly contrived example given, the only operation that could be performed was a Launch, so the booster is an intrinsic part of this particular example.

Now, if this class were one I was designing, I would take a slightly different approach if this was a full lifecycle of a rocket. This particular rocket implementation is tightly coupled to a single booster. What happens if I want a rocket with more than one booster? Or I want to use one that's based on the ACME Fusion Drive 3000 elastic band? To that end, I'd consider that a design like this might be better:
C#
public class Rocket
{
  public Rocket()
  {
  }

  public void PreLaunchTests()
  {
    OnPreLaunchTests();
  }

  public void Launch()
  {
  }

  protected virtual void OnPreLaunchTests()
  {
  }

  protected virtual void OnLaunch()
  {
  }
}

public class SingleBoosterRocket : Rocket
{
  public List<IBooster> Boosters { get; private set; }

  public SingleBoosterRocket()
  {
    Boosters = new List<IBooster>();
  }

  public void AddBooster(IBooster booster)
  {
    Boosters.Add(booster);
  }

  protected override void OnPreLaunchTests()
  {
    // Do some pre launch tests.
  }

  protected override void OnLaunch()
  {
    if (Boosters.Count == 0)
      throw new NotEnoughBoostersException();
    // Do some other work.
  }
}

public class DualBoosterRocket : SingleBoosterRocket
{

  public DualBoosterRocket() : base()
  {
  }

  protected override void OnPreLaunchTests()
  {
    // Do some pre launch tests.
  }

  protected override void OnLaunch()
  {
    if (Boosters.Count < 2)
      throw new NotEnoughBoostersException();
    // Do some other work.
  }
}
Again, this is a completely fabricated design, but this has the advantage of being more flexible. Ultimately, the design should be based on the real requirements, which is why I have steered well clear of saying that one approach is better than another in all cases. BTW - in case you're wondering, the actual composition in this sample has been moved to the containing object, rather than the actual rocket implementation, as that piece of code would be the one that decided what the composition entailed. The rules for the rocket have been encapsulated in a simple classes that have a single responsibility.

*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

"Mind bleach! Send me mind bleach!" - Nagy Vilmos


CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

GeneralRe: Simple composition - Your thoughts? Pin
Eytukan15-May-12 4:36
Eytukan15-May-12 4:36 
GeneralRe: Simple composition - Your thoughts? Pin
Pete O'Hanlon15-May-12 4:53
mvePete O'Hanlon15-May-12 4:53 
AnswerRe: Simple composition - Your thoughts? Pin
BobJanova15-May-12 0:46
BobJanova15-May-12 0:46 
AnswerRe: Simple composition - Your thoughts? Pin
Marc Clifton15-May-12 1:51
mvaMarc Clifton15-May-12 1:51 
GeneralRe: Simple composition - Your thoughts? Pin
Pete O'Hanlon15-May-12 1:57
mvePete O'Hanlon15-May-12 1:57 
GeneralRe: Simple composition - Your thoughts? Pin
Marc Clifton15-May-12 2:45
mvaMarc Clifton15-May-12 2:45 
GeneralRe: Simple composition - Your thoughts? Pin
Pete O'Hanlon15-May-12 3:25
mvePete O'Hanlon15-May-12 3:25 
GeneralRe: Simple composition - Your thoughts? Pin
Marc Clifton15-May-12 4:57
mvaMarc Clifton15-May-12 4:57 
GeneralRe: Simple composition - Your thoughts? Pin
Eytukan15-May-12 4:41
Eytukan15-May-12 4:41 
GeneralRe: Simple composition - Your thoughts? Pin
Marc Clifton15-May-12 5:06
mvaMarc Clifton15-May-12 5:06 
GeneralRe: Simple composition - Your thoughts? Pin
Eytukan15-May-12 18:32
Eytukan15-May-12 18:32 
QuestionDatabase Modeling - Conditional Relationships Pin
Leslie Sanford11-May-12 8:34
Leslie Sanford11-May-12 8:34 
AnswerRe: Database Modeling - Conditional Relationships Pin
R. Giskard Reventlov11-May-12 9:49
R. Giskard Reventlov11-May-12 9:49 
GeneralRe: Database Modeling - Conditional Relationships Pin
Leslie Sanford12-May-12 8:42
Leslie Sanford12-May-12 8:42 
AnswerRe: Database Modeling - Conditional Relationships Pin
Eddy Vluggen13-May-12 4:32
professionalEddy Vluggen13-May-12 4:32 
AnswerRe: Database Modeling - Conditional Relationships Pin
jschell14-May-12 9:24
jschell14-May-12 9:24 
Questionhow to design base class? Pin
wan.rui@qq.com10-May-12 17:09
wan.rui@qq.com10-May-12 17:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.