Sometime back in one of the interviews with top Investment Bank, I was asked to write top level design diagram and highlight Interfaces, Classes and important mehthods.
Below is the detailed problem statement:
1. Trade domain objects may be enriched to add further information.
2. There are many different types of enrichment which may be carried out on a trade.
Party & Booking enrichment are two specific types of Enricher.
3. All enrichment is done normally at certain point in Trade processing (e.g. multiple Enrichers operating sequentially on Trades). This must be done via single Enricher.
4. It may be important to control the order of some Enrichers.
This is smart question because it makes you think and touches upon few of basic core java concepts like: Open Closed principle, Program against interface and also your design pattern knowledge on Strategy & Chain Of Responsibility but most importantly it checks your analytical and designing skills.
I liked the question because it's smart, simple and yet tricky. It's much better than asking singltons & hashmaps and all those useless questions which are repeatedly asked over and over again and people still think knowing these concepts can only make them smart coders !!
Till, first two points, I was sure they are asking me to explain Strategy pattern and will be straight forward but when I read 3rd point, I was sure they are asking something tricky which needed to be done with combination of some design patterns.
After putting my analytical skills in action for couple of minutes, I realized they are indirectly asking me to implement Chain Of Responsibility to check my designing skills. So here is what I came up with:
We will mainly need one interface (TradeEnrichment) to define enrichment and two concrete implementations: BookingEnricher & PartyEnricher.
public interface TradeEnrichment {
public void enrichment(TradeAccount tradeAccount);
public void setNextEnricher(TradeEnrichment nextEnricher);
}
We will also need one class to invoke all these components and control sequencing order for the Trades: EnrichmentProvider.
public class EnrichmentProvider {
TradeEnrichment firstEnricher;
public void EnrichmentProvider(){
this.firstEnricher = new BookingEnricher();
TradeEnrichment secondEnricher = new PartyEnricher();
/**
* set enrichment sequence here. you may have N number of enrichers in futures and you can control
* the order of sequence here without impacting actual implementation in TradeEnrichment.
*/
firstEnricher.setNextEnricher(secondEnricher);
}
public static void main(String[] args) {
EnrichmentProvider tradeEnrichmentProvider = new EnrichmentProvider();
TradeAccount account = new TradeAccount();
account.setTradeType(TradeType.BOOK);
tradeEnrichmentProvider.firstEnricher.enrichment(account);
}
}
Below is the detailed problem statement:
1. Trade domain objects may be enriched to add further information.
2. There are many different types of enrichment which may be carried out on a trade.
Party & Booking enrichment are two specific types of Enricher.
3. All enrichment is done normally at certain point in Trade processing (e.g. multiple Enrichers operating sequentially on Trades). This must be done via single Enricher.
4. It may be important to control the order of some Enrichers.
This is smart question because it makes you think and touches upon few of basic core java concepts like: Open Closed principle, Program against interface and also your design pattern knowledge on Strategy & Chain Of Responsibility but most importantly it checks your analytical and designing skills.
I liked the question because it's smart, simple and yet tricky. It's much better than asking singltons & hashmaps and all those useless questions which are repeatedly asked over and over again and people still think knowing these concepts can only make them smart coders !!
Till, first two points, I was sure they are asking me to explain Strategy pattern and will be straight forward but when I read 3rd point, I was sure they are asking something tricky which needed to be done with combination of some design patterns.
After putting my analytical skills in action for couple of minutes, I realized they are indirectly asking me to implement Chain Of Responsibility to check my designing skills. So here is what I came up with:
We will mainly need one interface (TradeEnrichment) to define enrichment and two concrete implementations: BookingEnricher & PartyEnricher.
public interface TradeEnrichment {
public void enrichment(TradeAccount tradeAccount);
public void setNextEnricher(TradeEnrichment nextEnricher);
}
Here, second method is important because it will provide us order of execution. Now, let's see one concrete implementation of this interface as asked in the question.
public class BookingEnricher implements TradeEnrichment {
TradeEnrichment enricher;
@Override
public void enrichment(TradeAccount tradeAccount) {
System.out.println("Enriching The Trade Booked");
if(enricher != null)
this.enricher.enrichment(tradeAccount); // call next enrichment
}
@Override
public void setNextEnricher(TradeEnrichment nextEnricher) {
this.enricher = nextEnricher;
}
}
Here, I have provided dummy implementation of Booking enrichment. Important thing to note here is the last statement which calls next Enricher in the sequence. So after completing the job Enricher simply passes the call to next Enricher in the sequence set using setNextEnricher() method.
Now, using Strategy Pattern here we can make sure that TradeEnrichment can have any number of concrete implementations in future as required and so our design follows Open-Closed principle (Open for enhancement but closed for modifications).
public class EnrichmentProvider {
TradeEnrichment firstEnricher;
public void EnrichmentProvider(){
this.firstEnricher = new BookingEnricher();
TradeEnrichment secondEnricher = new PartyEnricher();
/**
* set enrichment sequence here. you may have N number of enrichers in futures and you can control
* the order of sequence here without impacting actual implementation in TradeEnrichment.
*/
firstEnricher.setNextEnricher(secondEnricher);
}
public static void main(String[] args) {
EnrichmentProvider tradeEnrichmentProvider = new EnrichmentProvider();
TradeAccount account = new TradeAccount();
account.setTradeType(TradeType.BOOK);
tradeEnrichmentProvider.firstEnricher.enrichment(account);
}
}
As you can see we are controlling the order of enrichers in the constructor by setting the next enricher in the chain.
Interviewers were quite impressed with clear understanding or design concepts and ability to craft clean interfaces because it helps to discuss problems in terms of design patterns during design sessions and building new softwares. You don't have to explain your team how you will set order, how you can keep your implementations open for enhancements but closed for modifications etc. All you need to tell them is use Strategy Pattern & Chain Of Responsibility for Trade Enricher to control the order here and your job as a manager or architect is done, provided, you have hired right tech resource !!!!
Working code can be found here.
No comments:
Post a Comment