public void ValueIsLazyWithOverloadConstructorCreatesValueTest() { var lazy = new LazySingleton<Dictionary<string, object>>(() => new Dictionary<string, object>()); var value = lazy.Value; Assert.IsNotNull(lazy.Value, "Value must not be null."); Assert.IsTrue(lazy.IsValueCreated); Assert.AreEqual(typeof(Dictionary<string, object>), lazy.Value.GetType()); }
public staitc LazySingleton GetInstance() { if (_instance == null) { _instance = new LazySingleton(); } return(_instance); }
public void AllTest() { var now = LazySingleton.GetInstance <DateTimeSingleton>().Time; for (var i = 0; i < 10; i++) { Assert.Equal(now.Ticks, LazySingleton.GetInstance <DateTimeSingleton>().Time.Ticks); } }
internal RuleContext(ApplicationContext applicationContext, Action <IRuleContext> completeHandler, LazySingleton <Dictionary <IPropertyInfo, object> > outputPropertyValues, LazySingleton <List <IPropertyInfo> > dirtyProperties, RuleContextModes executeContext) { ApplicationContext = applicationContext ?? throw new ArgumentNullException(nameof(applicationContext)); ApplicationContext = applicationContext; _completeHandler = completeHandler; _outputPropertyValues = outputPropertyValues; _dirtyProperties = dirtyProperties; ExecuteContext = executeContext; }
internal RuleContext(ApplicationContext applicationContext, Action <IRuleContext> completeHandler) { ApplicationContext = applicationContext ?? throw new ArgumentNullException(nameof(applicationContext)); _completeHandler = completeHandler; _outputPropertyValues = (LazySingleton <Dictionary <IPropertyInfo, object> >)ApplicationContext.CreateInstanceDI(typeof(LazySingleton <Dictionary <IPropertyInfo, object> >)); _dirtyProperties = (LazySingleton <List <IPropertyInfo> >)ApplicationContext.CreateInstanceDI(typeof(LazySingleton <List <IPropertyInfo> >)); }
public void ValueIsLazyWithOverloadConstructorCreatesValueTest() { var lazy = new LazySingleton <Dictionary <string, object> >(() => new Dictionary <string, object>()); var value = lazy.Value; Assert.IsNotNull(lazy.Value, "Value must not be null."); Assert.IsTrue(lazy.IsValueCreated); Assert.AreEqual(typeof(Dictionary <string, object>), lazy.Value.GetType()); }
public void Lazy_Singleton_Incrementer_Test() { LazySingleton lazySingletonOne = LazySingleton.Instance; LazySingleton lazySingletonTwo = LazySingleton.Instance; lazySingletonOne.Incrementer += 1; lazySingletonTwo.Incrementer += 1; Assert.Equal(2, lazySingletonOne.Incrementer); Assert.Equal(2, lazySingletonTwo.Incrementer); }
public void TestLazySingleton() { Console.WriteLine("Start of test lazy singleton"); LazySingleton.DoSomething(); LazySingleton lazySingleton1 = LazySingleton.Instance; LazySingleton lazySingleton2 = LazySingleton.Instance; Assert.AreSame(lazySingleton1, lazySingleton2); }
static void Main(string[] args) { var singleton = Singleton.GetInstance(); singleton.TextWrite(); var lazySingleton = LazySingleton.GetInstance(); lazySingleton.TextWrite(); var lazyTemplateSingleton = LazyTemplateSingleton.GetInstance(); lazyTemplateSingleton.TextWrite(); }
public LazySingleton getInstance() { if (_instance == null) { _instance = new LazySingleton(); } else { return(_instance); } return(_instance); }
/// <summary> /// /// </summary> private void Initialize() { Parallel.For(1, 10000, (index, state) => { bag.Add(index * Math.Log(index)); var _singleton = Singleton.AllSingletons; _lazySingleton = LazySingleton <Singleton> .Instance; if (_singleton.GetHashCode() == _lazySingleton.GetHashCode()) { } }); }
public static LazySingleton GetInstance(string value) { if (_instance == null) //checks to see if it has already been initialised { _instance = new LazySingleton // if it hasn't then it creates one and saves it in the private field { LValue = value }; } return(_instance); // returns the private field }
private static void Main(string[] args) { Console.WriteLine("Press any key to exit"); string input = string.Empty; while (string.IsNullOrWhiteSpace(input)) { var test = LazySingleton.GetSingleton(); input = Console.ReadLine(); } }
public static LazySingleton GetInstance() { if (instance == null) { lock (lockObj) { if (instance == null) { instance = new LazySingleton(); } } } return(instance); }
private void Awake() { if (instance != null) { Debug.Log("Destroy " + Tag + " " + gameObject.name); Destroy(gameObject); } else { Debug.Log("Assign this " + Tag + " instance with name " + gameObject.name); instance = this; DontDestroyOnLoad(gameObject); } }
public static void Run() { var lazySingleton = LazySingleton.getInstanceDoubleCheck(); lazySingleton.showMessage(); var hungrySingleton = HungrySingleton.getInstance(); hungrySingleton.showMessage(); var staticInnerClassSingleton = StaticInnerClassSingleton.getInstance(); staticInnerClassSingleton.showMessage(); }
public static LazySingleton GetInstance() { if (_instance == null) { lock (_lockObject) { if (_instance == null) { _instance = new LazySingleton(); } } } return _instance; }
public static LazySingleton GetInstance() { if (_instance == null) { lock (_lockObject) { if (_instance == null) { _instance = new LazySingleton(); } } } return(_instance); }
/// <summary> /// /// </summary> private void Initialize() { Parallel.For(1, 10000, (index, state) => { bag.Add(index * Math.Log(index)); var _singleton= Singleton.AllSingletons; _lazySingleton= LazySingleton<Singleton>.Instance; if (_singleton.GetHashCode() == _lazySingleton.GetHashCode()) { } }); }
private Task <string> TimeConsumingMethod2() { var task = Task.Run(() => { Console.WriteLine("Helo I am TimeConsumingMethod. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId); Thread.Sleep(10000); Console.WriteLine("Helo I am TimeConsumingMethod after Sleep(10000). My Thread ID is :" + Thread.CurrentThread.ManagedThreadId); LazySingleton instance = LazySingleton.Instance; instance.Calculator(); //NormalClass nc = new NormalClass(); //nc.Calculator(); return("Hello I am TimeConsumingMethod" + Thread.CurrentThread.ManagedThreadId); }); return(task); }
public void ZadanieALazySingletonManyTasks() { Task[] tasks = new Task[100]; for (int i = 0; i < tasks.Length; i++) { tasks[i] = new Task(() => { LazySingleton s = LazySingleton.singleton; }); tasks[i].Start(); } tasks.AsParallel().ForAll(task => task.Wait()); Assert.AreEqual(LazySingleton.constructorCount, 1, "Ilość Singletonów różna od 1!"); }
public void MultiThread() { LazySingleton instance1 = null; LazySingleton instance2 = null; Parallel.Invoke( () => { instance1 = LazySingleton.Instance; }, () => { instance2 = LazySingleton.Instance; } ); Assert.Multiple(() => { Assert.AreSame(instance1, instance2); Assert.AreEqual(instance1.GetHashCode(), instance2.GetHashCode()); Assert.AreEqual(1, LazySingleton.Counter, "There is more than 1 instance!"); }); }
/// <summary> /// 简单的Sigleton /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_CommonSingleton_Click(object sender, System.EventArgs e) { LazySingleton s1 = LazySingleton.Instance; LazySingleton s2 = LazySingleton.Instance; string strResult = ""; if (s1.Equals(s2)) { strResult = "[CommonSingleton]the same instance"; } else { strResult = "[CommonSingleton]the defferent instance"; } UpdateResult(strResult); }
static void Main(string[] args) { Console.WriteLine("饿汉式 单例类 的处理!"); for (int i = 0; i < 10; i++) { EagerSingleton data = EagerSingleton.GetInstance(); Console.WriteLine(data.DemoCount++); } Console.WriteLine("懒汉式 单例类 的处理!"); for (int i = 0; i < 10; i++) { LazySingleton data = LazySingleton.GetInstance(); Console.WriteLine(data.DemoCount++); } Console.ReadLine(); }
public static void Main(string[] args) { Console.WriteLine("Creational Patterns deals with object creation!"); Console.WriteLine("<---------------------------- Singleton ---------------------------->"); Parallel.Invoke(() => { SimpleSingleton simpleSingleton = SimpleSingleton.Instance; }, () => { SimpleSingleton simpleSingleton = SimpleSingleton.Instance; }); Parallel.Invoke(() => { ThreadSafeSingleton threadSafeSingleton = ThreadSafeSingleton.Instance; }, () => { ThreadSafeSingleton threadSafeSingleton = ThreadSafeSingleton.Instance; }); Parallel.Invoke(() => { LazySingleton lazySingleton = LazySingleton.Instance; }, () => { LazySingleton lazySingleton = LazySingleton.Instance; }); Console.WriteLine("<---------------------------- Simple Factory ---------------------------->"); SimpleFactory.ICreditCard card = SimpleFactory.CreditCardFactory.GetCreditCard(SimpleFactory.CreditCardType.Platinum); Console.WriteLine($"Credit card Type: {card.GetCardType()}"); Console.WriteLine($"Credit card Limit: {card.GetLimit()}"); Console.WriteLine($"Credit card AnnualCharges: {card.GetAnnualCharges()}"); card = SimpleFactory.CreditCardFactory.GetCreditCard(SimpleFactory.CreditCardType.Titanium); Console.WriteLine($"Credit card Type: {card.GetCardType()}"); Console.WriteLine($"Credit card Limit: {card.GetLimit()}"); Console.WriteLine($"Credit card AnnualCharges: {card.GetAnnualCharges()}"); Console.WriteLine("<---------------------------- Factory Method ---------------------------->"); FactoryMethod.CreditCardFactory creditCardFactory = new PlatinumFactory(); FactoryMethod.ICreditCard creditCard = creditCardFactory.GetCreditCard(); Console.WriteLine($"Credit card Type: {creditCard.GetCardType()}"); Console.WriteLine($"Credit card Limit: {creditCard.GetLimit()}"); Console.WriteLine($"Credit card AnnualCharges: {creditCard.GetAnnualCharges()}"); creditCardFactory = new TitaniumFactory(); creditCard = creditCardFactory.GetCreditCard(); Console.WriteLine($"Credit card Type: {creditCard.GetCardType()}"); Console.WriteLine($"Credit card Limit: {creditCard.GetLimit()}"); Console.WriteLine($"Credit card AnnualCharges: {creditCard.GetAnnualCharges()}"); Console.WriteLine("<---------------------------- Abstract Factory ---------------------------->"); ComputerFactory computerFactory = new ComputerFactory(new ExpensiveComputer()); computerFactory.Assemble(); computerFactory = new ComputerFactory(new CheapComputer()); computerFactory.Assemble(); Console.WriteLine("<---------------------------- Builder ---------------------------->"); Builder.IComputer computer = new Builder.LaptopBuilder(); computer.AddCPU("Intel I3"); computer.AddGPU("AMD Rideon"); computer.AddRAM("8 GB"); computer.AddHDD("SATA 500 GB"); computer.EanbleTouchscreen(true); computer.DisplayDetails(); computer = new Builder.DesktopBuilder(); computer.AddCPU("Intel I5"); computer.AddGPU("NVidea"); computer.AddRAM("16 GB"); computer.AddHDD("SATA 1 TB"); computer.AddMonitor("Full HD"); computer.DisplayDetails(); Console.WriteLine("<---------------------------- Fluent Builder ---------------------------->"); FluentBuilder.IComputer computer1 = new FluentBuilder.LaptopBuilder(); computer1.AddCPU("Intel I3") .AddGPU("AMD Rideon") .AddRAM("8 GB") .AddHDD("SATA 500 GB") .EanbleTouchscreen(true) .DisplayDetails(); computer1 = new FluentBuilder.DesktopBuilder(); computer1.AddCPU("Intel I5") .AddGPU("NVidea") .AddRAM("16 GB") .AddHDD("SATA 1 TB") .AddMonitor("Full HD") .DisplayDetails(); Console.ReadLine(); }
public void RunExample() { var singleton = Singleton.GetInstance(); var threadSafeSingleton = ThreadSafeSingleton.GetInstance(); var lazySingleton = LazySingleton.GetInstance(); }
public static void Main(string[] args) { Shape shape = new Shape(4, 5); Console.WriteLine("{0}, {1}", shape.X, shape.Y); // ConstReadOnlyStatic ConstReadonlyStatic constReadonlyStatic = new ConstReadonlyStatic(); //RefAndOut (swap numbers and testing Out params) RefAndOut refAndOut = new RefAndOut(); int a = 6; int b = 9; int c; refAndOut.SwapNubers(ref a, ref b); refAndOut.OutExampleSumNumbers(a, b, out c); Console.WriteLine("sum of numbers is : {0}", c); // ExtensionMethods Class1 c1 = new Class1(); c1.Display(); c1.NewMethod(); //DisposeVsFinalize DisposeVsFinalize d = new DisposeVsFinalize(); d.Dispose(); d = null; // DelegatesDemo DelegatesDemo dd = new DelegatesDemo(); SumDelegate sd = new SumDelegate(dd.Sum); Console.WriteLine("sum is: {0}", sd.Invoke(30, 40)); sd += dd.Multiply; //multicast delegate Console.WriteLine("Multiply : {0}", sd.Invoke(1, 2)); SumDelegate[] sd1 = { new SumDelegate(dd.Sum), new SumDelegate(dd.Multiply) }; for (int i = 0; i < sd1.Length; i++) { Console.WriteLine("{0}", sd1[i](2, 5)); Console.WriteLine("{0}", sd1[i](8, 5)); Console.WriteLine("{0}", sd1[i](4, 6)); } //EventsDemo EventsDemo ed = new EventsDemo(); event1 += new SampleDel(ed.India); event1 += new SampleDel(ed.USA); event1 += new SampleDel(ed.Netherlands); event1.Invoke(); event1 -= new SampleDel(ed.USA); event1.Invoke(); ed.Action("hello"); ed.xyz += new StrDel(ed.Action); //MicrosoftEventsPattern demo MicrosoftEventPattern mep = new MicrosoftEventPattern(); mep.MyEvent += new MicrosoftEventPattern.MyDelegate(mep.DisplayMsg); mep.RaiseEvent("Hello from the event!!!"); // Patter 2 demo MicrosoftEventPattern2 mep2 = new MicrosoftEventPattern2(); mep2.MyEvent2 += new MicrosoftEventPattern2.MyDelegate2(mep2.DisplayMsg); mep2.RaiseEvent("Hello MEP !!!!", 26); //IEnumerableDemo IEnumerableDemo id = new IEnumerableDemo(); id.Print(); id.ListOperations(); Test t1 = new Test(); t1.Name = "Bhawna"; t1.Surname = "Deonani"; Test t2 = new Test(); t2.Name = "Darwin"; t2.Surname = "Rajpal"; Test myList = new Test(); myList.Add(t1); myList.Add(t2); foreach (Test obj in myList) { Console.WriteLine("Name:- " + obj.Name + " Surname :- " + obj.Surname); } // static Polymorphism StaticPolymorphism sp = new StaticPolymorphism(); sp.Add(1, 2); sp.Add("bhawna", "deonani"); //Dynamic Polymorphism //Method hiding using new keyword Base b2 = new Base(); b2.Foo(); Base base1 = new Child1(); base1.Foo(); Child1 child1 = new Child1(); child1.Foo(); // Method overriding Base b3 = new Child2(); b3.Foo(); Child1 child11 = new Child3(); child11.Foo(); //Abstract methods Base2 b123 = new ChildAbstract(); b123.Test(); b123.Test1(); //Interfaces, you can create interface objects only for instance of class // Meaning that You cannot instantiate an interface // even if the object here is of interface type but the memory is allocated only for the class IInterface1 iiii = new A(); iiii.Print(); A aa = new A(); aa.Sum(2, 3); aa.Display(); B bb = new B(); bb.Sum(33, 44); bb.Print(); // this can only call interface 1 methods IInterface1 ii1 = aa; ii1.Print(); ii1.Sum(5, 6); //This can only call interface 2 methods IInterface2 ii2 = aa; ii2.Display(); if (ii1 is IInterface2) { IInterface2 interface2 = (IInterface2)ii1; interface2.Display(); } IInterface2 int21 = ii1 as IInterface2; int21.Display(); //ConstructorsDemo ClassConst cd = new ClassDerived(); ClassConst cd1 = new ClassDerived("Bhawna"); //Array ArrayClass ar = new ArrayClass(); ar.ReverseArray(); //Ienumerable Test1 t11 = new Test1(); t11.Display(); IEnumerable <int> e1 = t11.GetPowersofTwo(); foreach (int i in e1) { Console.WriteLine(i.ToString()); } //Singleton Demo //only one object is created via using a static property and Private constructor SingletonClass a1 = SingletonClass.Instance; a1.Test(); //Threadsafe singleton demo ThreadSafeSingleton tss = ThreadSafeSingleton.Instance; tss.Test(); ThreadSafeSingleton1 tss1 = ThreadSafeSingleton1.Instance; tss1.Test(); //Thread Safe Singleton without using locks and no lazy instantiation ThreadSafeSingletonNoLocksNoLazy tnl = ThreadSafeSingletonNoLocksNoLazy.Instance; tnl.Test(); LazySingleton ls = LazySingleton.Instance; ls.Test(); //using .NET 4's Lazy<T> type LazyClassSingleton lcs = LazyClassSingleton.Instance; lcs.Test(); //Object Pooling in .Net Factory f = new Factory(); Student s1 = f.GetStudentObject(); s1.FirstName = "BHawna"; s1.LastName = "deonani"; Console.WriteLine("First object: {0}, {1}: ", s1.FirstName, s1.LastName); Student s2 = f.GetStudentObject(); s2.FirstName = "Darwin"; s2.LastName = "Rajpal"; Console.WriteLine("Second Object: {0}, {1}: ", s2.FirstName, s2.LastName); Student s3 = f.GetStudentObject(); if (s3.FirstName == null && s3.LastName == null) { s3.FirstName = "Nyra"; s3.LastName = "Darwin Rajpal"; } Console.WriteLine("Third Object: {0}, {1}: ", s3.FirstName, s3.LastName); Student s4 = f.GetStudentObject(); if (s4.FirstName == null && s4.LastName == null) { s4.FirstName = "N"; s4.LastName = "DR"; } Console.WriteLine("Fourth Object: {0}, {1}: ", s4.FirstName, s4.LastName); Student s5 = f.GetStudentObject(); if (s5.FirstName == null && s5.LastName == null) { s5.FirstName = "Hello"; s5.LastName = "World"; } Console.WriteLine("Fifth Object: {0}, {1}: ", s5.FirstName, s5.LastName); // ACtionFuncPredicate Demo ActionFuncPredicate afp = new ActionFuncPredicate(); Action <int> action = new Action <int>(afp.ActionMethod); action.Invoke(100); Func <int, int, int> func = new Func <int, int, int>(afp.FuncMethodSum); func.Invoke(2, 3); Predicate <int> predicate = new Predicate <int>(afp.IsPositive); predicate.Invoke(3); predicate.Invoke(-234); //Generics Demo (provides typesafety, performance, and code reuse) GenericsDemo gd = new GenericsDemo(); int a12 = 3, b12 = 4; string str1 = "bhawna", str2 = "deonani"; gd.Swap(ref a12, ref b12); gd.Swap(ref str1, ref str2); gd.ReverseUsingStack(); //MultithreadingDemo Console.WriteLine("Main method"); MultithreadingDemo md1 = new MultithreadingDemo(); int a222 = 10; Thread t = new Thread(new ParameterizedThreadStart(md1.Method)); Thread t21 = new Thread(new ParameterizedThreadStart(md1.Method)); t.Start(a222); t21.Start(a222); Thread[] tArr = new Thread[5]; for (int i = 0; i < tArr.Length; i++) { tArr[i] = new Thread(new ThreadStart(md1.Calculate)); Console.WriteLine("Working from Thread: {0}", i); tArr[i].Name = i.ToString(); } foreach (Thread ttt in tArr) { ttt.Start(); } for (int i = 0; i < 4; i++) { Thread tmutex = new Thread(new ThreadStart(MultithreadingDemo.MutexDemo)); tmutex.Name = string.Format("Thread {0} :", i + 1); tmutex.Start(); } //Semaphore demo for (int i = 0; i < 5; i++) { Thread abc = new Thread(new ParameterizedThreadStart(MultithreadingDemo.SemphoreDemo)); abc.Start(i); } Console.ReadLine(); }
public void LazySingeltonConstructorWithOverloadConstructorCreatesObject() { var lazy = new LazySingleton<Dictionary<string, object>>(() => new Dictionary<string, object>()); Assert.IsNotNull(lazy, "new LazySingelton can not be null."); }
public void IsValueCreatedWithOverloadConstructorIsFalseTest() { var lazy = new LazySingleton<Dictionary<string, object>>(() => new Dictionary<string, object>()); Assert.IsFalse(lazy.IsValueCreated, "IsValueCreated must be false by default."); }
public void ZadanieALazySingletonOneTask() { LazySingleton s = LazySingleton.singleton; Assert.AreEqual(LazySingleton.constructorCount, 1, "Ilość Singletonów różna od 1!"); }
public void LazySingeltonDefaultConstructorCreatesObject() { var lazy = new LazySingleton<Dictionary<string, object>>(); Assert.IsNotNull(lazy, "new LazySingelton can not be null."); }
internal RuleContext(Action <RuleContext> completeHandler, LazySingleton <Dictionary <IPropertyInfo, object> > outputPropertyValues, LazySingleton <List <IPropertyInfo> > dirtyProperties, RuleContextModes executeContext) { _completeHandler = completeHandler; _outputPropertyValues = outputPropertyValues; _dirtyProperties = dirtyProperties; ExecuteContext = executeContext; }
internal RuleContext(Action <RuleContext> completeHandler) { _completeHandler = completeHandler; _outputPropertyValues = new LazySingleton <Dictionary <IPropertyInfo, object> >(); _dirtyProperties = new LazySingleton <List <IPropertyInfo> >(); }
public void LazySingeltonDefaultConstructorCreatesObject() { var lazy = new LazySingleton <Dictionary <string, object> >(); Assert.IsNotNull(lazy, "new LazySingelton can not be null."); }
public void LazySingeltonConstructorWithOverloadConstructorCreatesObject() { var lazy = new LazySingleton <Dictionary <string, object> >(() => new Dictionary <string, object>()); Assert.IsNotNull(lazy, "new LazySingelton can not be null."); }
public void IsValueCreatedWithOverloadConstructorIsFalseTest() { var lazy = new LazySingleton <Dictionary <string, object> >(() => new Dictionary <string, object>()); Assert.IsFalse(lazy.IsValueCreated, "IsValueCreated must be false by default."); }