Which Class should contain a method which has dependencies on multiple classes?
I’ve coded up a specific example to avoid generalizations. In this example we have an initial requirement :
customers have accounts and can purchase orders, which contain items.
The application is coded up as per OOP principles. Since the purchase requires an account, which is owned by a customer and the requirement is phrased in terms of a customer making a purchase, customer seems like a good place to put it.
However :
Added Requirement : An advance order can be reserved by a customer if
they have enough funds and purchased at a later date. an advance order
can’t be purchased if it has not been reserved.
Once we add the logic for the advance order the placement of the method on customer seems less certain. We had to re-factor quite a bit to get it in and have a duplicate method ‘Purchase’ on Order
Is having Purchase on Customer consistent with OOP principles and good design? Or is there a better place to put it, do we need a new class?
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace OOP
{
#region Repository Assembly
public class OrderRepository : IOrderRepository
{
public void Add(Order order) { }
}
public class AccountRepository : IAccountRepository
{
public void Update(Account account) { }
}
#endregion
#region Models Assembly
public interface IOrderRepository
{
void Add(Order order);
}
public interface IAccountRepository
{
void Update(Account account);
}
public class Customer
{
public string Id { get; private set; }
public string Name { get; private set; }
public Account Account { get; private set; }
public Customer(Account account)
{
this.Account = account;
}
public void Purchase(Order order)
{
if (Account.HasFundsAvailable(order.TotalPrice()))
{
Account.Debit(order.TotalPrice());
order.Purchase();
}
else
{
throw new Exception("not enough funds in account");
}
}
/// added code
public void Purchase(AdvanceOrder order)
{
if (!order.IsReserved())
{
throw new Exception("must reserve advanced orders");
}
if (Account.HasFundsAvailable(order.TotalPrice()))
{
order.Purchase();
}
else
{
throw new Exception("not enough funds in account");
}
}
/// added code
public void Reserve(AdvanceOrder order)
{
if (Account.HasFundsAvailable(order.TotalPrice()))
{
order.Reserve();
}
else
{
throw new Exception("not enough funds in account");
}
}
}
public class Account
{
private IAccountRepository rep;
public string Id { get; private set; }
public string CustomerId { get; private set; }
public decimal Balance { get; private set; }
public List<Transaction> Transactions { get; private set; }
public Account(IAccountRepository rep, decimal balance, List<Transaction> Transactions)
{
this.rep = rep;
this.Balance = balance;
this.Transactions = Transactions;
}
public bool HasFundsAvailable(decimal amount)
{
if (this.Balance >= amount)
{
return true;
}
return false;
}
public void Debit(decimal amount)
{
if (this.HasFundsAvailable(amount))
{
Transaction t = new Transaction(amount);
this.Transactions.Add(t);
this.rep.Update(this);
}
else
{
throw new Exception("not enough funds");
}
}
}
public class Transaction
{
public Transaction(decimal amount)
{
this.Amount = amount;
}
public string Id { get; private set; }
public decimal Amount { get; private set; }
}
public class Order
{
protected IOrderRepository rep;
public string Id { get; private set; }
public string Status { get; protected set; }
public IEnumerable<Item> Items { get; private set; }
public Order(IOrderRepository rep, string status, List<Item> items)
{
this.rep = rep;
this.Status = status;
this.Items = items;
}
public decimal TotalPrice()
{
return this.Items.Sum(i => i.Price);
}
public void Purchase()
{
this.Status = "Purchased";
this.rep.Add(this);
}
}
/// added code
public class AdvanceOrder : Order
{
public AdvanceOrder(IOrderRepository rep, string status, List<Item> items)
: base(rep, status, items)
{
}
public void Reserve()
{
this.Status = "Reserved";
this.rep.Add(this);
}
public bool IsReserved()
{
return this.Status == "Reserved";
}
}
public class Item
{
public string Id { get; private set; }
public string Name { get; private set; }
public decimal Price { get; private set; }
public Item(string name, decimal price)
{
this.Name = name;
this.Price = price;
}
}
#endregion
[TestClass]
public class Tester
{
[TestMethod]
public void PurchaseTest()
{
IOrderRepository orderRep = new OrderRepository();
Item i = new Item("test",(decimal)10.2);
List<Item> items = new List<Item>();
items.Add(i);
Order o = new Order(orderRep, "new", items);
IAccountRepository accountRep = new AccountRepository();
Account a = new Account(accountRep, (decimal)100, new List<Transaction>());
Customer c = new Customer(a);
c.Purchase(o);
Assert.AreEqual("Purchased", o.Status);
}
[TestMethod]
public void ReserveTest()
{
IOrderRepository orderRep = new OrderRepository();
Item i = new Item("test", (decimal)10.2);
List<Item> items = new List<Item>();
items.Add(i);
AdvanceOrder o = new AdvanceOrder(orderRep, "new", items);
IAccountRepository accountRep = new AccountRepository();
Account a = new Account(accountRep, (decimal)100, new List<Transaction>());
Customer c = new Customer(a);
c.Reserve(o);
Assert.AreEqual("Reserved", o.Status);
c.Purchase(o);
Assert.AreEqual("Purchased", o.Status);
}
}
}
10
Is having Purchase on Customer consistent with OOP principles and good
design? Or is there a better place to put it, do we need a new class?
No, it is not, and yes you need a new class. A purchase indicates that there is data to be persisted, and the Customer object should not know about data persistence pertaining to an order, or even about what is being ordered.
Typically you will have a service object that knows how to place an order, so the code becomes:
orderService.placeOrder(Customer customer, Shopping cart cart);
1