0

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

1
  • I almost never hear about the proxy pattern. This is unfortunately a non-answer (which is why it's just a comment.) Of all the questions you can ask - does it work, is it easy to read, will it be easy to change - whether or not it implements that pattern matters the least. Commented Feb 15, 2022 at 18:51

1 Answer 1

1

Well, the Proxy pattern is very interesting pattern. The client using the proxy object should not notice a difference between using original object or a wrapper proxy object.

They should have the same interface and respond to the same method calls.

It is similar in implementation to the decorator pattern, but its purpose is different. Both patterns wrap an inner object, however the decorator pattern expands on the functionality of the inner object, where the proxy pattern instead governs the access to the object.

Your code simply has two different objects that are connected via composition and does not have a common interface to hide the client Object.

To implement Client Proxy you can extract the common interface say IClient and create two classes inheriting it, one client and another Sha256Client that return hash when GetInfo gets called.

All objects and their proxies should have the same methods, typically achieved via interfaces.

Sign up to request clarification or add additional context in comments.

1 Comment

understood. i have updated my code . @NazaRN

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.