I am trying to use adapter and factory pattern which i am trying to learn but not sure about correct way of using them in real scenario.
Below is one console application for writing log in three difference part.
Thanks in advance.
This is console application
class Program
{
static void Main(string[] args)
{
LogWriterFactory _LogWriterFactory = new LogWriterFactory();
var data = _LogWriterFactory.SaveData(SaveOption.Database, "Something");
var email= _LogWriterFactory.SaveData(SaveOption.Email, "Something");
var text = _LogWriterFactory.SaveData(SaveOption.Text, "Something");
Console.WriteLine("{0}", data);
Console.WriteLine("{0}", email);
Console.WriteLine("{0}", text);
Console.ReadKey();
}
}
This is Class Library
public enum SaveOption
{
Database = 1,
Email = 2,
Text = 3
}
public class LogWriterFactory
{
public string SaveData(SaveOption option, string Log = "")
{
LogWriterAdp _LogWriterAdp = new LogWriterAdp();
string output;
switch (option)
{
case SaveOption.Database:
IConnector _SendEmailConnector= new SendEmailConnector();
output = _LogWriterAdp.Write(_SendEmailConnector, Log);
break;
case SaveOption.Email:
IConnector _WriteInDBConnector = new WriteInDBConnector();
output = _LogWriterAdp.Write(_WriteInDBConnector , Log);
break;
case SaveOption.Text:
IConnector _WriteInTextConnector= new WriteInTextConnector();
output = _LogWriterAdp.Write(_WriteInTextConnector, Log);
break;
default:
throw new ArgumentOutOfRangeException();
}
return output;
}
}
public class LogWriterAdp
{
public string Write(IConnector Connector, string Log)
{
return Connector.Save(Log);
}
}
public interface IConnector
{
string Save(string Log);
}
public class SendEmailConnector : IConnector
{
public string Save(string Log)
{
SendEmail _SendEmail = new SendEmail();
return _SendEmail.Send(Log);
}
}
public class WriteInDBConnector : IConnector
{
public string Save(string Log)
{
WriteInDB _WriteInDB = new WriteInDB();
return _WriteInDB.Save(Log);
}
}
public class WriteInTextConnector : IConnector
{
public string Save(string Log)
{
WriteInText _WriteInText = new WriteInText();
return _WriteInText.Write(Log);
}
}
public class SendEmail
{
public string Send(string Log)
{
return "Data From DatabaseHelper " + Log;
}
}
public class WriteInText
{
public string Write(string Log)
{
return "Data From WebSiteScanner " + Log;
}
}
public class WriteInDB
{
public string Save(string Log)
{
return "Data From XmlFileLoader " + Log;
}
}
I am trying to use adapter and factory pattern which i am trying to learn but not sure about correct way of using them in real scenario.
Aucun commentaire:
Enregistrer un commentaire