I programming with C#.
My scenario is how handle Write/Read with different files(i.e. .xml, .txt, .csv,...).
Which design pattern is better for your opinion?
My experience for the solution:
Interfraces:
interface IFileWrite
{
void WriteFile();
}
interface IFlieRead
{
string ReadFile();
}
Abstract class:
class AbstractFile
{
private string _path;
private string _suffixFile;
public AbstractFile(string path,string suffix)
{
_path = path;
_suffixFile = suffix;
}
protected string Path
{
get { return _path; }
set { _path = value; }
}
protected string SuffixFile
{
get { return _suffixFile; }
set { _suffixFile = value; }
}
}
Diffrenet files to inherit form AbstractFile and interfaces: (for example LogFile (.txt) can only write)
class LogFile : AbstractFile, IFileWrite
{
public LogFile() : base(Environment.CurrentDirectory, ".txt")
{
}
public void WriteFile()
{
throw new NotImplementedException();
}
}
And finally FileManager thats knows only how to Write/Read File:
class FileManager
{
public string GetTextFromFiles(IEnumerable<IFlieRead> aLstReadableFiles)
{
StringBuilder objStrBuilder = new StringBuilder();
foreach (var objFile in aLstReadableFiles)
{
objStrBuilder.Append(objFile.ReadFile());
}
return objStrBuilder.ToString();
}
public void SaveTextIntoFiles(IEnumerable<IFileWrite> aLstWritableFiles)
{
foreach (var objFile in aLstWritableFiles)
{
objFile.WriteFile();
}
}
}
Aucun commentaire:
Enregistrer un commentaire