You are probably already wondering why this page is named Factory Work. To satisfy that curiosity, consider what a factory does. Put simply, a factory works under given parameters to produce a desired product. How is this applicable to the problem at hand? Well, we want our ETFTransaction object to be capable of utilizing any Merchant that we provide a class for. To allow this without hard coding the ETFTransaction object, we have to provide an intermediary class, a Factory class, to provide the proper Merchant object to the ETFTransaction object. The purpose of our factory is this: take a parameter object and return the appropriate Merchant object to the ETFTransaction object. Let's look at a definition:
We would extend our ETFTransaction object again:
You will note that the constructor is protected; this is because we do not wish to allow this object to be instantiated more than once. _instance is a reference to the sole instance of this object. Obviously you cannot use this method in PHP, but there are ways. The Instance() method would look something like this:
To use the factory, we would have code such as this:
So now we've outlined how our Transaction object will go about getting a Payment Service object, but we still have not defined an interface for the ETFPaymentService class. Let's do so now:
As you can see, this is the abstract class for all Payment Services to inherit from; you would see objects like AuthorizeNet:ETFPaymentService and LinkPoint:ETFPaymentService to handle specific requests. We could simply add a call to _ps->SendRequest() in our Execute:ETFTransaction function to process the ETF. The private method of ETFPaymentService SupportsPaymentMethod() is used internally to check against whatever data registry we have provided to ensure that the PaymentMethod object represents and Payment Method that our Payment Service is capable of handling. Lastly, we would add a method such as the following to allow retrieval of status information from our ETFTransaction object. We would also want a method to store messages from our Payment Service. The way these messages are represented in XML from the Payment Service are not important to this discussion, because they will be adapted to fit the interface of a generic ETFMessage object.
You see a basic Iterator pattern here, which could be expanded if you want. We simply want to provide a standard method of message retrieval to our client. Now that it is established how our objects will interact, and we have an idea how the objects will receive their parameters, we can briefly discuss client implementation. To review the process involved in using these classes, we have the following steps:
And... viola! That's it. Now that we have a design, the rest is gravy. Remember that routines and functions will do whatever you want them to do -- it is the design that truly makes a system work.
blog comments powered by Disqus |