private static void Main(string[] args) { //SequenceAggregate list = new SequenceAggregate(); ReverseAggregate list = new ReverseAggregate(); list[0] = "张三"; list[1] = "李四"; list[2] = "王五"; list[3] = "赵六"; list[4] = "周七"; IIterator i = list.GetIterator(); object item = i.First(); Console.WriteLine("First:" + item); while (i.Current() != i.Last()) { Console.WriteLine(i.Current()); i.Next(); } Console.WriteLine("Last:" + i.Last()); Console.ReadLine(); }
private void CheckBtn_Click(object sender, RoutedEventArgs e) { if (iterator.Current().answer == answerBox.Text) { checkBtn.Background = Brushes.Green; canNext = true; } else { checkBtn.Background = Brushes.Red; canNext = false; } }
static void Main(string[] args) { ConcreteAggregate aggregate = new ConcreteAggregate(); aggregate.Add(new Category() { Name = "Csharp" }); aggregate.Add(new Category() { Name = "Asp.Net" }); aggregate.Add(new Category() { Name = "EntityFramework" }); aggregate.Add(new Category() { Name = ".NET CORE" }); IIterator iterator = aggregate.CreateIterator(); Console.WriteLine($"İlk eleman : {((Category)iterator.First()).Name}"); Console.WriteLine($"Son eleman : {((Category)iterator.Last()).Name}"); while (iterator.MoveNext()) { Console.WriteLine(((Category)iterator.Current()).Name); } Console.ReadLine(); }
public void TestEnumerator() { Node <int> root = new Node <int>(1); root.Childs.Add(new Node <int>(2)); root.Childs.Add(new Node <int>(3)); var node4 = new Node <int>(4); root.Childs.Add(node4); node4.Childs.Add(new Node <int>(5)); node4.Childs.Add(new Node <int>(6)); node4.Childs.Add(new Node <int>(7)); IEnumer <Node <int> > graphEnumer = new Graph <int>(root); IIterator <Node <int> > iterator = graphEnumer.Iterator(); List <int> acutal = new List <int>(); while (iterator.MoveNext()) { var currentNode = iterator.Current(); acutal.Add(currentNode.Value); } int[] expected = new int[] { 1, 2, 3, 4, 5, 6, 7 }; Assert.Equal(expected, acutal.ToArray()); }
public static void Traverse(IIterator iterator) { while (iterator.MoveNext()) { MusicFestival festival = iterator.Current(); Console.WriteLine($"Music festival {festival.Name} takes place in {festival.Country}."); } }
public static void Main() { var tree = new CompleteBinaryTree <int>(1); tree.AddNode(2).AddNode(3).AddNode(5).AddNode(7).AddNode(11).AddNode(13).AddNode(17); IIterator <int> iterator = tree.GetIterator(); while (iterator.HasNext) { Console.WriteLine(iterator.Current()); } }
static void Main(string[] args) { DatetimeAggregate dateTimeRange = new DatetimeAggregate(); dateTimeRange.startDate = new DateTime(2014, 05, 14); dateTimeRange.endDate = new DateTime(2014, 10, 14); IIterator iterator = dateTimeRange.CreateIterator(); while (iterator.ReadNext()) { Console.WriteLine(iterator.Current().ToString("yyyy.MM.dd")); } Console.ReadLine(); }
private static void Iterator() { ProductHistory productHistory = new ProductHistory(); productHistory.Push("a"); productHistory.Push("b"); productHistory.Push("c"); IIterator <string> it = productHistory.CreateIterator(); while (it.HasNext()) { string product = it.Current(); Console.WriteLine(product); it.Next(); } }
public IteratorPatternMain() { var history = new BrowseHistory(); history.Push("a"); history.Push("b"); history.Push("c"); IIterator <string> historyIterator = history.CreateIterator(); while (historyIterator.HasNext()) { Console.WriteLine(historyIterator.Current()); historyIterator.Next(); } }
public void TestIterator() { var aggregate = new ConcreteAggregateB(); aggregate.Add("Item 1"); aggregate.Add("Item 2"); aggregate.Add("Item 3"); IIterator iterator = aggregate.GetIterator(); while (!iterator.IsDone()) { var currentItem = iterator.Current(); currentItem = iterator.Next(); } }
static void Iterator() { BrowserHistory browserHistory = new BrowserHistory(); browserHistory.Push("edgias.com"); browserHistory.Push("apps.edgias.com"); browserHistory.Push("sales.edgias.com"); IIterator iterator = browserHistory.CreateIterator(); while (iterator.HasNext()) { var url = iterator.Current(); Console.WriteLine(url); iterator.Next(); } }
public static void Main(string[] args) { IterableCollection iterableCollection = new IterableCollection(); iterableCollection.Push("a"); iterableCollection.Push("b"); iterableCollection.Push("c"); IIterator <string> iterator = iterableCollection.CreateIterator(); while (iterator.HasNext()) { var value = iterator.Current(); Console.WriteLine(value); iterator.Next(); } }
static void Main(string[] args) { //first build an army - two older robots and three grand new ones IRobotBuilder olderRobotBuilder = OlderRobotBuildFactory.Instance.CreateBuilder(); RobotCreator robotCreator = new RobotCreator(olderRobotBuilder); Robot olderRobot1 = robotCreator.Create(); olderRobot1.Name = "Older Robot 1"; Robot olderRobot2 = (Robot)olderRobot1.Clone(); olderRobot2.Name = "Older Robot 2"; IRobotBuilder grandNewRobotBuilder = GrandNewGenerationRobotBuilderFactory.Instance.CreateBuilder(); robotCreator = new RobotCreator(grandNewRobotBuilder); Robot grandNewRobot1 = robotCreator.Create(); grandNewRobot1.Name = "Grand new robot number 1"; Robot grandNewRobot2 = (Robot)grandNewRobot1.Clone(); grandNewRobot2.Name = "Grand new robot number 2"; Robot grandNewRobot3 = (Robot)grandNewRobot1.Clone(); grandNewRobot3.Name = "Grand new robot number 3"; RobotArmy army = new RobotArmy(); //const int robotNum = 5; army.Add(olderRobot1); army.Add(olderRobot2); army.Add(grandNewRobot1); army.Add(grandNewRobot2); army.Add(grandNewRobot3); //review troops IIterator iter = army.CreateIterator(); iter.First(); //move to first elem while (iter.HasNext()) { Console.WriteLine((Robot)iter.Current()); iter.Next(); } }
public IEnumerable <Route> Solve(IGraphDatabase graph, City from, City to) { Dictionary <City, Route> routes = new Dictionary <City, Route>(); routes[from] = null; Queue <City> queue = new Queue <City>(); queue.Enqueue(from); do { City city = queue.Dequeue(); IIterator <Route> outgoing = graph.GetRoutesFrom(city); /* * For each outgoing route from city... */ while (outgoing.MoveNext()) { Route route = outgoing.Current(); /* Change to current Route*/ if (routes.ContainsKey(route.To)) { continue; } routes[route.To] = route; if (route.To == to) { break; } queue.Enqueue(route.To); } } while (queue.Count > 0); if (!routes.ContainsKey(to)) { return(null); } List <Route> result = new List <Route>(); for (Route route = routes[to]; route != null; route = routes[route.From]) { result.Add(route); } result.Reverse(); return(result); }
public static void Run() { ReadingList readingList = new ReadingList(); readingList.Add("C# For Professionals"); readingList.Add("Sapiens, A Brief History Of Humankind"); readingList.Add("Head First Design Patterns"); readingList.Add("Thinking, Fast and Slow"); readingList.Add("A Random Walk Down Wall Street"); IIterator iterator = readingList.CreateIterator(); while (iterator.HasNext()) { System.Console.WriteLine(iterator.Current()); iterator.Next(); } }
static void Main(string[] args) { ConcreteAggregate aggregate = new ConcreteAggregate(); aggregate.Add("A1"); aggregate.Add("A2"); aggregate.Add("A3"); aggregate.Add("A4"); aggregate.Add("A5"); aggregate.Add("A6"); IIterator iterator = aggregate.GetIterator(); iterator.Reset(); while (iterator.IsLast() == false) { Console.WriteLine(iterator.Current()); iterator.Next(); } Console.ReadLine(); }
protected TripDay[] PrepareTrip(int days) { TripDay[] trip = new TripDay[days]; for (int i = 0; i < days; i++) { Attraction[] attr = new Attraction[3]; for (int j = 0; j < 3; j++) { attractions.Move(); AttractionData a = attractions.Current(); attr[j] = new Attraction(int.Parse(a.Price), int.Parse(a.Rating), a.Name); } rooms.Move(); RoomData r = rooms.Current(); Room room = new Room(int.Parse(r.Price), int.Parse(r.Rating), r.Name); trip[i] = new TripDay(room, attr); } return(trip); }
private void PopulateClosedAnswers(List <string> list) { Options.Children.Clear(); Random random = new Random(); list.Insert(random.Next(0, list.Count), iterator.Current().answer); foreach (string item in list) { RadioButton radioButton = new RadioButton() { Content = item, GroupName = "test" }; radioButton.Checked += RadioButton_Checked; Options.Children.Add(radioButton); } }
public static void Behavioral_Iterator() { var history = new BrowseHistory(); history.Push("a"); history.Push("b"); history.Push("c"); IIterator <String> iterator = history.CreateIterator(); while (iterator.HasNext()) { var url = iterator.Current(); Console.WriteLine(url); iterator.Next(); } //for(var i=0; i < history.GetUrls().Count; i++) //{ // var url = history.GetUrls()[i]; // Console.WriteLine(url); //} }
public Route Current() { return(iterator.Current()); }
public T Current() { return(it.Current().Current()); }
public T Current() { return(iterator.Current()); }
private void Run_Test(object sender, RoutedEventArgs e) { command.Execute(); OptionGrid.Visibility = Visibility.Collapsed; if (type == "Otwarty") { IBuilder builder = new TestOpenBuilder( skill == "Bardzo łatwy" ? new FactoryVeryEasy() : skill == "Łatwy" ? new FactoryEasy() : skill == "Średni" ? new FactoryNormal() : new FactoryHard() as AbstractFactory, strategy); user.MakeNewTest(builder); this.test = builder.PrintTest(); Open.Visibility = Visibility.Visible; ConcreteAggregate concreteAggregate = new ConcreteAggregate(); concreteAggregate.AddQuestions(test.questionChooseStrategy.GetQuestions("write", test.level.Nubmer)); iterator = concreteAggregate.CreateIterator(random.Next(3)); if (iterator.HasNext()) { Question question = iterator.Next(); if (way == "pol->ang") { question = new HintPolish(question, Database.GetPolishHint(question.ID)); PolishOpen.ToolTip = (question as HintPolish).GetHint(); } else { question.ChangeSolvingWay(); question = new HintEnglish(question, Database.GetEnglishHint(question.ID)); PolishOpen.ToolTip = (question as HintEnglish).GetHint(); } PolishOpen.Content = question.question; } } else if (type == "Zamkniety") { IBuilder builder = new TestCloseBuilder( skill == "Bardzo łatwy" ? new FactoryVeryEasy() : skill == "Łatwy" ? new FactoryEasy() : skill == "Średni" ? new FactoryNormal() : new FactoryHard() as AbstractFactory, strategy); user.MakeNewTest(builder); this.test = builder.PrintTest(); Test.Visibility = Visibility.Visible; ConcreteAggregate concreteAggregate = new ConcreteAggregate(); concreteAggregate.AddQuestions(test.questionChooseStrategy.GetQuestions("single", test.level.Nubmer)); iterator = concreteAggregate.CreateIterator(random.Next(3)); if (iterator.HasNext()) { Question question = iterator.Next(); if (way == "pol->ang") { question = new HintPolish(question, Database.GetPolishHint(question.ID)); Polish.ToolTip = (question as HintPolish).GetHint(); } else { question.ChangeSolvingWay(); question = new HintEnglish(question, Database.GetEnglishHint(question.ID)); Polish.ToolTip = (question as HintEnglish).GetHint(); } if (way == "pol->ang") { (iterator.Current() as CloseQuestion).GetWrongAnswers(false, test.level.Nubmer + 1, test.level.Nubmer); } else { (iterator.Current() as CloseQuestion).GetWrongAnswers(true, test.level.Nubmer + 1, test.level.Nubmer); } PopulateClosedAnswers((iterator.Current() as CloseQuestion).wrongAnswers); Polish.Content = question.question; } } }
static void Main(string[] args) { Console.WriteLine("Enter 1 for Factory Pattern. \n" + "Enter 2 for Observer Pattern."); int choice = Convert.ToInt32(Console.ReadLine()); switch (choice) { case 1: var ef = new EmployeeFactory(); Console.WriteLine(ef.GetEmployee(1).GetBonus()); Console.ReadLine(); break; case 2: var observable = new TemperatureMonitor(); var observer = new TemperatureReporter(); observer.Subscribe(observable); observable.GetTemperature(); break; case 3: var editor = new Editor(); var history = new Momento.History(); editor.SetContent("a"); history.Push(editor.CreateState()); editor.SetContent("b"); history.Push(editor.CreateState()); editor.SetContent("c"); history.Push(editor.CreateState()); editor.Restore(history.Pop()); editor.Restore(history.Pop()); Console.WriteLine(editor.GetContent()); break; case 4: Canvas canvas = new Canvas(); canvas.SelectTool(new BrushTool()); canvas.MouseDown(); canvas.MouseUp(); break; case 5: BrowseHistory browseHistory = new BrowseHistory(); browseHistory.Push("www.google.com"); browseHistory.Push("www.yahoo.com"); browseHistory.Push("www.reddif.com"); browseHistory.Push("www.youtube.com"); IIterator <string> iterator = browseHistory.CreateIterator(); while (iterator.HasNext()) { var url = iterator.Current(); Console.WriteLine(url); iterator.next(); } break; case 6: //The difference between State and Strategy pattern is that in state pattern there is only a single state of the object and the behaviour is determined by the implementation injected. //In strategy pattern there could be multiple behaviours in form of multiple properties inside class such as IFilter & ICompression. The implementation injected further changes the behaviour. PhotoProcessor photoProcessor = new PhotoProcessor(new BnW(), new JPEG()); photoProcessor.ProcessPhoto(); break; case 7: //template AbstractPreFlightCheckList flightChecklist = new F16PreFlightCheckList(); flightChecklist.runChecklist(); break; case 8: //command var service = new CustomerService(); var command = new AddCustomerCommand(service); var button = new Command.Button(command); button.click(); var composite = new CompositeCommand(); composite.Add(new ResizeCommand()); composite.Add(new BlackAndWHiteCommand()); var button2 = new Command.Button(composite); button2.click(); var commandHisotry = new Command.Undo.History(); var doc = new Command.Undo.HtmlDocument(); doc.SetContent("Hello World"); var boldCommand = new BoldCommand(doc, commandHisotry); boldCommand.Execute(); Console.WriteLine(doc.GetContent()); var undoCommand = new UndoCommand(commandHisotry); undoCommand.Execute(); Console.WriteLine(doc.GetContent()); break; case 9: //Observer DataSource dataSource = new DataSource(); dataSource.AddObserver(new Chart()); dataSource.AddObserver(new SpreadSheet(dataSource)); dataSource.SetValue("value changed"); break; case 10: //Mediator //the pattern is applied to encapsulate or centralize the interactions amongst a number of objects var dialog = new ArticlesDialogBox(); dialog.SimulateUsserInteraction(); break; case 11: //Chain of Responsibility //autehnticator --> logger --> compressor --> null var compressor = new Compressor(null); var logger = new Logger(compressor); var authenticator = new Authenticator(logger); var server = new WebServer(authenticator); server.handle(new HttpRequest() { UserName = "******", Password = "******" }); break; case 12: //Visitor var document = new Visitor.HtmlDocument(); document.Add(new HeadingNode()); document.Add(new AnchorNode()); document.Execute(new HighlighOperation()); break; case 13: // Composite var shape1 = new Shape(); var shape2 = new Shape(); var group1 = new Group(); group1.Add(shape1); group1.Add(shape2); var group2 = new Group(); var shape3 = new Shape(); group2.Add(shape3); group1.Add(group2); group1.render(); break; case 14: //Adapter Image image = new Image(); ImageViewer imageViewer = new ImageViewer(image); imageViewer.Apply(new SepiaFilter()); imageViewer.Apply(new FancyAdapter(new FancyFilter())); break; case 15: //Decorator var cloudStream = new CloudStream(); var encryptData = new EncryptStream(cloudStream); var compressData = new CompressStream(encryptData); compressData.write("some random data"); break; case 16: //Facade NotificationService notificationService = new NotificationService(); notificationService.Send("Hello..", "17.0.0.1"); break; case 17: //Flyweight PointService pointService = new PointService(new PointFactory()); var points = pointService.getPoints(); foreach (var p in points) { p.draw(); } break; case 18: //Bridge AdvancedRemoteControl remote = new AdvancedRemoteControl(new SonyTv()); remote.setChannel(1); break; case 19: //Proxy Library lib = new Library(); List <string> bookNames = new List <string>() { "a", "b", "c" }; foreach (var book in bookNames) { lib.eBooks.Add(book, new EBookProxy(book)); } lib.OpenEbook("a"); break; case 20: //Factory Method FactoryMethod.Employee emp = new FactoryMethod.Employee(); BaseEmployeeFactory permanentEmp = new PermanentEmployeeFactory(emp); permanentEmp.ApplySalary(); Console.WriteLine(emp.HouseAllowance); break; case 21: //Abstract Factory AbstractFactory.Employee emp1 = new AbstractFactory.Employee(); emp1.EmployeeTypeID = 1; emp1.JobDescription = "Manager"; EmployeeSystemFactory esf = new EmployeeSystemFactory(); var computerFactory = esf.Create(emp1); Console.WriteLine($"{computerFactory.GetType()}, {computerFactory.Processor()}, {computerFactory.SystemType()}, {computerFactory.Brand()}"); break; case 22: //Builder Builder.IToyBuilder toyBuilder = new Builder.PremiumToyBuilder(); Builder.ToyDirector toyDirector = new Builder.ToyDirector(toyBuilder); toyDirector.CreateFullFledgedToy(); Console.WriteLine(toyDirector.GetToy()); break; case 23: //Fluent Builder //Difference of implementation is visible in Director class. FluentBuilder.IToyBuilder toyBuilder1 = new FluentBuilder.PremiumToyBuilder(); FluentBuilder.ToyDirector toyDirector1 = new FluentBuilder.ToyDirector(toyBuilder1); toyDirector1.CreateFullFledgedToy(); Console.WriteLine(toyDirector1.GetToy()); break; case 24: //Object Pool ObjectPool <OneExpensiveObjToCreate> objPool = new ObjectPool <OneExpensiveObjToCreate>(); OneExpensiveObjToCreate obj = objPool.Get(); objPool.Release(obj); break; } Console.ReadLine(); }
public IEnumerable <Route> Solve(IGraphDatabase graph, City from, City to) { Dictionary <City, (double dist, Route last)> distances = new Dictionary <City, (double dist, Route last)>(); HashSet <City> visitedCitites = new HashSet <City>(); distances[from] = (0, null); City minCity = from; while (minCity != to) { IIterator <Route> outgoing = graph.GetRoutesFrom(minCity); /* * For each outgoing route from city... */ while (outgoing.MoveNext()) { Route route = outgoing.Current(); /* Change to current Route*/ if (visitedCitites.Contains(route.To)) { continue; } double dist = distances[minCity].dist + OptimizingValueFunc(route); if (!distances.ContainsKey(route.To)) { distances[route.To] = (dist, route); } else { if (dist < distances[route.To].dist) { distances[route.To] = (dist, route); } } } visitedCitites.Add(minCity); minCity = null; foreach (var(city, (dist, route)) in distances) { if (visitedCitites.Contains(city)) { continue; } if (minCity == null || dist < distances[city].dist) { minCity = city; } } if (minCity == null) { return(null); } } List <Route> result = new List <Route>(); for (Route route = distances[to].last; route != null; route = distances[route.From].last) { result.Add(route); } result.Reverse(); return(result); }