i am learning Design pattern and i want to check if the usage of proxy pattern is legit in my code.
first of all i have an interface :
public interface Iclient { string GetCard(); } THE CLIENT CLASS:
public class Client:Iclient { public string Name { get; set; } public string Card { get; set; } public int CardMoney { get; set; } public Client(string name, string card,int money) { this.Name = name; this.Card = card; this.CardMoney = money; } public Client(string name, string card) { this.Name = name; this.Card = card; } public string GetInfo() { return this.Name + "-" + this.Card + "-" + this.CardMoney; } public string GetCard() { return this.Card; } i also have a Bank.txt file to check if the card number is available.
now, the proxy code :
public class Sha256Client:Iclient { private Client client; public Sha256Client(Client emp) { client = emp; } public string GetCard() { using (SHA256 sha256Hash = SHA256.Create()) { byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(client.Card)); // Convert byte array to a string //X2 is for using hexadecimal StringBuilder builder = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { builder.Append(bytes[i].ToString("x2")); } return builder.ToString(); } } } the implementatino of the Proxy pattern :
Client c1 = new Client(NAME.Text, CARDnbr.Text); Sha256Client encrypt = new Sha256Client(c1); the function ComputeSha256Hash() returns the hashed code of the Card and compares it with each line of the hashed codes in Bank.txt