Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. OOP question: correct obect composition in C#

OOP question: correct obect composition in C#

Scheduled Pinned Locked Moved C#
questioncsharpcss
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Metal76
    wrote on last edited by
    #1

    Hi, I have a question on correct usage of object composition. This is the situation I'm facing:

    class Address
    {
    // An address is composed of several fields
    private byte _FieldA;
    private byte _FieldB;
    private byte _FieldC;

    // Each field is exposed with a public property
    public byte FieldA
    {
        set { \_FieldA = value; }
        get { return \_FieldA; }
    }
    // ... and so on for the other fields
    
    // I have several useful public methods for address manipulation
    public void SetAddress (byte fieldA, byte fieldB, byte fieldC);
    public void DoStuff(...)
    public void DoOtherStuff(...)
    ...
    

    }

    class Message
    {
    // A message contains 2 addresses
    public Address SourceAddress = new Address();
    public Address DestAddress = new Address();
    }

    A client which instantiates a message should only be able to access the address fields and to invoke SetAddress(). By declaring SourceAddress and DestAddress objects as public, I can write things like message.SourceAddress.FieldA, message.DestAddress.SetAddress(), with no need for additional code. However, I also expose other methods like DoStuff() etc, which a message should not be able to call. On the other hand, if I declare SourceAddress and DestAddress as private, I can control what I expose to the client but I'm also forced to add a lot of code: public properties and methods like SourceAddressFieldA, SourceAddressFieldB, SetSourceAddress(), all duplicated for DestAddress... this means a lot of code duplication, and less flexibility (imagine if I have to add other Address fields to Message...). Is there a "sweet spot"? Is there a correct (or better, preferred) way to expose the behaviour of child objects in C#? Regards, Andrea

    C 1 Reply Last reply
    0
    • M Metal76

      Hi, I have a question on correct usage of object composition. This is the situation I'm facing:

      class Address
      {
      // An address is composed of several fields
      private byte _FieldA;
      private byte _FieldB;
      private byte _FieldC;

      // Each field is exposed with a public property
      public byte FieldA
      {
          set { \_FieldA = value; }
          get { return \_FieldA; }
      }
      // ... and so on for the other fields
      
      // I have several useful public methods for address manipulation
      public void SetAddress (byte fieldA, byte fieldB, byte fieldC);
      public void DoStuff(...)
      public void DoOtherStuff(...)
      ...
      

      }

      class Message
      {
      // A message contains 2 addresses
      public Address SourceAddress = new Address();
      public Address DestAddress = new Address();
      }

      A client which instantiates a message should only be able to access the address fields and to invoke SetAddress(). By declaring SourceAddress and DestAddress objects as public, I can write things like message.SourceAddress.FieldA, message.DestAddress.SetAddress(), with no need for additional code. However, I also expose other methods like DoStuff() etc, which a message should not be able to call. On the other hand, if I declare SourceAddress and DestAddress as private, I can control what I expose to the client but I'm also forced to add a lot of code: public properties and methods like SourceAddressFieldA, SourceAddressFieldB, SetSourceAddress(), all duplicated for DestAddress... this means a lot of code duplication, and less flexibility (imagine if I have to add other Address fields to Message...). Is there a "sweet spot"? Is there a correct (or better, preferred) way to expose the behaviour of child objects in C#? Regards, Andrea

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I think the best way is to make Address use an interface which defines the methods you want the Message to have access to. Then do public IPublicAddress SourceAddress = new Address(); This means that the Message class can only access the methods you want it to.

      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups