Skip to content

Instantly share code, notes, and snippets.

@mrtank
Created September 21, 2016 09:07
Show Gist options
  • Save mrtank/652f97e113eb9b483c2440bdfdedf93c to your computer and use it in GitHub Desktop.
Save mrtank/652f97e113eb9b483c2440bdfdedf93c to your computer and use it in GitHub Desktop.
// usage
// ...
IUnityContainer container = new UnityContainer();
container.RegisterType(typeof (IPhone), typeof (Phone));
container.RegisterType(typeof (IPda), typeof (Pda));
container.RegisterType(typeof (ICellPhone), typeof (CellPhone));
ICellPhone cellPhone = container.Resolve<ICellPhone>()
// ...
// classes
public interface IPhone
{
void MakeCall(int phoneNumber);
void AnswerCall();
void HangUp();
}
public interface IPda
{
void SendEmail(string[] recipientList, string subject, string message);
int LookUpContactPhoneNumber(string contactName);
void SyncWithComputer();
}
public interface ICellPhone: IPhone, IPda { }
public class CellPhone : ICellPhone
{
private readonly IPhone _phone;
private readonly IPda _pda;
public CellPhone(IPhone phone, IPda pda)
{
_phone = phone;
_pda = pda;
}
public void MakeCall(int phoneNumber)
{
_phone.MakeCall(phoneNumber);
}
public void AnswerCall()
{
_phone.AnswerCall();
}
public void HangUp()
{
_phone.HangUp();
}
public void SendEmail(string[] recipientList, string subject, string message)
{
_pda.SendEmail(recipientList, subject, message);
}
public int LookUpContactPhoneNumber(string contactName)
{
return _pda.LookUpContactPhoneNumber(contactName);
}
public void SyncWithComputer()
{
_pda.SyncWithComputer();
}
}
public class Phone : IPhone
{
public void MakeCall(int phoneNumber) { // implementation }
public void AnswerCall() { // implementation }
public void HangUp() { // implementation }
}
public class Pda : IPda
{
public void SendEmail(string[] recipientList, string subject, string message) { // implementation }
public int LookUpContactPhoneNumber(string contactName) { // implementation }
public void SyncWithComputer() { // implementation }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment