// "Visitor" // "ConcreteVisitor1" public void TestVisitorPattern() { // Setup structure Element aE = new ConcreteElementA(); Element bE = new ConcreteElementB(); var o = new ObjectStructure(); o.Attach(aE); o.Attach(bE); // Create visitor objects var v1 = new ConcreteVisitor1(); var v2 = new ConcreteVisitor2(); // Structure accepting visitors o.Accept(v1); o.Accept(v2); o.Detach(aE); o.Detach(bE); // Wait for user Console.Read(); }
static void VisitorTester() { #region sample 1 // Setup structure ObjectStructure o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); // Create visitor objects ConcreteVisitor1 v1 = new ConcreteVisitor1(); ConcreteVisitor2 v2 = new ConcreteVisitor2(); // Structure accepting visitors o.Accept(v1); o.Accept(v2); #endregion #region sample 2 // Setup employee collection EmployeeVs e = new EmployeeVs(); e.Attach(new ClerkV()); e.Attach(new DirectorV()); e.Attach(new PresidentV()); // Employees are 'visited' e.Accept(new IncomeVisitor()); e.Accept(new VacationVisitor()); #endregion }
public VisitorTest() { _objectStructure = new ObjectStructure(); _element1 = new ConcreteElement1(); _element2 = new ConcreteElement2(); _visitor1 = new ConcreteVisitor1(); _visitor2 = new ConcreteVisitor2(); _objectStructure.Attach(_element1); _objectStructure.Attach(_element2); }
public Client() { ObjectStructure o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); ConcreteVisitor1 v1 = new ConcreteVisitor1(); ConcreteVisitor2 v2 = new ConcreteVisitor2(); o.Accept(v1); o.Accept(v2); }
static void Main(string[] args) { ObjectStructure o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); ConcreteVisitor1 v1 = new ConcreteVisitor1(); ConcreteVisitor2 v2 = new ConcreteVisitor2(); o.Accept(v1); o.Accept(v2); }
public void VisitorPatternCodeTest() { ConcreteElementA elementA = new ConcreteElementA(); ConcreteElementB elementB = new ConcreteElementB(); ObjectStruture struture = new ObjectStruture(); struture.Add(elementA); struture.Add(elementB); ConcreteVisitor1 v1 = new ConcreteVisitor1(); struture.Aceppt(v1); }
static void Main() { ObjectStructure o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); ConcreteVisitor1 v1 = new ConcreteVisitor1(); ConcreteVisitor2 v2 = new ConcreteVisitor2(); o.Accept(v1); o.Accept(v2); Console.ReadKey(); }
static void Main(string[] args) { ObjectStructure os = new ObjectStructure(); os.Add(new ConcreteElementA()); os.Add(new ConcreteElementB()); ConcreteVisitor1 cv1 = new ConcreteVisitor1(); ConcreteVisitor2 cv2 = new ConcreteVisitor2(); os.Accept(cv1); os.Accept(cv2); Console.Read(); }
public void Test_Visitor() { VisitorObjectStructure o = new VisitorObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); var v1 = new ConcreteVisitor1(); var v2 = new ConcreteVisitor2(); o.Accept(v1); o.Accept(v2); }
static void Main(string[] args) { ObjectStructure objectStructure = new ObjectStructure(); objectStructure.Attach(new ConcreteElementA()); objectStructure.Attach(new ConcreteElementB()); ConcreteVisitor1 concreteVisitor1 = new ConcreteVisitor1(); ConcreteVisitor2 concreteVisitor2 = new ConcreteVisitor2(); objectStructure.Accept(concreteVisitor1); objectStructure.Accept(concreteVisitor2); }
public void Run() { // Setup structure var o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); // Create visitor objects var v1 = new ConcreteVisitor1(); var v2 = new ConcreteVisitor2(); // Structure accepting visitors o.Accept(v1); o.Accept(v2); }
static void Main(string[] args) { var o = new ObjectStructure(); o.Add(new ConcreteElementA()); o.Add(new ConcreteElementB()); o.Add(new ConcreteElementB()); o.Delete(new ConcreteElementB()); var v1 = new ConcreteVisitor1(); var v2 = new ConcreteVisitor2(); o.Accept(v1); o.Accept(v2); Console.ReadKey(); }
public static void Demo(string[] args) { // Setup structure ObjectStructure o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); // Create visitor objects ConcreteVisitor1 v1 = new ConcreteVisitor1(); ConcreteVisitor2 v2 = new ConcreteVisitor2(); // Structure accepting visitors o.Accept(v1); o.Accept(v2); }
static int Main() { ObjectStructure o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); ConcreteVisitor1 v1 = new ConcreteVisitor1(); ConcreteVisitor2 v2 = new ConcreteVisitor2(); o.Accept(v1); o.Accept(v2); return(0); }
private static void Test() { var o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); var v1 = new ConcreteVisitor1(); var v2 = new ConcreteVisitor2(); o.Accept(v1); o.Accept(v2); Console.ReadKey(); }
static void Main() { var o = new ObjectStructure(); o.Add(new ConcreteElementA("A1")); o.Add(new ConcreteElementA("A2")); o.Add(new ConcreteElementB("B1")); o.Add(new ConcreteElementA("A3")); o.Add(new ConcreteElementB("B2")); var visitor1 = new ConcreteVisitor1(); o.Accept(visitor1); Console.ReadKey(); }
public static void VisitorStructural() { // Setup structure ObjectStructure o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); // Create visitor objects ConcreteVisitor1 v1 = new ConcreteVisitor1(); ConcreteVisitor2 v2 = new ConcreteVisitor2(); // Structure accepting visitors o.Accept(v1); o.Accept(v2); }
/* * Visitor permite definir uma nova operação sem mudar as classes dos elementos sobre as quais opera Entre os principais benefícios podemos citar a facilidade na * adição de novas operações que dependem de objetos complexos, reduzindo assim o acoplamento e fazendo com que o desenvolvimento foque na programação com abstrações. * * Participantes: * Visitor - declara uma operação Visit para cada ConcreteElement na estrutura do objeto; O visitante pode acessar o elemento diretamente através da interface específica. * ConcreteVisitor - implementa cada operação (fragmento do algoritmo) declarada por Visitor; * Element - define a operação Accept que aceita o visitante; * ConcreteElement - implementa a operação Accept; * ObjectStructure - pode enumerar seus elementos; * */ public void Visitor() { // configurar a estrutura ObjectStrutcture obj = new ObjectStrutcture(); obj.Attach(new ConcreteElementA()); obj.Attach(new ConcreteElementB()); // criar os visitors ConcreteVisitor1 v1 = new ConcreteVisitor1(); ConcreteVisitor2 v2 = new ConcreteVisitor2(); // estrutura aceitar visitors obj.Accept(v1); obj.Accept(v2); Response.Write(WriterMessages.GetAllMessages()); }
public void VisitorTest() { var components = new List <IComponent> { new ConcreteComponentA(), new ConcreteComponentB() }; // The client code works with all visitors via the base Visitor interface var visitor1 = new ConcreteVisitor1(); Client.ClientCode(components, visitor1).Should().Be("AB"); // It allows the same client code to work with different types of visitors var visitor2 = new ConcreteVisitor2(); Client.ClientCode(components, visitor2).Should().Be("AB"); }
public static void TestVisitors() { var o = new ObjectStructure(); var celA = new ConcreteElementA(); var celB = new ConcreteElementB(); Assert.AreSame(o.Attach(celA, true), celA); Assert.AreSame(o.Attach(celB, true), celB); var v1 = new ConcreteVisitor1(); var v2 = new ConcreteVisitor2(); var test1 = o.Accept(v1, true); StringAssert.AreEqualIgnoringCase(test1, "ConcreteElementA visited by ConcreteVisitor1\r\nConcreteElementB visited by ConcreteVisitor1\r\n"); var test2 = o.Accept(v2, true); StringAssert.AreEqualIgnoringCase(test2, "ConcreteElementA visited by ConcreteVisitor2\r\nConcreteElementB visited by ConcreteVisitor2\r\n"); }
static void Main() { // Setup structure ObjectStructure o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); // Create visitor objects ConcreteVisitor1 v1 = new ConcreteVisitor1(); ConcreteVisitor2 v2 = new ConcreteVisitor2(); // Structure accepting visitors o.Accept(v1); o.Accept(v2); // Wait for user Console.Read(); }
//Відвідувач - Visitor public Run Visitor() { Console.WriteLine("\nVisitor:"); // Setup structure ObjectStructure o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); // Create visitor objects ConcreteVisitor1 v1 = new ConcreteVisitor1(); ConcreteVisitor2 v2 = new ConcreteVisitor2(); // Structure accepting visitors o.Accept(v1); o.Accept(v2); return(this); }
public void Get() { List <IComponent> components = new List <IComponent> { new ConcreteComponentA(), new ConcreteComponentB() }; Console.WriteLine("The client code works with all visitors via the base Visitor interface:"); var visitor1 = new ConcreteVisitor1(); Client.ClientCode(components, visitor1); Console.WriteLine(); Console.WriteLine("It allows the same client code to work with different types of visitors:"); var visitor2 = new ConcreteVisitor2(); Client.ClientCode(components, visitor2); }
static void Main(string[] args) { #region Strategy //First Step Console.WriteLine("Initialize Strategy"); long[] inputArray = new long[20]; Random radom = new Random(); for (int strategy = 0; strategy < inputArray.Length; strategy++) { inputArray[strategy] = radom.Next(100); } foreach (long number in inputArray) { Console.WriteLine(number); } Console.ReadKey(); //Second Step //Strategy 1 var alg = new BubbleSort(); alg.Sort(inputArray); Console.WriteLine("sort numbers"); foreach (long number in inputArray) { Console.WriteLine(number); } Console.ReadKey(); //Strategy 2 var alg2 = new SelectionSort(); alg2.Sort(inputArray); Console.WriteLine("sort numbers"); foreach (long number in inputArray) { Console.WriteLine(number); } Console.ReadKey(); //Apply Strategy Patterns Strategy.Context ctx = new Strategy.Context(new SelectionSort()); ctx.ContextInterface(inputArray); Console.WriteLine("sort numbers"); foreach (long number in inputArray) { Console.WriteLine(number); } Console.ReadKey(); Console.WriteLine("Finalize Strategy" + Environment.NewLine); #endregion #region ChainOfResponsability Console.WriteLine("ChainOfResponsability Initialize"); // First Step Validate validate = new Validate(); Console.WriteLine(validate.ValidateUser("Test", "Test").ToString()); ///Apply ChainOfResponsability pattern ChainOfResponsability.Form valform = new ChainOfResponsability.Form(); ChainOfResponsability.Server valserver = new ChainOfResponsability.Server(); BD valBD = new BD(); valform.setSucessor(valserver); valserver.setSucessor(valBD); Console.WriteLine(valform.ValidateUser("Teste", "Teste").ToString()); Console.WriteLine("ChainOfResponsability Finalize" + Environment.NewLine); #endregion #region Command Console.WriteLine("Command Initialize"); //Configure Receiver Command.Server server = new Command.Server(); //Create command and configure receiver. CommandAbstract cmd = new ServerCommand(server); //Configure invoker Command.Formulario form = new Command.Formulario(); form.SetCommand(cmd); form.ClickValidate(); Console.WriteLine("Command Finalize" + Environment.NewLine); Console.ReadLine(); #endregion #region Iterator Console.WriteLine("Iterator Initialize"); //Create concrete aggregate Team team = new Team(); team[0] = "Luiz"; team[0] = "Alex"; team[0] = "Rodrigo"; team[0] = "Renan"; ConcreteIterator i = new ConcreteIterator(team); Console.WriteLine("Show team's members"); Object item = i.First(); while (item != null) { Console.WriteLine(item); item = i.Next(); } Console.WriteLine("Iterator Finalize" + Environment.NewLine); Console.ReadLine(); #endregion #region Mediator Console.WriteLine("Mediator Initialize"); ConcreteMediator concreteMediator = new ConcreteMediator(); Support support = new Support(concreteMediator); User user = new User(concreteMediator); concreteMediator.Suporte = support; concreteMediator.Usuario = user; support.Send("Hello user"); user.Send("Hello support"); Console.WriteLine("Mediator Finalize" + Environment.NewLine); Console.ReadLine(); #endregion #region Memento Console.WriteLine("Memento Initialize"); //Create originator Originator people = new Originator(); people.State = "Bored"; //Create caretaker Caretaker caretaker = new Caretaker(); caretaker.Memento = people.CreateMemento(); people.State = "Happy"; Console.WriteLine("Actual State:" + people.State); people.setMemento(caretaker.Memento); Console.WriteLine("Restore State: " + people.State); Console.WriteLine("Memento Finalize" + Environment.NewLine); #endregion #region Observer Console.WriteLine("Observer Initialize"); Balance balanco = new Balance(); Sale venda = new Sale(balanco); balanco.Attach(venda); balanco.Iniciar(); balanco.Notify(); balanco.Finalizar(); balanco.Notify(); venda.Iniciar(); //After remove observer balanco.Detach(venda); balanco.Iniciar(); balanco.Notify(); venda.Iniciar(); Console.WriteLine("Observer Finalize" + Environment.NewLine); Console.ReadLine(); #endregion #region State Console.WriteLine("State Initialize"); Connection connection = new Connection(new ConnectionOpened()); connection.Open(); connection.Close(); Console.WriteLine("State Finalize" + Environment.NewLine); Console.ReadLine(); #endregion #region Template Method Console.WriteLine("Template Method Initialize"); Correction proofCorrecion = new ProofCorrection(); proofCorrecion.Process(); Correction writingCorrection = new WritingCorrection(); writingCorrection.Process(); Console.WriteLine("Template Method Finalize" + Environment.NewLine); Console.ReadLine(); #endregion #region Visitor Console.WriteLine("Visitor Initialize"); //Config structure ObjectStructure objectStructure = new ObjectStructure(); objectStructure.Attach(new ConcreteElementA()); objectStructure.Attach(new ConcreteElementB()); //Create Visitors ConcreteVisitor1 concreteVisitor1 = new ConcreteVisitor1(); ConcreteVisitor2 concreteVisitor2 = new ConcreteVisitor2(); objectStructure.Accept(concreteVisitor1); objectStructure.Accept(concreteVisitor2); Console.WriteLine("Visitor Finalize" + Environment.NewLine); Console.ReadLine(); #endregion #region Interpreter Console.WriteLine("Interpreter Initialize"); string roman = "MCMXXVIII"; Interpreter.Context context = new Interpreter.Context(roman); List <Expression> tree = new List <Expression>(); tree.Add(new ThousandExpression()); tree.Add(new HundredExpression()); tree.Add(new TenExpression()); tree.Add(new OneExpression()); foreach (Expression exp in tree) { exp.Interpret(context); } Console.WriteLine("{0} = {1}", roman, context.Output); Console.WriteLine("Interpreter Finalize" + Environment.NewLine); Console.ReadKey(); #endregion }