Exemple #1
0
        static void Main(string[] args)
        {
            Trainer kristina = new Trainer();

            Subscriber1 nikola = new Subscriber1("Nikola");
            Subscriber1 marija = new Subscriber1("Marija");
            Subscriber2 jana   = new Subscriber2("Jana");
            Subscriber2 ivan   = new Subscriber2("Ivan");
            Subscriber3 gligor = new Subscriber3("Gligor");
            Subscriber3 pero   = new Subscriber3("Pero");

            kristina.EventHandler += nikola.Facebook;
            kristina.EventHandler += marija.Facebook;
            kristina.EventHandler += jana.Mail;
            kristina.EventHandler += ivan.Mail;
            kristina.EventHandler += gligor.SMS;
            kristina.EventHandler += pero.SMS;

            kristina.SendMessage("'Test message!'");
            Console.WriteLine("Waiting for the custom message ...");
            Console.WriteLine();
            kristina.ComposeMessage(" Pance Manaskov", 4, "Decki vo Sreda Workshop!");


            Console.ReadLine();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            shape       shape = new shape();
            Subscriber1 sub1  = new Subscriber1(shape);
            Subscriber2 sub2  = new Subscriber2(shape);

            shape.Draw();

            Console.Read();
        }
Exemple #3
0
    static void Main()
    {
        Publisher   m  = new Publisher();   //create an object of publisher class which will later be passed on subscriber classes
        Subscriber1 l  = new Subscriber1(); //create object of 1st subscriber class
        Subscriber2 l2 = new Subscriber2(); //create object of 2nd subscriber class

        l.Subscribe(m);                     //we pass object of publisher class to access delegate of publisher class
        l2.Subscribe2(m);                   //we pass object of publisher class to access delegate of publisher class
        m.Start();                          //starting point of publisher class
    }
            static void MyMain3(string[] args)
            {
                Shape       shape = new Shape();
                Subscriber1 sub   = new Subscriber1(shape);
                Subscriber2 sub2  = new Subscriber2(shape);

                shape.Draw();

                // Keep the console window open in debug mode.
                System.Console.WriteLine("Press any key to exit.");
                //System.Console.ReadKey();
            }
Exemple #5
0
        WeakReference MemoryCoreAlloc(Action <Subscriber2, EventOwner> subscribe, Action <Subscriber2> check, Action <EventOwner> raise)
        {
            EventOwner  e = new EventOwner();
            Subscriber2 s = new Subscriber2();

            subscribe(s, e);
            Collect();
            raise(e);
            check(s);
            s.Clear();
            return(new WeakReference(s));
        }
Exemple #6
0
        static void Main(string[] args)
        {
            Publisher   pub  = new Publisher();
            Subscriber1 sub1 = new Subscriber1();
            Subscriber2 sub2 = new Subscriber2();
            Subscriber3 sub3 = new Subscriber3();

            pub.MyEvent += new EventHandler(sub1.OnEvent);
            pub.MyEvent += new EventHandler(sub2.OnEvent);
            pub.MyEvent += new EventHandler(sub3.OnEvent);
            pub.DoSomething();
            Console.ReadKey();
        }
Exemple #7
0
    static void Main(string[] args)
    {
        var sub1a = new Subscriber1();
        var sub1b = new Subscriber1();
        var sub2  = new Subscriber2();

        var publisher = new Publisher();

        publisher.MessageReceived += sub1b.HandleMessage;
        publisher.MessageReceived += sub2.HandleMessage;
        publisher.MessageReceived -= sub1b.HandleMessage;

        publisher.ReceiveMessage();
    }
Exemple #8
0
    public void Run()
    {
        var publisher = new Publisher();

        var subscr1 = new Subscriber1();
        var subscr2 = new Subscriber2();
        var subscr3 = new Subscriber3();

        publisher.receiveNotif += subscr1.SubscribeTheAction;
        publisher.receiveNotif += subscr2.SubscribeTheAction;
        publisher.receiveNotif += subscr3.SubscribeTheAction;

        publisher.CreateNews();
    }
        void MemoryCore(Action <Subscriber2, EventOwner> subscribe, Action <Subscriber2> check, Action <EventOwner> raise)
        {
            var e = new EventOwner();
            var s = new Subscriber2();

            subscribe(s, e);
            Collect();
            raise(e);
            check(s);
            s.Clear();
            WeakReference sRef = new WeakReference(s);

            s = null;
            Collect();
            Assert.AreEqual(false, sRef.IsAlive);
        }
        static void Main(string[] args)
        {
            Timer.Timer timer = new Timer.Timer();

            Subscriber1 subscriber1 = new Subscriber1();

            subscriber1.StartMail(timer);

            Subscriber2 subscriber2 = new Subscriber2();

            subscriber2.StartMail(timer);

            timer.StartTimer(5000);

            Console.ReadLine();
        }
