Pass an object, inherit it, or use a direct method
I am new to C # and OOP scripting and in the process of building an infrastructure that will help me understand OOP concepts better. This is a project I did to test my knowledge in class. Can someone get over this and explain to me what would be the best approach and why?
The following are the objects in my C # program:
- deskClerk [has an addCustomer method that returns a Customer object.]
- Order
- Photo
- Customer
- Utility
- Showroom [contains lists of customers, stocks and orders - all listings are private. Showroom also has addCustomer method]
I want the DeskClerk object to add the customer to the Showroom object. For this I have the following options:
- I can pass a Showroom object to deskClerk and then use addCustomer in Showroom Center to add a Client.
- I can directly add a customer to the Showroom since the Showroom object already has an addCustomer method.
- I can inherit the deskClerk object from Showroom, in which case deskClerk can add to the list of customers using Showroom's addCustomer method.
My questions:
- Which of the three options above has sound logic?
- If Showroom has addCustomer method? The showroom should only be a repository for inventory, customer and order lists, right?
source to share
You must work if the Client can exist without participating in the Showroom and if DeskClerk can sail without participating in the Showroom.
If this is the scenario I would do something like this
public class Showroom {
public DeskClerk Clerk { get; private set; }
public List<Customer> Customers { get; set; }
[...]
}
A clerk won't make a lot of sense without a showroom, so set it as a constructor as a constructor:
public class DeskClerk {
private ShowRoom { get; set; }
public DeskClerk(ShowRoom showRoom) {
ShowRoom = showRoom;
}
public Customer AddCustomer(Customer customer) {
//do stuff with customer object
ShowRoom.Add(customer);
return cutomer;
}
}
Don't feel like it would be right to have 2 places to add clients, but DeskClerk should still be responsible for that.
However, I may have misunderstood your problem :-) Hope this helps!
source to share
I like your approach to the problem. Therefore, reading your questions:
-
Each of these options has sound logic, so your choice depends on how you want to structure your objects and how you want to use them. As a programmer, I recommend that you use the second or third.
-
Yes, Showroom can have an addCostumer method, in which case if you want to use a second or third choice, it must have an addCostumer method if you want to add Costumer to the store.
Using the third choice here is my example code:
class Stock { /*...*/ }
class Customer { /*...*/ }
class Order { /*...*/ }
class Showroom
{
protected List<Stock> StockList;
protected List<Customer> CustomerList;
protected List<Order> OrderList;
public Showroom()
{
StockList = new List<Stock>();
CustomerList = new List<Customer>();
OrderList = new List<Order>();
}
public virtual void addStock(Stock stock)
{
StockList.Add(stock);
}
public virtual void addCustomer(Customer customer)
{
CustomerList.Add(customer);
}
public virtual void addOrder(Order order)
{
OrderList.Add(order);
}
//...
}
class deskClerk : Showroom
{
public deskClerk()
{
CustomerList = new List<Customer>();
}
public override void addCustomer(Customer customer)
{
CustomerList.Add(customer);
}
//...
}
Therefore, I can recommend you something else:
Every time you work with objects, they each give their role and their tasks, and then each choice depends on the logic that you want to use. Pick one that you think will work and will work well in the context in which you want to use it.
source to share