Reputation: 605
I'm new to the C# and the OOP scenario and in process of building an infrastructure that'll help me better understand the OOP concepts. This is a project that I made to put my classroom knowledge to test. Can someone go over this and explain to me what would be a better approach and why?
The following are objects in my C# program:
I want the deskClerk Object to add a Customer to the Showroom object. For this I have the following choices:
My questions are:
Upvotes: 3
Views: 232
Reputation: 16623
I like your approach to the problem. So reading your questions:
Each of this choices have a 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 reccomend you use the second one or the third one.
Yes, the Showroom could have an addCostumer method and in this case if you want to use the second or third choise it must have an addCostumer method if you'd like to add a Costumer to the storage.
Using the third choise here is my sample 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);
}
//...
}
So I can reccomend you another thing:
Every time you will work with objects give to each one his role and his tasks, and then every choise depends on the logic that you want to use. Choose the one you think will work and fit well in the context where you want to use it.
Upvotes: 1
Reputation: 388
You have to work out if a Customer can exist without being part on a Showroom and if a DeskClerk can float around with out being part on a Showroom.
If that's the scenraio I'd do something like this
public class Showroom {
public DeskClerk Clerk { get; private set; }
public List<Customer> Customers { get; set; }
[...]
}
A desk clerk wouldn't be of much use without a showroom, so set the showroom as a dependency in its 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 think it would be correct to have 2 places to add customers, but the DeskClerk should still have the responsibility of doing so.
However, might have misundersttod your issue :-) Hope it helps!
Upvotes: 1