Goldman Sachs Interview Question
Software ArchitectsCountry: India
Interview Type: Phone Interview
You can look at the Java's java.io.Reader and java.io.Writer classes. These are abstract interfaces that can be used to read/write multiple sources/formats. Have generic methods like close(), read(), skip() in the reader interface and have your writer interface contain functions to write(), close(), save(), flush(), etc. The source/format of data is defined by the implementation you use for the abstract reader and writer.
For instance one format is String. To read and write the same we have a StringReader and a StringWriter. If you want to read from String but write to file then you use a StringReader(string) and write using the FileStreamWriter(filename).
see java.io implementation. for interview, you can define 3 interfaces reader, writer, convert.
Client may call convert and reader, writer to get it converted.
managing multiple sources and multiple destination with dynamic algorithm, you can avail strategy with builder.
so ultimately , you will be using handle to read and write and select conversion using strategy.
or
while reading, you use strategy to normalize it and later provide converted data to client with another strategy and write
Interfaces: DataSource
open()
close()
getData()
getDataFormat()
Interfaces: Formatter
format()
Interfaces: DataSourceReader
public String read();
Interfaces: DataSourceWriter
public void write();
public class WebserviceDataSourceReader implements DataSourceReader
{
private DataSourceReader ds;
private Formatter f;
public WebserviceDataSourceReaded(DataSource ds, Formatter f)
{
this.ds = ds;
this.f = f;
}
public String read()
{
ds.open();
String data = f.format(ds.getData());
ds.close();
return data;
}
};
/* There could be multiple different Formatter objects like XMLFormatter, IMGFormappter , TEXTFormatter etc
There will be some factory which can send formmater depends on the DataSource.getDataType() return value
The class that creates objects of DataSourceReader knows which DataSource & Formatter to be used for the specialized class
*/
This looks like a simple bridge design pattern solution.
Abstracton --> Format
Feld --> Stream
Refined Abstracton --> XMLFormat, JSON Format
Bridge interface --> Stream
Implementation --> DiskStream, NetworkStream etc.
This way we can add new formats and readers quite flexibly without complicating the whole design with unnecessary Inheritance heirarchy
We can have two separate Hierarchy
- martin.mathew.2k11 April 24, 20141. types of Data Source.
2. For Handling the format.
A controller entity which would read from the datasource and depending on the format would assign the respective format handler.
For serialization each format handler can decide how to write in their formats