How can I share values โ€‹โ€‹between classes (or get a variable from another class) without instantiating twice.

My problem is this: I have a class, let it be called the Theatre .

This Theatre has a constructor in which I indicate the amount of Seats that this Theatre .

The Show class has a set of Seats , each with its own properties, such as bool Empty .

So, we summarize the code:

 class Theatre { public Theatre(int numberOfSeats) { this.numberOfSeats = numberOfSeats; } } 

Suppose we create 100 somewhere.

 Theatre myTheatre = new Theatre(100); 

For class Show :

 class Show { List<Seats> listOfSeats = new List<Seats>(); public Show() { for (int i = 0; i < 100; i++) // <---- And here is my problem!! { //Code to add to list } } } 

I'm new to oop , so bare with me.

My problem is that I do not know how to get rid of this 100 .

I would like to have something like myTheatre.NumberOfSeats, but I'm not quite sure how this will work.

My problem is that I already created myTheatre in a different class, so I would have to create a new theater inside the Show , just to get the number of seats, I would use something like composition and deletion, but this is clearly violated by the ISP.

But even this has a problem, because when I create a new Theatre , I would have to put 100 as a parameter and, if it was necessary to change, I would have to manually change it.

So my questions are:

  • Am I missing something obvious? (Very likely)
  • Which approach would you recommend?
  • Is there something like a shared variable in classes? (I do not want to use the word global, because I'm not sure what I want, but maybe).

As an obvious clarification, the above is not an actual code, but just a presentation to clarify the situation.

+4
source share
3 answers

This is how you solve it. Pass the Theater as an argument for the show

  class Theatre { public Theatre(int numberOfSeats) { NumberOfSeats = numberOfSeats; } public int NumberOfSeats { get; private set; } } class Show { List<Seats> listOfSeats = new List<Seats>(); public Show(Theatre theatre) { for (int i = 0; i < theatre.NumberOfSeats; i++) // <---- And here is my problem!! { //Code to add to list } } } class Program { static void Main(string[] args) { Theatre myTheatre = new Theatre(100); Show myShow = new Show(myTheatre); } } 
+3
source

The Show :: Show method can take an instance of the Theater as an argument that provides the SeatCount as a property. You could โ€œshowโ€ the same exhibition in two different theaters, and the Show class should not have any specific knowledge about this theater.

You also do not want it to be global, because the shows can be shown in different theaters. This is the OOP point.

Another option is to expose the Show method in the Theater class, which takes an instance of Show as an argument.

+2
source

C # is not my specialty, therefore it is purely theoretical; anyway:

ISTM that myTheatre.numberOfSeats already exists. All you have to do is allow outside access to it; either by making it public / package visible, or (better) keeping it confidential / protected and providing access through a public receiver (method).

In class-based OOP, you must tell the Show instance about the Theatre instance, so you will pass the link to the Theatre instance to the Show instance method. In this case, the method can be a Show constructor (it is recommended to save the default constructor without arguments and call it from a special one):

 class Show { List<Seats> listOfSeats = new List<Seats>(); public Show() { } public Show(Theatre theatre) { this(); for (int i = 0, len = theatre.numberOfSeats; i < len; ++i) { /* Code to add to list */ } } } 

Then somewhere:

 Theatre myTheatre = new Theatre(100); ... Show myShow = new Show(myTheatre); 

See also: Wikipedia: OOD principles and strategies .

0
source

Source: https://habr.com/ru/post/1380508/


All Articles