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

  • SEARCH
  • Home
  • 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 8860807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:20:36+00:00 2026-06-14T15:20:36+00:00

I am having trouble reconciling the Single Responsibility Principle with encapsulation. It seems like

  • 0

I am having trouble reconciling the Single Responsibility Principle with encapsulation. It seems like splitting responsibilities amongst classes requires exposing a lot of data. As an example, consider some object called DataPoints. DataPoints is filled with x and y coordinates, among other things. I can create a Generator class that fills DataPoints. Now, let’s say I want to plot those data points. Clearly, that’s a separate responsibility that might come from a class called DataPointsPlotter. But to plot the data, I need to know what the internal x and y coordinates are. With a single class handling both, that’s no problem. x and y are internal variables, but both a create() and a print() method have access to those. I can expose x and y (perhaps through getters/setters–ugh) or I can pass the DataPoints structure to the Plotter class, but it still needs to get inside to get x and y. I can have an instance of Plotter declared in the DataPoints class to which I send x and y. But that’s still an exposure.

How can I in this example plot x and y using the plotter without violating encapsulation?

  • 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-06-14T15:20:38+00:00Added an answer on June 14, 2026 at 3:20 pm
    class DataPoint {
    
        protected x, y
    
        public function construct(theX, theY) {
            x = theX
            y = theY
        }
    
        public function getX {
            return x
        }
    
        public function getY {
            return y
        }
    
    }
    

    This encapsulates the responsibility of representing a datapoint in a known format. Assuming there’s also some sanity checking going on to confirm that x and y have good values, this is one responsibility which is being encapsulated here.

    class Plotter {
    
        public function plot(DataPoint dp) {
            x = dp.getX
            y = dp.getY
    
            ... plot ...
        }
    
    }
    

    The plotter accepts an instance of DataPoint and plots it. That’s also one responsibility: plot an instance of DataPoint. It does not require Plotter to have any knowledge of the internal structure of DataPoint. It just requires a defined and stable interface how Plotter can get the necessary data from DataPoint. Which are the getters getX and getY. You can change the internals of DataPoint around however you want as long as its interface stays the same. And the responsibility of DataPoint is to hold a datapoint and give other code access to its data in a defined way. If there was no way to get data out of DataPoint, its existence would be quite useless.

    So no, SRP and encapsulation are not at odds with each other. You just need to be clear about what exactly the responsibility is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having trouble with Grails Domain Classes. I am overriding the constructor to build the
Having trouble when trying to create a class using another class(and 2 inner classes),
Having trouble figuring out how to put a single quote in my view. <td><%=
Having trouble inheriting from a template class. Looks something like this: template<typename type> class
Having trouble with each function... Will try to explain by example... In my code,
Having trouble with an .htaccess file on WAMP. Works on the live server, but
Having trouble accessing javascript code in a mixed html/js ajax response. jQuery ajax doc
Having trouble understanding how to filter an images table by tag information in a
Having trouble understanding. With the following css : .bloc .field:nth-last-child(2){ ...some values... } and
Having trouble framing this assertion in Rspec for my rails app. A User has

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.