mardi 20 septembre 2016

understanding Visitor design pattern in c#

I have the below classes for datastructure and logic.

public class clsCustomer : ICustomerElements
    {
        public void accept(IVisitor objVisitor)
        {
            objVisitor.visit(this);

            foreach (clsAddress objAddress in objAddresses)
            {
                objAddress.accept(objVisitor);
            }
        }
        public string strCustomerName = "";
        public ArrayList objAddresses = new ArrayList();
    }

 public class clsVisitorString : IVisitor
    {
        public string strData;
        public void visit(clsCustomer obj)
        {
            strData = "Customer Name :- " + obj.strCustomerName + "\r\n";
        }
        public void visit(clsAddress obj)
        {
            strData = strData + "Address 1 :- " + obj.strAddress1 + "\r\n";
            strData = strData + "Address 2 :- " + obj.strAddress2 + "\r\n";
        }
        public void visit(clsPhone obj) 
        {
            strData = strData + "Phone :- " + obj.strPhoneNumber + "\r\n";
        }
    }

  1. So as per the visitor design pattern, the logic should be decoupled from the other layers. But here if i change anything in the clscustomer class it will impact the visitor class also.
  2. Is this the right approach ?
  3. will this not break the Open close principle?

Aucun commentaire:

Enregistrer un commentaire