Visitor – It allows the program changes the algorithm and the operations that base on the visitor to change, which means can separate the algorithms from objects on which they operate.
From Design Pattern – Behavioral Patterns
Structure
Applicability
Pros and Cons
PROS
CONS
How to Implement
#1 Basic Sample
static void Main(string[] args)
{
DataStructure dataStructure = new DataStructure();
dataStructure.Attach(new ConcreteComponentA());
dataStructure.Attach(new ConcreteComponentB());
Console.WriteLine("The client code works with all visitors via the base Visitor interface:");
var Visitor1 = new ConcreteVisitor1();
DataStructure.Accept(Visitor1);
Console.WriteLine("It allows the same client code to work with different types of visitors:");
var Visitor2 = new ConcreteVisitor2();
DataStructure.Accept(Visitor2);
Console.ReadKey();
}
class DataStructure
{
private static List<IComponent> components = new List<IComponent>();
public void Attach(IComponent component) => components.Add(component);
public void Detach(IComponent component) => components.Remove(component);
public static void Accept(IVisitor visitor)
{
foreach (IComponent component in components)
{
component.Accept(visitor);
}
}
}
interface IComponent
{
void Accept(IVisitor visitor);
}
class ConcreteComponentA : IComponent
{
public void Accept(IVisitor visitor)
{
visitor.VisitConcreteComponentA(this);
}
public string ExclusiveMethodOfConcreteComponentA() => "A";
}
class ConcreteComponentB : IComponent
{
public void Accept(IVisitor visitor)
{
visitor.VisitConcreteComponentB(this);
}
public string SpecialMethodOfConcreteComponentB() => "B";
}
interface IVisitor
{
void VisitConcreteComponentA(ConcreteComponentA element);
void VisitConcreteComponentB(ConcreteComponentB element);
}
class ConcreteVisitor1 : IVisitor
{
public void VisitConcreteComponentA(ConcreteComponentA element)
{
Console.WriteLine(element.ExclusiveMethodOfConcreteComponentA() + " + ConcreteVisitor1");
}
public void VisitConcreteComponentB(ConcreteComponentB element)
{
Console.WriteLine(element.SpecialMethodOfConcreteComponentB() + " + ConcreteVisitor1");
}
}
class ConcreteVisitor2 : IVisitor
{
public void VisitConcreteComponentA(ConcreteComponentA element)
{
Console.WriteLine(element.ExclusiveMethodOfConcreteComponentA() + " + ConcreteVisitor2");
}
public void VisitConcreteComponentB(ConcreteComponentB element)
{
Console.WriteLine(element.SpecialMethodOfConcreteComponentB() + " + ConcreteVisitor2");
}
} #2 Web Application Project
Step 1 Create three interface class files. One interface class file name sets “IVisitorServices.” Another file name sets “IVisitor.” Other file name sets “IVisitorComponent.” These files inherit the IDispose interface class.
Step 2 Create two class object files about the “IVisitorComponent.” One class file name sets “ConcreteVisitorComponentA.” Other file name sets “ConcreteVisitorComponentB.” These files inherit the “IVisitorComponent.”
Step 3 Create two class object files about the “IVisitor.” One class file name sets “ConcreteVistor1.” Other file name set “ConcreteVisitor2.” These files are intended for use by the visitor viewpoint to watch other object class files and inherit the “IVisitor.”
Step 4 Create a class file named “VisitorObjStructure,” which means the business logic layer(BLL) connects to the visitor viewpoints who want to watch what kind of visitor component.
Step 5 Create the visitor pattern-related files in the web project, such as the controller files, the view flies, then register the “IVisitorServices” class files and the “VisitorServices” class files in the Startup.cs.
// Visitor
services.AddScoped(typeof(IVisitorServices<>), typeof(VisitorServices<>)); Reference
What is the API gateway? An API gateway is an API management tool that sits…
The .NET 5 SDK is a kind of milestone in the .NET world. The .NET…
In object-oriented design, the dependency inversion principle is a specific methodology for loosely coupling software…
In the field of software engineering, the interface segregation principle (ISP) states that no code…
Subtype Requirement: Let be a property provable about objects of type T. Then should be true for objects of type S where S is…
In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be…