Passing single object vs. passing multiple parameters

后端 未结 4 860
孤街浪徒
孤街浪徒 2021-01-31 03:26

Suppose I have the following

Class A {
    Foo getFoo();
    Bar getBar();
    Baz getBaz();
}

And I need to define a function doStuff

4条回答
  •  滥情空心
    2021-01-31 04:03

    Consider a Customer that has an Address and an CurrentInvoice. Which is more correct -

    SendInvoiceToAddress(Invoice invoice, Address adress);
    

    or

    SendInvoiceToAddress(Customer customer);
    

    I think both. Or stated differently - that really depends on your app.

    If every invoice belongs (by definition) to a single customer, that's one thing. and that would imply that your method exists inside CustomerInvoiceSender class (or something like that). This is something that lies entirely within the customer domain. Each month you want to send that months' invoice (and nothing else).

    If you want to send multiple invoices to multiple adressess (not neccesarily for customers, for any purpose), well than that's a completely different story. It would probably also exist inside InvoiceSender class (or something like that). It also has nothing to do with the Customer domain. Customers in this case is just one small case of Invoices being shipped. Also worth noting is that in this case you might want interfaces instead of concretes becuase a customer's invoice and say a company's invoice might be two very different classes that just happen to share a common interface ("a couple of properties" in this case).

提交回复
热议问题