Exemple #11
0
        /// <summary>
        /// Здесь мы используем паттерн Наблюдатель
        /// </summary>
        static void Main()
        {
            Subject subject = new Subject {
                Value = 10
            };

            var sub1 = new Subscriber1(subject);
            var sub2 = new Subscriber2(subject);

            subject.AddSubsriber(sub1);
            subject.AddSubsriber(sub2);

            subject.NotifySubsribers();
            subject.Value += 5;
            subject.NotifySubsribers();
            subject.RemoveSubsriber(sub2);
            subject.Value += 5;
            subject.NotifySubsribers();
        }
Exemple #12
0
        static void Main(string[] args)
        {
            #region MANY EXERCISES

            /* 1) Create an abstract class Animal and Dog, Cat and Bird classes inheriting it
             *         Animal has
             * Name
             * Age(custom getter and setter) - Can't be set below 0 or over 175
             * Color
             * Print( abstract method ) - Prints all properties of the Animal
             * Dog has
             * Race
             * Bark(method ) - Prints BARK BARK in console
             * Cat has
             * IsLazy
             * Meow(method ) - Prints MEOW in console
             * Bird has
             * IsWild
             * FlySouth(method ) - Print's in the console that is flying south if IsWild is true or print's that it's a domesticated bird if its false
             * Create Lists of:
             * 3 dogs
             * 3 cats
             * 3 birds
             * Use LINQ to:
             * Get all dogs of a particular race
             * Get the last lazy cat
             * Get the all wild birds that are younger than 3 and are ordered by their name
             * 2) Create extension methods:
             * GetFirstLetter - Method on String that returns the first letter of a string
             * LastLetter - Method on String that returns the last letter of a string
             * IsEven - Method on Int that checks if its even and returns bool
             * GetNfromList - Generic method that accepts an int and returns the first N(input ) items from that list
             * 3) Create generic methods:
             * PrintList - Method that prints any list of items in the console(strings, bools ints etc. )
             * PrintAnimals - Method that prints any list with Animals(print method) in the console(Dog, Cat Bird and any Animal )
             * 4) Create a delegate that accepts two strings and returns bool
             * Create a method called StringMagic that requires the delegate as parameter and that executes the delegate and prints the 2 strings and the result
             * Call the StringMagic method to compare 2 strings length
             * Call the StringMagic method to compare if the 2 strings start on the same character
             * Call the StringMagic method to compare if the 2 strings end on the same character
             * 5) Create a publisher class called Trainer that:
             * Has delegate that returns void and accepts one string parameter
             * Has method SendMessage - Accepts a message and sends it to all subscribers in the event
             * Has method ComposeMessage - Accepts 3 parameters, trainerName, groupNumber, message.This method will Thread.Sleep(3000) and then call a method SendMessage with a string that says: {trainerName
             * }
             * informs G { groupNumber }: {message
             * }
             * Create 3 Subscriber classes with one method in each that implements the delegate from the publisher
             * First subscriber will write that it got the message through SMS
             * Second subscriber will write that it got the message through E-Mail
             * Third subscriber will write that it got the message through Facebook
             * Create instances of the publisher and 3 subscribers and make the publisher send a message to all 3 of them*/

            #endregion MANY EXERCISES

            List <Dog> dogs = new List <Dog>()
            {
                new Dog("race1", "sarko", 3, "red"),
                new Dog("race2", "sarko1", 4, "blue"),
                new Dog("race3", "sarko2", 5, "pink"),
                new Dog("race4", "sarko3", 6, "black"),
            };

            List <Cat> cats = new List <Cat>()
            {
                new Cat("maca1", 2, "white", true),
                new Cat("maca2", 5, "red", false),
                new Cat("maca3", 3, "blue", true),
                new Cat("maca4", 7, "yellow", false),
            };
            List <Bird> birds = new List <Bird>()
            {
                new Bird("bird1", 3, "white", false),
                new Bird("bird2", 3, "red", true),
                new Bird("bird3", 1, "brown", false),
                new Bird("bird4", 2, "blue", true),
            };
            //var particularRace = dogs
            //                .Where(x => x.Race == "race1")
            //                .ToList();

            //foreach (var item in particularRace)
            //{
            //    item.Print();
            //}

            //var lastlazyCat = cats
            //                 .Where(x => x.IsLazy == true)
            //                 .LastOrDefault();

            //Console.WriteLine(lastlazyCat.Name);
            //List<Bird> allBirds = birds
            //                  .Where(x => x.IsWild == true && x.Age < 3)
            //                  .OrderBy(x => x.Name)
            //                  .ToList();

            //foreach (var item in allBirds)
            //{
            //    item.Print();
            //}
            //string s1 = "natasha";
            //Console.WriteLine(s1.GetFirstLetter());
            //string s2 = "Natasha is developer";
            //Console.WriteLine(s2.GetLastLetter());
            //int integer = 32;
            //Console.WriteLine(integer.isEven());

            //var c = dogs.FirstInt(1);
            //foreach (var item in c)
            //{
            //    Console.WriteLine(item.Name);
            //}


            //List<int> integers = new List<int>() { 1, 2, 3, 4, 5 };
            //List<string> strings = new List<string>() { "Natasha", "Goran", "Danilo","Mateo" };
            //GenericMethods g = new GenericMethods();
            //g.Print(integers);
            //g.Print(strings);
            //g.PrintAllAnimals(dogs);
            //g.PrintAllAnimals(birds);
            //g.PrintAllAnimals(cats);

            //string str1 = "Natasha Andova";
            //string str2 = "Natasha Andonova";


            //StringMagic(str1, str2, (x, y) =>
            //{
            //    if (x.Length > y.Length)
            //    {
            //        return true;
            //    }
            //    return false;
            //});
            //StringMagic(str1, str2, (x, y) =>
            //{
            //    if (x.ToCharArray().First() == y.ToCharArray().First())
            //    {
            //        return true;
            //    }
            //    return false;
            //});
            //StringMagic(str1, str2, (x, y) =>
            //{
            //    if (x.ToCharArray().Last() == y.ToCharArray().Last())
            //    {
            //        return true;
            //    }
            //    return false;
            //});
            Publisher   p1   = new Publisher();
            Subscriber1 sub1 = new Subscriber1();
            Subscriber2 sub2 = new Subscriber2();
            Subscriber3 sub3 = new Subscriber3();



            p1.EventHandler += sub1.Subscriber1Mess;
            p1.EventHandler += sub2.Subscriber2Mess;
            p1.EventHandler += sub3.Subscriber3Mess;
            p1.ComposeMessage("Risto Panchevski", 1, "Let's talk about C# Adv");



            Console.ReadLine();
        }
