static void Main() { ConcreteAggregate aggregate = new ConcreteAggregate(); aggregate[0] = "Item A"; aggregate[1] = "Item B"; aggregate[2] = "Item C"; aggregate[3] = "Item D"; aggregate[4] = "Bob"; // Create Iterator and provide aggregate ConcreteIterator it = new ConcreteIterator(aggregate); Console.WriteLine("Iterating over collection:"); object item = it.First(); while (item != null) { Console.WriteLine(item); item = it.Next(); } // Wait for user Console.ReadKey(); }
// "Aggregate" public void TestIteratorPattern() { var a = new ConcreteAggregate(); a[0] = "Item A"; a[1] = "Item B"; a[2] = "Item C"; a[3] = "Item D"; // Create Iterator and provide aggregate var i = new ConcreteIterator(a); Console.WriteLine("Iterating over collection:"); object item = i.First(); while (item != null) { Console.WriteLine(item); item = i.Next(); } // Wait for user Console.Read(); }
public Client() { _iterator = new ConcreteIterator(_aggregate); _iterator.First(); _iterator.Siguiente(); _iterator.Current(); _iterator.IsDone(); }
public override void GetAllOrders() { ConcreteAggregate a = new ConcreteAggregate(); for (int j = 0; j < _orders.Count; j++) { a[j] = _orders[j]; } ConcreteIterator i = a.CreateIterator(); object item = i.First(); while (item != null) { Console.WriteLine(item); item = i.Next(); } }
// Start is called before the first frame update void Start() { ConcreteAggregate a = new ConcreteAggregate(); a[0] = "大鸟"; a[1] = "小菜"; a[2] = "行李"; Iterator i = new ConcreteIterator(a); object item = i.First(); while (!i.IsDone()) { Debug.Log(i.CurrentItem() + "来了"); i.Next(); } }
public static void IteratorPattern() { ConcreteAggregate a = new ConcreteAggregate(); a[0] = "张三"; a[1] = "李四"; a[2] = "王五"; a[3] = "赵六"; ConcreteIterator i = new ConcreteIterator(a); object item = i.First(); while (!i.IsDone()) { Console.WriteLine($"{i.CurrentItem()}请先买票"); i.Next(); } Console.Read(); }
static void Main(string[] args) { ConcreteAggregate aggregate = new ConcreteAggregate(); aggregate[0] = "Item 1"; aggregate[1] = "Item 2"; aggregate[2] = "Item 3"; aggregate[3] = "Item 4"; ConcreteIterator iterator = new ConcreteIterator(aggregate); object item = iterator.First(); while (!iterator.IsEnd()) { Console.WriteLine(item); item = iterator.Next(); } Console.Read(); }
/* * * A intenção do padrão Iterator é fornecer uma maneira de acessar os elementos de um objeto agregado sequencialmente sem expor sua representação. * * Participantes: * Iterator - define uma interface para acessar e percorrer os elementos * ConcreteIterator - implementa a interface do Iterator; mantém o controle da posição corrente no percurso do agregado; * Aggregate - define uma interface para criação de um objeto Iterator; * ConcreteAggregate - implementa a interface de criação do Iterator para retornar uma instância do ConcreteCreator apropriado; * */ //Client public void Iterator() { //Cria Concrete Aggregate Equipe equipe = new Equipe(); equipe[0] = "x"; equipe[1] = "y"; equipe[2] = "z"; equipe[3] = "zz"; ConcreteIterator i = new ConcreteIterator(equipe); Response.Write("Listando membros da equipe:"); Object item = i.First(); while (item != null) { Response.Write("<br>" + item); item = i.Next(); } }
public static void IteratorStructural() { ConcreteAggregate a = new ConcreteAggregate(); a[0] = "Item A"; a[1] = "Item B"; a[2] = "Item C"; a[3] = "Item D"; // Create Iterator and provide aggregate ConcreteIterator i = new ConcreteIterator(a); Log.WriteLine("Iterating over collection:"); object item = i.First(); while (item != null) { Log.WriteLine(item.ToString()); item = i.Next(); } }
/// <summary> /// Entry point into console application. /// </summary> static void Demo() { ConcreteAggregate a = new ConcreteAggregate(); a[0] = "Item A"; a[1] = "Item B"; a[2] = "Item C"; a[3] = "Item D"; // Create Iterator and provide aggregate ConcreteIterator i = new ConcreteIterator(a); Console.WriteLine("Iterating over collection:"); object item = i.First(); while (item != null) { Console.WriteLine(item); item = i.Next(); } // Wait for user Console.ReadKey(); }
//迭代器 public static void testIterator() { ConcreteAggregate a = new ConcreteAggregate(); a[0] = "大鸟"; a[1] = "小菜"; a[2] = "行李"; a[3] = "老外"; a[4] = "公交内部员工"; a[5] = "小偷"; Iterator i = new ConcreteIterator(a); //Iterator i = new ConcreteIteratorDesc(a); object item = i.First(); while (!i.IsDone()) { Console.WriteLine("{0} 请买车票!", i.CurrentItem()); i.Next(); } Console.Read(); }
public static void Main(string[] args) { ConcreteAggregate a = new ConcreteAggregate(); a[0] = "Item A"; a[1] = "Item B"; a[2] = "Item C"; a[3] = "Item D"; // Create Iterator and provide aggregate ConcreteIterator i = new ConcreteIterator(a); // Iterate over collection object item = i.First(); while( item != null ) { Console.WriteLine( item ); item = i.Next(); } }
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 }