Ejemplo n.º 1
0
        public void CompareInstancesSingleton()
        {
            Singleton.Singleton instance1 = Singleton.Singleton.GetInstance();
            Singleton.Singleton instance2 = Singleton.Singleton.GetInstance();

            Assert.AreSame(instance1, instance2);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // Console.WriteLine("Enter 1st number :");


            // int fnum = Convert.ToInt32(Console.ReadLine());

            // Console.WriteLine("Enter 2nd number :");


            // int snum = Convert.ToInt32(Console.ReadLine());

            // int sum = pamal.Add(fnum, snum);

            // Console.WriteLine("Sum is :" + sum);

            // Console.ReadKey();

            Singleton.Singleton fromTeachaer = Singleton.Singleton.GetSingleton;
            fromTeachaer.InstanceMessage("From Tacher");

            Singleton.Singleton fromStudent = Singleton.Singleton.GetSingleton;
            fromStudent.InstanceMessage("From Student");
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Singleton s1 = Singleton.Instance();
            Singleton s2 = Singleton.Instance();

            if(s1 == s2)
            {
                Console.WriteLine("They are the same");
            }
            else
            {
                Console.WriteLine("They are different");
            }

            Console.ReadKey();

            Singleton s3 = new Singleton();
            Singleton s4 = new Singleton();

            if(s3 == s4)
            {
                Console.WriteLine("They are the same");
            }
            else
            {
                Console.WriteLine("They are different");
            }

            Console.ReadKey();
        }
Ejemplo n.º 4
0
 public static Singleton GetInstance()
 {
     if (instance == null)
     {
         instance = new Singleton();
     }
     return instance;
 }
Ejemplo n.º 5
0
 public static Singleton getInstance()
 {
     if (moiMeme == null)
     {
         moiMeme = new Singleton();
     }
     return moiMeme;
 }
Ejemplo n.º 6
0
            /// <summary>
            ///     Responsible for creating and maintaining its own unique instance</summary>
            ///     
            public static Singleton Instance()
            {
                if(_instance == null)
                {
                    _instance = new Singleton();
                }

                return _instance;
            }
Ejemplo n.º 7
0
        public static Singleton Instance()
        {
            if (_instance == null)      // NOT thread-safe
            {
                _instance = new Singleton();
            }

            return _instance;
        }
Ejemplo n.º 8
0
        public static Singleton Instance()
        {
            if (_instance == null) {
                lock (syncRoot) {
                    _instance = new Singleton();
                }
            }

            return _instance;
        }
        public static Singleton Instance()
        {
            // Use 'Lazy initialization'
            if (instance == null)
            {
                instance = new Singleton();
            }

            return instance;
        }
Ejemplo n.º 10
0
 public static Singleton GetSingleton()
 {
     if (_instance == null)
     {
         lock (_lockThis)
         {
             if (_instance == null) _instance = new Singleton();
         }
     }
     return _instance;
 }
Ejemplo n.º 11
0
        public static Singleton GetInstance(string value)
        {
            if (_singleton == null)
            {
                // 现在,假设该程序刚刚启动。
                // 由于还没有Singleton实例,因此多个线程可以同时通过先前的条件并几乎同时到达这一点。
                // 他们中的第一个将获得锁定并将继续进行操作,而其余的将在这里等待。
                lock (Object)
                {
                    // 获取锁的第一个线程达到此条件,进入内部并创建Singleton实例。
                    // 一旦离开锁块,可能一直在等待锁释放的线程可能会进入此部分。
                    // 但是由于Singleton字段已经初始化,因此线程不会创建新对象。
                    if (_singleton == null)
                    {
                        _singleton = new Singleton {
                            Value = value
                        };
                    }
                }
            }

            return(_singleton);
        }
Ejemplo n.º 12
0
 public static Singleton GetInstance()
 {
     _test = new Singleton();
     return(_test);
 }
Ejemplo n.º 13
0
 // Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
 static Singleton()
 {
     _instance = new Singleton();
 }