Exemple #13
0
        static void Main(string[] args)
        {
            List <Dog> dogs = new List <Dog> {
                new Dog("retriever", "Sarko", 7, "Brown"),
                new Dog("haskey", "Mailo", 2, "White"),
                new Dog("retriever", "Leo", 3, "Black")
            };
            List <Cat> cats = new List <Cat> {
                new Cat(true, "Garfield", 3, "Brown"),
                new Cat(false, "Tom", 2, "Black"),
                new Cat(false, "Kitty", 3, "white")
            };
            List <Bird> birds = new List <Bird> {
                new Bird(false, "Cavka", 1, "Black"),
                new Bird(false, "Kokoska", 1, "Brown"),
                new Bird(true, "Lastovica", 2, "Black")
            };
            var retriever = dogs.Where(x => x.Race == "retriever");

            foreach (Dog retrieve in retriever)
            {
                Console.WriteLine(retrieve.Name);
            }

            var lastLazyCat = cats.Last(x => x.isLazy == true);

            Console.WriteLine(lastLazyCat.Name);

            var wildBirdsYoungerThan3 = birds.Where(x => x.isWild == true && x.Age < 3).OrderBy(x => x.Name).ToList();

            foreach (Bird bird in wildBirdsYoungerThan3)
            {
                Console.WriteLine($"Bird younger than 3 {bird.Name}");
            }
            var name = "Goran";

            Console.WriteLine(name.FirstLetter());
            Console.WriteLine(name.LastLetter());

            var c = dogs.FirstInt(9);

            foreach (var item in c)
            {
                Console.WriteLine(item.Name);
            }
            List <int> int322 = new List <int> {
                3, 6, 2, 8, 2, 15, 16, 18
            };
            List <string> strings = new List <string> {
                "Krste", "Dragan", "Petko"
            };

            MyGenericClass generici = new MyGenericClass();

            generici.Print(int322);
            generici.Print(strings);
            generici.PrintAnimals(dogs);

            string str1 = "Gorana";
            string str2 = "blabla";

            StringMagic(str1, str2, (x, y) =>
            {
                if (x.Length > y.Length)
                {
                    return(true);
                }
                return(false);
            });
            StringMagic(str1, str2, (x, y) =>
            {
                if (x.ToCharArray().First() == y.ToCharArray().First())
                {
                    return(true);
                }
                return(false);
            });
            StringMagic(str1, str2, (x, y) =>
            {
                if (x.ToCharArray().Last() == y.ToCharArray().Last())
                {
                    return(true);
                }
                return(false);
            });


            Publisher   p  = new Publisher();
            Subscriber1 s1 = new Subscriber1();
            Subscriber2 s2 = new Subscriber2();
            Subscriber3 s3 = new Subscriber3();

            p.EventHandler += s1.Subscribe1Process;
            p.EventHandler += s2.Subscribe2Process;
            p.EventHandler += s3.Subscribe3Process;

            p.ComposeMessage("Dragan", 1, "DECKI UCETE");
            Console.ReadLine();
        }