I have a Class that retrieves some data and images does some stuff to them and them uploads them to a third party app using web services.
The object needs to perform some specific steps in order.
My question is should I be explicitly exposing each method publicly like so.
myObject obj = new myObject();
obj.RetrieveImages();
obj.RetrieveAssociatedData();
obj.LogIntoThirdPartyWebService();
obj.UploadStuffToWebService();
or should all of these methods be private and encapsulated in a single public method like so.
public class myObject()
{
private void RetrieveImages(){};
private void RetrieveAssociatedData(){};
private void LogIntoThirdPartyWebService(){};
private void UploadStuffToWebService(){};
public void DoStuff()
{
this.RetrieveImages();
this.RetrieveAssociatedData();
this.LogIntoThirdPartyWebService();
this.UploadStuffToWebService();
}
}
which is called like so.
myObject obj = new myObject();
obj.DoStuff();
It depends on who knows that the methods should be called that way.
Consumer knows: For example, if the object is a
Stream, usually the consumer of the Stream decides when toOpen,Read, andClosethe stream. Obviously, these methods need to be public or else the object can’t be used properly. (*)Object knows: If the object knows the order of the methods (e.g. it’s a
TaxFormand has to make calculations in a specific order), then those methods should be private and exposed through a single higher-level step (e.g.ComputeFederalTaxwill invokeCalculateDeductions,AdjustGrossIncome, andDeductStateIncome).If the number of steps is more than a handful, you will want to consider a
Strategyinstead of having the steps coupled directly into the object. Then you can change things around without mucking too much with the object or its interface.In your specific case, it does not appear that a consumer of your object cares about anything other than a processing operation taking place. Since it doesn’t need to know about the order in which those steps happen, there should be just a single public method called
Process(or something to that effect).(*) However, usually the object knows at least the order in which the methods can be called to prevent an invalid state, even if it doesn’t know when to actually do the steps. That is, the object should know enough to prevent itself from getting into a nonsensical state; throwing some sort of exception if you try to call
ClosebeforeOpenis a good example of this.