zoukankan      html  css  js  c++  java
  • Singleton (单件模式)

    意图: 保证一个类仅有一个实例,并提供一个访问它的全局访问点。

    使用性: 

  • 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。
  • 当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。

    代码:

    namespace Singleton_DesignPattern
    {
        using System;

    class Singleton
    {
    private static Singleton _instance;

    public static Singleton Instance()
    {
    if (_instance == null)
    _instance = new Singleton();
    return _instance;
    }
    protected Singleton(){}

    // Just to prove only a single instance exists
    private int x = 0;
    public void SetX(int newVal) {x = newVal;}
    public int GetX(){return x;}
    }

            public class Client
        {
            public static int Main(string[] args)
            {
                 int val;
    // can't call new, because constructor is protected
    Singleton FirstSingleton = Singleton.Instance();
    Singleton SecondSingleton = Singleton.Instance();

    // Now we have two variables, but both should refer to the same object
    // Let's prove this, by setting a value using one variable, and
    // (hopefully!) retrieving the same value using the second variable
    FirstSingleton.SetX(4);
    Console.WriteLine("Using first variable for singleton, set x to 4");

    val = SecondSingleton.GetX();
    Console.WriteLine("Using second variable for singleton, value retrieved = {0}", val);
                 return 0;
            }
        }
    }

查看全文
  • 相关阅读:
    Selenium简单测试页面加载速度的性能(Page loading performance)
    Selenium Page object Pattern usage
    Selenium如何支持测试Windows application
    UI Automation的两个成熟的框架(QTP 和Selenium)
    分享自己针对Automation做的两个成熟的框架(QTP 和Selenium)
    敏捷开发中的测试金字塔(转)
    Selenium 的基础框架类
    selenium2 run in Jenkins GUI testing not visible or browser not open but run in background浏览器后台运行不可见
    eclipse与SVN 结合(删除SVN中已经上传的问题)
    配置Jenkins的slave节点的详细步骤适合windows等其他平台
  • 原文地址:https://www.cnblogs.com/sophie/p/231387.html
  • Copyright © 2011-2022 走看看