A
A
Alina2021-01-24 16:33:12
OOP
Alina, 2021-01-24 16:33:12

Constructor and how to use attributes correctly?

Hello!
I'm starting to learn OOP and C #, I have some experience with PHP but without classes. I am training on a demo project for the sale of TV.

Here I have three initial TV classes, the Buyer and the Manager (his task is to sell the TV or write a line to the database). Pseudo code below. The TV class is created through a factory to which the TV ID is passed, the factory class goes to the database (there are no orms yet) finds this record and at the same time looks to see if there is a client. and if there is, it creates a client object and also passes it to the TV constructor.

Let's say in the future there will be a RepairMgr class that will need to know which TV and who the client is. Since the TV is sold, the specific object will already have an attribute of the Client type.

The question is the following. I'm starting right away with TDD and to test the RepairMgr class I only pass TV and RepairMgr gets the client itself.
I don't think this is correct, but I can't figure out why. In theory, it seems right to do new RepairMgr($tvObj, $clientObj); But then it turns out that it is redundant to pass the Client to the TV constructor above?

In general, I have a feeling that I am confusing some approaches, it seems like the TV itself has an object and you don’t really need to know whether it is sold or not, but it’s also strange not to include the client there.
Tell me how is it right?

Plus, another question is how to correctly understand what should be class attributes and what can be passed to method parameters? In this case, for example, there is an "extended warranty" property. Should it be an attribute or passed to a method? since the extended warranty, in theory, specifically applies to TV, but it can also be by itself ..

class TV
{
  has string name, required;
  has Customer customer, not required;
}
class Customer 
{
  has name, required;
}
class SalesMgr(TV tv, Customer customer)
{
  has name;
  method 'SoldTV'(TV tv, Customer customer, Enum options) => {
    tv.customer = Customer;
  }
}


Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alina, 2021-02-23
@CsharpNovice

I will answer myself, we need one more aggregate entity, TvOrder.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question