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 }
// "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(); }
public static void Test() { var os = new ObjectStructure(); os.Attach(new ConcreteElementA()); os.Attach(new ConcreteElementB()); os.Accept(new ConcreteVisitorA()); os.Accept(new ConcreteVisitorB()); }
void Main() { var structure = new ObjectStructure(); structure.Add(new ElementA()); structure.Add(new ElementB()); structure.Accept(new ConcreteVisitor1()); structure.Accept(new ConcreteVisitor2()); }
public UseCase() { ObjectStructure structure = new ObjectStructure(); structure.Add(new ConcreteElementOne()); structure.Add(new ConcreteElementTwo()); structure.Accept(new ConcreteVisitorOne()); structure.Accept(new ConcreteVisitorTwo()); }
static void Main(string[] args) { //Hero h = new Ninja(); //HeroDecorator hd1 = new Knife(); //HeroDecorator hd3 = new Knife(); //HeroDecorator hd2 = new Armor(); //hd1.SetComponent(h); //hd2.SetComponent(hd1); //hd3.SetComponent(hd2); //var a = hd3.getArmor(); //var b = hd3.getDamage(); //TV receiver = new TV(); //Remote invoker = new Remote(); //Commanda command = new VolumeUp(receiver); //invoker.SetCommand(command); //invoker.Invoke(); //Context cont = new Context(); //Strategy.Strategy qstrategy = new QuickSort(); //Strategy.Strategy istrategy = new InsertionSort(); //cont.SetStrategy(qstrategy); Visitor.Visitor v1 = new ConcreteVisitorA(); Visitor.Visitor v2 = new ConcreteVisitorB(); Visitor.Person s = new Student(); Visitor.Person p = new Profesor(); ObjectStructure o = new ObjectStructure(); o.Attach(s); o.Attach(p); o.Accept(v1); o.Accept(v2); ChatMediator m = new ChatMediator(); User1 u1 = new User1(m); User2 u2 = new User2(m); m.user1 = new User1(m); m.user2 = new User2(m); Console.ReadLine(); }
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); }
private static void Main() { var structure = new ObjectStructure(); structure.Add(new ElementA()); structure.Add(new ElementB()); structure.Accept(new Visitor1()); structure.Accept(new Visitor2()); Console.ReadKey(); }
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 static void VisitorPattern() { ObjectStructure o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); ConcreteVisittor1 v1 = new ConcreteVisittor1(); ConcreteVisittor2 v2 = new ConcreteVisittor2(); 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); }
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 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); }
static void Main(string[] args) { ObjectStructure objectStructure = new ObjectStructure(); objectStructure.Add(new ConcreteElementA()); objectStructure.Add(new ConcreteElementB()); ConcreteVisitor1 cea = new ConcreteVisitor1(); ConcreteVisitor2 ceb = new ConcreteVisitor2(); objectStructure.Accept(cea); objectStructure.Accept(ceb); Console.ReadKey(); }
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(); }
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); }
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); }
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 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); }
public void TestIterator() { decimal expected = this._shopcart[0].UnitPrice * this._shopcart[0].Amount * 0.8M + this._shopcart[2].UnitPrice * this._shopcart[2].Amount; decimal actual = 0; Aggregate aggregate = new ConcreteAggregate(ProductTypeEnum.Book); this._shopcart.ForEach(prod => { aggregate.Add(prod); }); #region 改寫Visitor單元測試 IObjectStructure checkout = new ObjectStructure(); //Attach the elements into ObjectStructure checkout.Elements = aggregate.GetAll(); //Accept all the elements and execute the strategy from certain Visitor checkout.Accept(new VisitorDiscount4Count()); actual = checkout.Elements.Sum(x => x.TotalPrice); Assert.Equal(expected, actual); #endregion }
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 VisitorExample() { var structure = new ObjectStructure(); structure.Add(new ConcreteElement1()); structure.Add(new ConcreteElement2()); structure.Add(new ConcreteElement1()); structure.Accept(new ConcreteVisitor()); }
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 Start() { var car = new CarElement(); var motor = new MotorCycleElement(); ObjectStructure o = new ObjectStructure(); o.Attach(car); o.Attach(motor); // Create visitor objects ServiceVisitor vService = new ServiceVisitor(); RefuelVisitor vRefuel = new RefuelVisitor(); // Structure accepting visitors o.Accept(vService); o.Accept(vRefuel); car.GoHome(); motor.GoHome(); }
static void Main(string[] args) { // base implementation var structure = new ObjectStructure(); structure.Add(new ElementA()); structure.Add(new ElementB()); structure.Accept(new ConcreteVisitor1()); structure.Accept(new ConcreteVisitor2()); //--------------------------------- doubleDespetcher.Visitor v = new doubleDespetcher.Visitor(); Graphic g = new Dot(); // Метод accept() – переопределен, но не перегружен. А значит, связан // динамически. Поэтому реализация `accept` будет выбрана во время выполнения // уже из того класса, объект которого его вызвал (класс Dot). g.accept(v); //--------------------- Console.WriteLine("Bank sample: "); testReal(); Console.ReadLine(); }
static bool TestVisitorPattern() { Console.WriteLine("TESTING THE VISITOR DESIGN PATTERN: "); ObjectStructure o = new ObjectStructure(); o.Attach(new Element1()); o.Attach(new Element2()); var v1 = new Visitor1(); var v2 = new Visitor2(); o.Accept(v1); //Output: //Element1 visited by Visitor1 //Element2 visited by Visitor1 o.Accept(v2); //Output: //Element1 visited by Visitor2 with override behaviour //Element2 visited by Visitor2 with override behaviour return(true); }
public void ElementsVisited() { // Clear the visit history for testing _element1.VisitedBy.Clear(); _element2.VisitedBy.Clear(); _visitor1.Visited.Clear(); _visitor2.Visited.Clear(); // Accept a visit from the visitors _objectStructure.Accept(_visitor1); _objectStructure.Accept(_visitor2); // Assert that the elements have been visited by each of the visitors using the object structure Assert.IsTrue(_element1.VisitedBy.Contains(_visitor1)); Assert.IsTrue(_element1.VisitedBy.Contains(_visitor2)); Assert.IsTrue(_element2.VisitedBy.Contains(_visitor1)); Assert.IsTrue(_element2.VisitedBy.Contains(_visitor2)); // Assert that the visitors have visited each of the elements using the object structure Assert.IsTrue(_visitor1.Visited.Contains(_element1)); Assert.IsTrue(_visitor1.Visited.Contains(_element2)); Assert.IsTrue(_visitor2.Visited.Contains(_element1)); Assert.IsTrue(_visitor2.Visited.Contains(_element2)); }
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 void TestVisitorDiscount4TotalPrice() { decimal expected = this._shopcart[2].UnitPrice * this._shopcart[2].Amount * 0.9M + this._shopcart[3].UnitPrice * this._shopcart[3].Amount; decimal actual = 0; IObjectStructure checkout = new ObjectStructure(); //Attach the elements into ObjectStructure this._shopcart.Where(item => item.ProductType.Equals(ProductTypeEnum.Living)).ToList().ForEach(item => { checkout.Attach(item); }); //Accept all the elements and execute the strategy from certain Visitor checkout.Accept(new VisitorDiscount4TotalPrice()); actual = checkout.Elements.Sum(x => x.TotalPrice); Assert.Equal(expected, actual); }
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 }