Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class Animal
- {
- public abstract void MakeSound();
- }
- class Cat : Animal
- {
- public override void MakeSound()
- {
- Console.Write("Mew mew");
- }
- }
- class Dog : Animal
- {
- public override void MakeSound()
- {
- Console.Write("Woof woof");
- }
- }
- static class AnimalFactory
- {
- public static T Create<T>() where T : Animal, new()
- {
- return new T();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Dog d = AnimalFactory.Create<Dog>();
- d.MakeSound();
- }
- }
- class Foo
- {
- static Foo<TKey, TValue> Create<TKey, TValue>(TKey key, TValue value)
- {...}
- }
- class Foo<TKey>
- {
- static Foo<TKey, TValue> Create<TValue>(TValue value)
- {...}
- }
- class Foo<TKey, TValue>
- {
- }
- Foo.Create(x, y);
- Foo<string>.Create(y);
- new Foo<string, int>(x, y);
- public class Factory
- {
- public static T Create<T>() where T : ParentClass, new(String)
- {
- }
- }
- public class Factory
- {
- public static T Create<T>() where T : ParentClass
- {
- T child = new T("hi")
- }
- }
- public class Factory
- {
- public static T Create<T>() where T : ParentClass, new()
- {
- T child;
- child = new T();
- if (child is ChildClass)
- {
- child = new ChildClass("hi");
- }
- return child;
- }
- }
- static class AnimalFactory
- {
- public static Animal Create<T>() where T : Animal
- {
- return Create<T>("blue");
- }
- public static Animal Create<T>(string colour) where T : Animal, new()
- {
- return new T() {Colour = colour};
- }
- }
- Factory<IAwesome> awesomeFactory = new Factory<IAwesome>();
- IAwesome awesomeObject = awesomeFactory.Load(configValue);
- public interface IFactory<T>
- {
- T Create();
- }
- public class DefaultConstructorFactory<T> : IFactory<T> where T : new()
- {
- public T Create()
- {
- return new T();
- }
- }
- public class ActivatorFactory<T> : IFactory<T>
- {
- public T Create()
- {
- return Activator.CreateInstance<T>();
- }
- }
- public class AnimalTamer<TAnimal> where TAnimal : Animal
- {
- IFactory<TAnimal> _animalFactory;
- public AnimalTamer(IFactory<TAnimal> animalFactory)
- {
- if(animalFactory == null)
- {
- throw new ArgumentNullException("animalFactory");
- }
- _animalFactory= animalFactory;
- }
- public void PutOnShow()
- {
- var animal = _animalFactory.Create();
- animal.MakeSound();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var tamer = new AnimalTamer<Tiger>(new DefaultConstructorFactory<Tiger>());
- tamer.PutOnShow();
- }
- }
Add Comment
Please, Sign In to add comment