A proxy is an object that can be used to control creation and access of a more complex object thereby deferring the cost of
creating it until the time its needed.
class Program
{
static void Main(string[] args)
{
JournalProxy proxy = new JournalProxy();
string s="teststring";
proxy.GetSearchResults(s);
proxy.InsertNewRow(System.DateTime.Now);
}
}
public interface IJournalData
{
void InsertNewRow(DateTime dateTime);
DataTable GetSearchResults(string queryString);
}
//Database layer
class JournalLayer : IJournalData
{
public void InsertNewRow(DateTime dateTime)
{
return;
}
public DataTable GetSearchResults(string queryString)
{
return new DataTable();
}
}
//Proxy
public class JournalProxy : IJournalData
{
JournalLayer _journalLayer;
string _password;
public bool Authenticate(string suppliedPassword)
{
// User sends in the password, and we make sure it is correct.
if (string.IsNullOrEmpty(suppliedPassword) || suppliedPassword != _password)
{
return false;
}
// Password is correct. Allocate the database layer, and
// setup the connection to the database code.
_journalLayer = new JournalLayer();
return true;
}
public void InsertNewRow(DateTime dateTime)
{
// In every method that the proxy implements from the interface,
// make sure that the user was authenticated.
if (_journalLayer == null)
{
return;
}
// Forward the call to the actual database code.
_journalLayer.InsertNewRow(dateTime);
}
public DataTable GetSearchResults(string queryString)
{
if (_journalLayer == null)
{
return null;
}
return _journalLayer.GetSearchResults(queryString);
}
}
creating it until the time its needed.
class Program
{
static void Main(string[] args)
{
JournalProxy proxy = new JournalProxy();
string s="teststring";
proxy.GetSearchResults(s);
proxy.InsertNewRow(System.DateTime.Now);
}
}
public interface IJournalData
{
void InsertNewRow(DateTime dateTime);
DataTable GetSearchResults(string queryString);
}
//Database layer
class JournalLayer : IJournalData
{
public void InsertNewRow(DateTime dateTime)
{
return;
}
public DataTable GetSearchResults(string queryString)
{
return new DataTable();
}
}
//Proxy
public class JournalProxy : IJournalData
{
JournalLayer _journalLayer;
string _password;
public bool Authenticate(string suppliedPassword)
{
// User sends in the password, and we make sure it is correct.
if (string.IsNullOrEmpty(suppliedPassword) || suppliedPassword != _password)
{
return false;
}
// Password is correct. Allocate the database layer, and
// setup the connection to the database code.
_journalLayer = new JournalLayer();
return true;
}
public void InsertNewRow(DateTime dateTime)
{
// In every method that the proxy implements from the interface,
// make sure that the user was authenticated.
if (_journalLayer == null)
{
return;
}
// Forward the call to the actual database code.
_journalLayer.InsertNewRow(dateTime);
}
public DataTable GetSearchResults(string queryString)
{
if (_journalLayer == null)
{
return null;
}
return _journalLayer.GetSearchResults(queryString);
}
}
No comments:
Post a Comment