I'm working on a design for a project and have run into an interesting problem.
Essentially there is a web form that allows someone to create a new job for my scheduler service. The form will contain properties like:
Buy Exchange
Sell Exchange
For this let's say we have for each of these fields:
Buy Exchange: Exchange1, Exchange 2 Sell Exchange: SellExchange1, SellExchange2
I should be able to select either of the Buy Exchanges or Sell Exchanges from the form but there is not an explicit relationship between Buy Exchange, or Sell Exchange in the domain model
My current design is to have the scheduler service on creation of a new job invoke a factory that will instantiate a provider for the Buy Exchange that was chosen. Both providers will implement an interface that contains a Sell() method.
My question is should I use a second factory for the sell exchange that will get called by the interface sell method? As the "selling" logic will be different for either sell exchange I'm wondering if there is a specific design pattern for this?
Here is some quick psuedocode for my design
public interface IExchangeFactory()
{
// Create Exchange 1 or Exchange 2
}
public class ExchangeFactory : IExchangeFactory
{
// Either return Exchange 1 or Exchange 2 provider
}
public class SellExchangeFactory : IExchangeFactory
{
// Either return Sell Exchange 1 or Sell Exchange 2 provider
}
public class Exchange1Provider : IExchange1Provider
{
public void PerformPurchase(){
{
// Purchase Logic Here
}
public void Sell()
{
// Instantiate Sell Exchange chosen in form
}
}
public class Exchange2Provider : IExchange2Provider
{
public void PerformPurchase(){
{
// Purchase Logic Here
}
public void Sell()
{
// Use sell exchange factory to instantiate sell exchange provider
}
}
Would this be a proper design for handling this problem? I wasn't sure how to search for this question as I'm not sure the terminology for this
I think it's not a bad idea to have two factories, each for each functionality type, buy/sell. But keep them independent.
i.e. the customer selects buy provider A, and sell provider B. So from the factories you get the correct buy and sell providers for A and B respectively.
This pattern you're proposing to use is essentially the Strategy pattern, if you're interested in reading up on it. I think you could couple it with the Template Method pattern, in case all providers need to follow the same sequence of steps with implementation differences.