Ejemplo n.º 14
0
        static void Main(string[] args)
        {
            //原型模式
            //ConcretePrototype p1 = new ConcretePrototype("I");
            //ConcretePrototype p2 = p1.Clone() as ConcretePrototype;
            //Console.WriteLine("Cloned:{0}", p1.Id);
            //Console.Read();

            //Resume a = new Resume("伊布");
            //a.SetPersonallInfo("男", 32);
            //a.SetWorkExperience("2011-2016", "巴黎圣日耳曼");

            //Resume b = (Resume)a.Clone();
            //b.SetWorkExperience("2016-2017", "曼联");

            //Resume c = (Resume)a.Clone();
            ////c.SetPersonallInfo("男",28);
            //c.SetWorkExperience("2017-XX","未知");

            //a.Display();
            //b.Display();
            //c.Display();

            //模板方法模式
            //AbstractClass c;

            //c = new ConcreteClassA();
            //c.TemplateMethod();

            //c = new ConcreteClassB();
            //c.TemplateMethod();

            //外观模式
            //Facade.Facade facade = new Facade.Facade();
            //facade.MethodA();
            //facade.MethodB();

            //建造者模式
            //Director director = new Director();
            //Builder.Builder b1 = new ConcreteBuilder1();
            //director.Construct(b1);
            //Product product = b1.GetResult();
            //product.Show();

            //观察者模式
            //ConcreteSubject s = new ConcreteSubject();

            //s.Attach(new ConcreteObserver(s, "X"));
            //s.Attach(new ConcreteObserver(s, "Y"));
            //s.Attach(new ConcreteObserver(s, "Z"));

            //s.SubjectState = "Ready";
            //s.Notify();

            Boss huhansan = new Boss();

            //看股票的同事
            //StockObserver tongshil = new StockObserver("马蓉", huhansan);
            //看NBA的同事
            //NBAObserver tongshi2 = new NBAObserver("易茶冠", huhansan);

            //huhansan.Update += new Observer.EventHandler(tongshil.CloseStockMarket);
            //huhansan.Update += new Observer.EventHandler(tongshi2.CloseNBADirectSeeding);

            //huhansan.SubjectState = "我胡汉三回来了";

            //huhansan.Notify();

            //抽象工厂
            //Department department = new Department();
            //IFactory factory = new MysqlFactory();
            ////IDepartment idpt = factory.CreateDepartment();
            //IDepartment idpt = DataAccess.CreateDepartment();
            //idpt.Insert(department);

            //状态模式
            //Context c = new Context(new ConcreteStateA());
            //c.Request();
            //c.Request();
            //c.Request();
            //c.Request();

            //适配器模式
            //Target target = new Adapter.Adapter();
            //target.Request();

            //备忘录模式
            //Originator o = new Originator();
            //o.State = "On";
            //o.Show();

            //Caretaker c = new Caretaker();
            //c.Memento = o.CreateMemento();

            //o.State = "Off";
            //o.Show();

            //o.SetMemento(c.Memento);
            //o.Show();

            //组合模式
            //Composite.Composite root = new Composite.Composite("root");
            //root.Add(new Leaf("Leaf A"));
            //root.Add(new Leaf("Leaf B"));

            //Composite.Composite comp = new Composite.Composite("Composite X");
            //comp.Add(new Leaf("Leaf XA"));
            //comp.Add(new Leaf("Leaf XB"));

            //root.Add(comp);

            //Composite.Composite comp2 = new Composite.Composite("Composite XY");
            //comp2.Add(new Leaf("Leaf XYA"));
            //comp2.Add(new Leaf("Leaf XYB"));

            //comp.Add(comp2);

            //root.Add(new Leaf("Leaf C"));
            //Leaf leaf = new Leaf("Leaf D");

            //root.Add(leaf);
            //root.Remove(leaf);

            //root.Display(1);

            //单例模式
            Singleton.Singleton s = Singleton.Singleton.GetInstance();

            //桥接模式
            //Abstraction ab = new RefinedAbstraction();

            //ab.SetImplementor(new ConcreteImplementor());
            //ab.Operation();

            //ab.SetImplementor(new ConcreteImplementorB());
            //ab.Operation();

            //命令模式
            //Receiver r = new Receiver();
            //Command.Command c = new ConcreteCommand(r);
            //Invoker i = new Invoker();
            //i.SetCommand(c);
            //i.ExecuteCommand();

            //责任链模式
            //Handler h1 = new ConcreteHandlerA();
            //Handler h2 = new ConcreteHandlerB();
            //Handler h3 = new ConcreteHandlerC();

            //h1.SetSuccessor(h2);
            //h2.SetSuccessor(h3);

            //int[] requests = { 1, 3, 14, 25, 19, 29, 31 };
            //foreach (int request in requests)
            //{
            //    h1.HandlerRequest(request);
            //}

            //中介模式
            //ConcreteMediator m = new ConcreteMediator();

            //ConcreteColleagueA a = new ConcreteColleagueA(m);
            //ConcreteColleagueB b= new ConcreteColleagueB(m);

            //m.ColleagueA = a;
            //m.ColleagueB = b;

            //a.Send("吃了吗?");
            //b.Send("没呢,你请客?");

            //享元分享
            //int extrinsicstate = 22;

            //FlyweightFactory f = new FlyweightFactory();
            //Flyweight.Flyweight fx = f.GetFlyweight("X");
            //fx.Operation(--extrinsicstate);

            //Flyweight.Flyweight fy = f.GetFlyweight("Y");
            //fy.Operation(--extrinsicstate);

            //Flyweight.Flyweight fz = f.GetFlyweight("Z");
            //fz.Operation(--extrinsicstate);

            //UnsharedConcreteFlyweight uf = new UnsharedConcreteFlyweight();
            //uf.Operation(--extrinsicstate);

            //解释器模式
            //Interpreter.Context context = new Interpreter.Context();
            //IList<AbstractExpression> list = new List<AbstractExpression>();
            //list.Add(new TerminalExpression());
            //list.Add(new NonterminalExpression());
            //list.Add(new TerminalExpression());
            //list.Add(new TerminalExpression());

            //foreach (var exp in list)
            //{
            //    exp.Interpret(context);
            //}

            //访问者模式
            //ObjectStructure o = new ObjectStructure();
            //o.Attach(new ConcreteElementA());
            //o.Attach(new ConcreteElementB());

            //ConcreteVisitorA va = new ConcreteVisitorA();
            //ConcreteVisitorB vb = new ConcreteVisitorB();

            //o.Accept(va);
            //o.Accept(vb);

            //装饰模式
            //ConcreteComponet c = new ConcreteComponet();
            //ConcreteDecoratorA d1 = new ConcreteDecoratorA();
            //ConcreteDecoratorB d2 = new ConcreteDecoratorB();

            //d1.SetComponent(c);
            //d2.SetComponent(d1);
            //d2.Operation();

            //代理模式
            Proxy.Proxy p = new Proxy.Proxy();
            p.Request();

            Console.Read();
        }
Ejemplo n.º 15
0
 static void Main(string[] args)
 {
     Singleton myInstance  = Singleton.Instance();
     Singleton myInstance2 = Singleton.Instance();
 }
Ejemplo n.º 16
0
        static void Main(string[] args)
        {
            Singleton obj = Singleton.Instance;

        }
Ejemplo n.º 17
0
 public static Singleton GetInstance()
 {
     return(_instance ?? (_instance = new Singleton()));
 }