Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Fruit{}
- interface FruitFactory<F extends Fruit>{
- F newFruit(String color,double weight);
- Cocktail mixFruits(F f1,F f2);
- }
- class Apple implements Fruit{}
- class AppleFactory implements FruitFactory<Apple>{
- public Apple newFruit(String color, double weight){
- // create an instance
- }
- public Cocktail mixFruits(Apple f1,Apple f2){
- // implementation
- }
- }
- class Base
- {
- private Base() { }
- public Base(int x) {}
- }
- class Derived : Base
- {
- //public Derived() { } won't compile because Base() is private
- public Derived(int x) :base(x) {}
- public Derived() : base (0) {} // still works because you are giving a value to base
- }
- interface IFoo
- {
- IFoo(int gottaHaveThis);
- static Bar();
- }
- interface ISummable
- {
- operator+(ISummable a, ISummable b);
- }
- class Foo: IFoo
- {
- Foo(int gottaHaveThis) {};
- static Bar() {};
- }
- class SonOfFoo: Foo
- {
- // SonOfFoo(int gottaHaveThis): base(gottaHaveThis); is implicitly defined
- }
- class DaughterOfFoo: Foo
- {
- DaughhterOfFoo (int gottaHaveThis) {};
- }
- ISummable PassedFirstGrade = (ISummable) 10;
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine(Foo.Instance.GetHelloWorld());
- Console.ReadLine();
- }
- }
- public class Foo : FooStaticContract<FooFactory>
- {
- public Foo() // Non-static ctor.
- {
- }
- internal Foo(bool st) // Overloaded, parameter not used.
- {
- }
- public override string GetHelloWorld()
- {
- return "Hello World";
- }
- }
- public class FooFactory : IStaticContractFactory<Foo>
- {
- #region StaticContractFactory<Foo> Members
- public Foo CreateInstance()
- {
- return new Foo(true); // Call static ctor.
- }
- #endregion
- }
- public interface IStaticContractFactory<T>
- {
- T CreateInstance();
- }
- public abstract class StaticContract<T, Factory>
- where Factory : IStaticContractFactory<T>, new()
- where T : class
- {
- private static Factory _factory = new Factory();
- private static T _instance;
- /// <summary>
- /// Gets an instance of this class.
- /// </summary>
- public static T Instance
- {
- get
- {
- // Scary.
- if (Interlocked.CompareExchange(ref _instance, null, null) == null)
- {
- T instance = _factory.CreateInstance();
- Interlocked.CompareExchange(ref _instance, instance, null);
- }
- return _instance;
- }
- }
- }
- public abstract class FooStaticContract<Factory>
- : StaticContract<Foo, Factory>
- where Factory : IStaticContractFactory<Foo>, new()
- {
- public abstract string GetHelloWorld();
- }
Add Comment
Please, Sign In to add comment