Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 618627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:34:01+00:00 2026-05-13T18:34:01+00:00

I am not sure if what I want to do breaks Object oriented guidelines

  • 0

I am not sure if what I want to do breaks Object oriented guidelines or not so I will explain what I am doing and hopefully you guys can show me a better way if I am wrong. I tried asking this question before but I gave a poor example so I think it just caused more confusion.

So I have a main class, USBCommunicator. The constructor takes a Product ID of the type of device that you want to talk to. The USBCommunicator class also has a property for a specific serial number to talk to. USBCommunicator has OpenConnection and CloseConnection methods which will open or close a data stream to transfer data between the USB device and the PC.

To send data across the stream I want the USBCommunicator to be able to create an instance of a Report class, set some parameters such as timeouts, ReportID’s etc, and then call a Send() method of the Report class to actually send the data. I don’t think any class other than USBCommunicator should be able to create an instance of the Report class. (For example, a Boat lass shouldn’t be able to create instances of CarDoor class because boat can’t have a car door.) Finally, I was originally thinking that the Report class should be able to have access to the members of USBCommunicator but I guess that is not true. If USBCommunicator opens the stream the the device, all Report really needs is a parameter passed in that is a reference/handle to the open stream. But what form should that stream be that it would allow it to be passed by the high level application? a public property? That doesn’t seem quite right.

So here is what I have so far…

namespace USBTools
{
    class HighLevelApplication
    {
        void main()
        {
            USBCommunicator myUSB = new USBCommunicator("15B3");
            myUSB.SerialNumber = "123ABC";
            myUSB.OpenConnection();
            myUSB.Report ReportToSend = new myUSB.Report(//how do I pass the stream here?);
                //It would be nice if I didn't have to pass in the stream because the stream shouldn't
                //be publicly available to the HighLevelApplication class right?
            ReportToSend.ReportID = 3;
            ReportToSend.Timeout = 1000;
            ReportToSend.Data = "Send this Data";
            ReportToSend.Send();
        }
    }

    class myUSB
    {
        myUSB(string PID)
        {
            //...
        }

        // public SerialNumber property

        // private stream field ???

        // public OpenConnection and CloseConnection methods

        class Report
        {
            Report(stream StreamToUse)
            {
                //...
            }
            Send()
            {
                //send the data
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:34:01+00:00Added an answer on May 13, 2026 at 6:34 pm

    Since USBCommunicator manages all the important resources (including the stream’s lifetime), applications should call USBCommunicator.Send, not Report.Send:

    public class USBCommunicator {
        public void Send(Report report) {
    
            // If it's appropriate, this method can also Open 
            // and Close the stream so callers don't have to.
    
            report.Send(this.stream);
        }
    }
    

    Next, make Report.Send internal so it isn’t part of the public API, and applications can do this:

    public void main(string[] args) {
            USBCommunicator myUSB = new USBCommunicator("15B3");
            myUSB.SerialNumber = "123ABC";
            myUSB.OpenConnection();
    
            Report report = new Report(3, 1000, "Send this Data");
            myUSB.Send(report);
    }
    

    I don’t think any class other than
    USBCommunicator should be able to
    create an instance of the Report
    class.

    Your current design doesn’t prevent this – indeed, your sample application is creates an instance of the Report class. If you really want to hide the report class, you should make it private to USBCommunicator and push its attributes out to the communicator’s interface like this:

    // USBCommunicator
    public void Send(int reportID, int timeout, string data) {
        Report report = new Report(reportID, timeout, data);
        report.Send(this.stream);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.