Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

Напиши пример паттерна посетитель на C#

  1. User

    Напиши пример паттерна посетитель на C#

  2. Assistant

    Пример паттерна посетитель на C#:

    using System;
    using System.Collections.Generic;
    
    namespace VisitorPatternExample
    {
        public interface IAnimal
        {
            void Accept(IAnimalOperation operation);
        }
    
        public class Monkey : IAnimal
        {
            public void Accept(IAnimalOperation operation)
            {
                operation.VisitMonkey(this);
            }
        }
    
        public class Lion : IAnimal
        {
            public void Accept(IAnimalOperation operation)
            {
                operation.VisitLion(this);
            }
        }
    
        public class Dolphin : IAnimal
        {
            public void Accept(IAnimalOperation operation)
            {
                operation.VisitDolphin(this);
            }
        }
    
        public interface IAnimalOperation
        {
            void VisitMonkey(Monkey monkey);
            void VisitLion(Lion lion);
            void VisitDolphin(Dolphin dolphin);
        }
    
        public class SpeakOperation : IAnimalOperation
        {
            public void VisitMonkey(Monkey monkey)
            {
                Console.WriteLine("Oh oo aa aa!");
            }
    
            public void VisitLion(Lion lion)
            {
                Console.WriteLine("Roaaaar!");
            }
    
            public void VisitDolphin(Dolphin dolphin)
            {
                Console.WriteLine("Tuut tuttu!");
            }
        }
    
        public class RunOperation : IAnimalOperation
        {
            public void VisitMonkey(Monkey monkey)
            {
                Console.WriteLine("Monkey runs fast!");
            }
    
            public void VisitLion(Lion lion)
            {
                Console.WriteLine("Lion runs with power!");
            }
    
            public void VisitDolphin(Dolphin dolphin)
            {
                Console.WriteLine("Oops! Dolphin can't run!");
            }
        }
    
        class Program
        {
            static void Main()
            {
                Monkey monkey = new Monkey();
                Lion lion = new Lion();
                Dolphin dolphin = new Dolphin();
    
                SpeakOperation speak = new SpeakOperation();
                RunOperation run = new RunOperation();
    
                monkey.Accept(speak);
                lion.Accept(speak);
                dolphin.Accept(speak);
    
                Console.WriteLine();
    
                monkey.Accept(run);
                lion.Accept(run);
                dolphin.Accept(run);
            }
        }
    }
    

    Особенность паттерна посетитель в разделении интерфейса, связанного с поведением (IAnimalOperation) и самого объекта, поведение которого требуется изменить (IAnimal).

    В данном примере, существует три животных - обезьяна, лев и дельфин - и две операции: говорить и бегать. Все животные могут говорить, но только обезьяна и лев могут бегать. Внешний объект (IAnimalOperation) знает, как обращаться с каждым из животных.

  3. User

    напиши пример паттерна стратегия на C#

  4. Assistant

    Пример паттерна стратегия на C#:

    using System;
    
    namespace StrategyPatternExample
    {
        public interface ITransportationStrategy
        {
            void GoToDestination();
        }
    
        public class CarStrategy : ITransportationStrategy
        {
            public void GoToDestination()
            {
                Console.WriteLine("Driving by car to the destination");
            }
        }
    
        public class BicycleStrategy : ITransportationStrategy
        {
            public void GoToDestination()
            {
                Console.WriteLine("Riding a bicycle to the destination");
            }
        }
    
        public class WalkingStrategy : ITransportationStrategy
        {
            public void GoToDestination()
            {
                Console.WriteLine("Walking to the destination");
            }
        }
    
        public class Traveller
        {
            private ITransportationStrategy _transportationStrategy;
    
            public Traveller(ITransportationStrategy strategy)
            {
                _transportationStrategy = strategy;
            }
    
            public void SetStrategy(ITransportationStrategy strategy)
            {
                _transportationStrategy = strategy;
            }
    
            public void GoToDestination()
            {
                _transportationStrategy.GoToDestination();
            }
        }
    
        class Program
        {
            static void Main()
            {
                var car = new CarStrategy();
                var bicycle = new BicycleStrategy();
                var walking = new WalkingStrategy();
    
                Traveller travellerJane = new Traveller(car);
                travellerJane.GoToDestination();
    
                travellerJane.SetStrategy(bicycle);
                travellerJane.GoToDestination();
    
                Traveller travellerJohn = new Traveller(walking);
                travellerJohn.GoToDestination();
            }
        }
    }
    

    В данном примере, контекст класса, Traveller, использует стратегию транспортировки, чтобы переместиться до пункта назначения. Поведение перемещения зависит от выбранной стратегии. В реализации стратегий (CarStrategy, BicycleStrategy и WalkingStrategy) используется интерфейс ITransportationStrategy, который определяет общий метод GoToDestination. Мы можем легко изменить способ перемещения, используя метод SetStrategy класса Traveller.

  5. User

    Приведи другой пример который не связан с передвижением

  6. Assistant

    Пример паттерна стратегия на C#, связанный с различными способами сортировки числовых массивов:

    using System;
    using System.Linq;
    
    namespace StrategyPatternExample
    {
        public interface ISortingStrategy
        {
            void Sort(int[] numbers);
        }
    
        public class BubbleSortStrategy : ISortingStrategy
        {
            public void Sort(int[] numbers)
            {
                for (int i = 0; i < numbers.Length - 1; i++)
                {
                    for (int j = 0; j < numbers.Length - 1 - i; j++)
                    {
                        if (numbers[j] > numbers[j + 1])
                        {
                            int temp = numbers[j];
                            numbers[j] = numbers[j + 1];
                            numbers[j + 1] = temp;
                        }
                    }
                }
    
                Console.WriteLine("The array has been sorted using Bubble Sort");
            }
        }
    
        public class QuickSortStrategy : ISortingStrategy
        {
            public void Sort(int[] numbers)
            {
                QuickSort(numbers, 0, numbers.Length - 1);
                Console.WriteLine("The array has been sorted using Quick Sort");
            }
    
            private void QuickSort(int[] numbers, int left, int right)
            {
                if (left < right)
                {
                    int pivotIndex = Partition(numbers, left, right);
                    QuickSort(numbers, left, pivotIndex - 1);
                    QuickSort(numbers, pivotIndex + 1, right);
                }
            }
    
            private int Partition(int[] numbers, int left, int right)
            {
                int pivot = numbers[right];
                int i = left - 1;
    
                for (int j = left; j <= right - 1; j++)
                {
                    if (numbers[j] <= pivot)
                    {
                        i++;
                        Swap(ref numbers[i], ref numbers[j]);
                    }
                }
    
                Swap(ref numbers[i + 1], ref numbers[right]);
                return i + 1;
            }
    
            private void Swap(ref int a, ref int b)
            {
                int temp = a;
                a = b;
                b = temp;
            }
        }
    
        public class ArrayProcessor
        {
            private ISortingStrategy _sortingStrategy;
    
            public ArrayProcessor(ISortingStrategy strategy)
            {
                _sortingStrategy = strategy;
            }
    
            public void SetStrategy(ISortingStrategy strategy)
            {
                _sortingStrategy = strategy;
            }
    
            public void SortArray(int[] numbers)
            {
                _sortingStrategy.Sort(numbers);
            }
        }
    
        class Program
        {
            static void Main()
            {
                int[] numbers = new[] { 8, 2, 7, 4, 1, 5, 3, 6 };
                Console.WriteLine("Original Array: " + string.Join(", ", numbers));
    
                ArrayProcessor processor = new ArrayProcessor(new BubbleSortStrategy());
                processor.SortArray(numbers);
                Console.WriteLine("Sorted Array: " + string.Join(", ", numbers));
    
                // Changing array back to unsorted state
                numbers = new[] { 8, 2, 7, 4, 1, 5, 3, 6 };
                Console.WriteLine("Original Array: " + string.Join(", ", numbers));
    
                processor.SetStrategy(new QuickSortStrategy());
                processor.SortArray(numbers);
                Console.WriteLine("Sorted Array: " + string.Join(", ", numbers));
            }
        }
    }
    

    В данном примере, контекст класса, ArrayProcessor, использует стратегию сортировки, чтобы отсортировать числовой массив. Различные стратегии сортировки реализуются через интерфейс ISortingStrategy (BubbleSortStrategy и QuickSortStrategy). Метод SetStrategy класса ArrayProcessor позволяет легко изменить способ сортировки массива.