Software Design & Architecture
Assignment 1
Title: Complete Implementation of Strategy Pattern
Group Members:
Syed Muhammad Huzaifa 65888
Muhammad Taha 65925
Azain Rabbani 65842
Muhammad Sufyan 16063
Course Instructor: Muhammad Ayub Latif
Overview:
This project demonstrates the Strategy Design Pattern through an authentication mechanism
system. The Strategy Pattern is used to define a family of authentication algorithms,
encapsulate each one, and make them interchangeable at runtime without altering the main
authentication logic.
In this system, different authentication strategies such as Password-based, Biometric, SMS
OTP, and Email OTP are implemented as separate classes. Each of these strategies follows a
common interface called IAuthStrategy, which defines a single method — Authenticate(User user).
The AuthContext class acts as the context of the strategy pattern. It maintains a reference to an
object of type IAuthStrategy. Depending on the user's chosen method, the appropriate strategy
(authentication type) is assigned at runtime using the setAuthStrategy() method. The context then
calls the Authenticate() method to perform the login using the selected authentication
mechanism.
The User class represents the user entity and holds the user’s credentials like username,
password, email, and phone number. Different constructors are provided to initialize the user
object based on the authentication type being used.
This design allows the system to be flexible, reusable, and easily extendable — new
authentication methods can be added without changing the existing code in the AuthContext or
User classes.
Use Case Diagram:
User
AuthContext
<<interface>>
IAuthStrategy
BiometricAuth PasswordAuth SMSOTPAuth EmailOTPAuth
Code:
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace StrategyPattern_AuthMechanism_Assignment1
{
internal interface IAuthStrategy
{
void Authenticate(User user);
}
}
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace StrategyPattern_AuthMechanism_Assignment1
{
internal class BiometricAuth : IAuthStrategy //interface child classes
{
public void Authenticate(User user)
{
[Link]("Authenticating using Biometric");
[Link](2500);
[Link]($"Welcome!");
}
}
}
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace StrategyPattern_AuthMechanism_Assignment1
{
internal class PasswordAuth : IAuthStrategy //interface child classes
{
public void Authenticate(User user)
{
[Link]("Authenticating using USERNAME and PASSWORD");
[Link](2500);
[Link]($"Welcome!");
}
}
}
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace StrategyPattern_AuthMechanism_Assignment1
{
internal class SmsOTPAuth : IAuthStrategy //interface child classes
{
public void Authenticate(User user)
{
[Link]("Authenticating using SMS OTP");
[Link](2500);
[Link]($"Welcome!");
}
}
}
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace StrategyPattern_AuthMechanism_Assignment1
{
internal class EmailOTPAuth : IAuthStrategy //interface child classes
{
public void Authenticate(User user)
{
[Link]("Authenticating using Email OTP");
[Link](2500);
[Link]($"Welcome!");
}
}
}
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace StrategyPattern_AuthMechanism_Assignment1
{
internal class AuthContext
{
private IAuthStrategy _authStrategy;
public void setAuthStrategy(IAuthStrategy authStrategy) { _authStrategy = authStrategy; }
public void login(User user)
{
_authStrategy.Authenticate(user);
}
}
}
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace StrategyPattern_AuthMechanism_Assignment1
{
internal class User
{
private string _username { get; set; }
private string _password { get; set; }
private string _email { get; set; }
private int _number { get; set; }
//public string getUsername() { return _username; }
//public string getPassword() { return _password; }
//public string getPhone() { return _number.ToString(); }
//public string getEmail() { return _email; }
public User(string username, string password)
{
this._username = username;
this._password = password;
//this._email = email;
//this._number = number;
}
public User(int number)
{
this._number = number;
}
public User(string email)
{
this._email = email;
}
}
}
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace StrategyPattern_AuthMechanism_Assignment1
{
internal class Program
{
static void Main(string[] args)
{
//AuthContext authContext = new AuthContext();
//[Link](new PasswordAuth());
//[Link](new User("Ali bachey", "Mera naam ali bachey hai"));
//[Link](1000);
//[Link](new SmsOTPAuth());
//[Link](new User(0320990060));
//[Link](1000);
//[Link](new EmailOTPAuth());
//[Link](new User("admin@[Link]"));
//[Link](1000);
//[Link](new BiometricAuth());
//[Link](new User(19283045));
AuthContext auth = new AuthContext();
(User user, IAuthStrategy authStrategy) User_AuthType = MethodSelection();
[Link](User_AuthType.authStrategy);
[Link](User_AuthType.user);
static (User user, IAuthStrategy authStrategy) MethodSelection()
{
[Link]("===================== WELCOME BACK
==================");
[Link](
"\n1. lOGIN USING USERNAME/PASSWORD" +
"\n2. LOGIN USING SMS OTP" +
"\n3. LOGIN USING EMAIL OTP" +
"\n4. LOGIN USING BIOMETRIC"
);
ConsoleKeyInfo keyInfo = [Link]();
int userInput = [Link]([Link]());
if (userInput == 1)
{
return User_PassLogin();
}
else if (userInput == 2)
{
return SmsOTPLogin();
}
else if (userInput == 3)
{
return EmailOTPLogin();
}
else if (userInput == 4)
{
return BiometricLogin();
}
else
{
return (null, null);
}
static (User user, IAuthStrategy authStrategy) User_PassLogin()
{
[Link]("\nEnter Username: ");
string uname = [Link]();
[Link]("Enter Password: ");
string password = [Link]();
return (new User(uname, password), new PasswordAuth());
}
static (User user, IAuthStrategy authStrategy) SmsOTPLogin()
{
[Link]("\nEnter Number: ");
int number = Convert.ToInt32([Link]());
return (new User(number), new SmsOTPAuth());
}
static (User user, IAuthStrategy authStrategy) EmailOTPLogin()
{
[Link]("\nEnter Email: ");
string email = [Link]();
return (new User(email), new EmailOTPAuth());
}
static (User user, IAuthStrategy authStrategy) BiometricLogin()
{
[Link]("\nScan Biometric: ");
int number = Convert.ToInt32([Link]());
return (new User(number), new BiometricAuth());
}
}
}