使用基类的单例模式

问题描述:



我想在数据库中保存数据,因为我想通过基类使用Singleton模式来实现,任何机构都可以知道,请帮帮我

Hi,

I want to save data in database in that i want to implement using Singleton pattern with base class can any body know please help me

一个简单的方法是singleton对象是一个只能在内存中拥有一个实例的实例.

看下面的例子

In a simple way singleton object is the one which can have only one instance of it in the memory.

Look at the example below

 public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    private static Person person;
    private Person()
    {
    }
    public static Person GetPerson()
    {
        if (person == null)
        {
            person = new Person();
        }
        return person;
    }
}



由于私有构造函数,因此无法像 Person person=new Person()这样的常规方式实例化此Person类.但是您需要像Person person=Person.GetPerson()一样访问它,它总是返回相同的实例,直到内存中的静态Person对象为止.



这是最简单的形式.现在,您要如何使用这种模式?



This Person class you can''t instantiate in the normal way like Person person=new Person() because of the private constructor. But you need to access it like Person person=Person.GetPerson() which always return the same instance till the static Person object in the memory.



It is the simplest form. Now how you want to use this pattern?.


定义一个类时,这意味着您可以在运行时创建大量的对象.这是因为public 构造函数.
When you define a class, it means you can create a large number of its object at runtime. It is because of public constructor.
class Box {
public Box()
{
}
...
...
...
}
Box b1 = new Box();
Box b2 = new Box();
..
// so on
Box b100 = new Box();


但是如果您应用单例模式.这意味着您只能使用它创建一个对象.在此模式中,构造器为private,这意味着您不能像
那样使用


but if you apply singleton pattern. it means you can create ONLY ONE object with it. In this pattern constructor is private it means you can''t use like

Box b1 = new Box();
Box b2 = new Box(); // invalid ..


因此,单例模式是使用私有构造函数定义一个类,以使该类之外的代码无法创建它".
以下代码创建一个单例类.


So singleton pattern is "Defining a Class with private constructor so that the code which is outside the class cannot create it".
following code creates a singleton class.

class Box{	
    private static Box instance ;	
    private Box()	
      {		...	
      }	
 public static Box getInstance()	
      {		
         if (instance == null)
 	     instance = new Singleton();
         return instance;	
      }		
 public void doSomething()	
     {		...		
     }
}


通过上述定义,您可以通过
访问Box类的对象


With the above defination you can access the object of class Box by

Box b = Box.getInstance();
b.doSomething();



但重要的是要了解,我们在哪里使用单例,而在不使用单例.
如果我们想要通向通用功能集的单个网关,则可以使用单例类.例如日志记录,应用程序配置,服务器客户端架构,资源访问,全局/共享值/变量(公司电子邮件,地址等).通用数据存储访问点.如果使用得当,它可以节省大量内存并提高性能.

如何确定类的单例行为.

以最简单的形式,

如果您的应用程序中的一个实体使用不同的属性值共存,那么它不是单例的候选对象.例如在学校系统学生":Student-1中,Student-2具有相同的属性/行为,但属性值不同. (他们的姓名,年龄,地址类别不同).

如果System中存在一个通用的实体,并且其属性值相同,则它是Singleton类的候选对象.例如游乐场,礼堂,学校告示板.这些是整个School系统中的单实例对象.


需要进行深入分析以获取我们可以创建单例的类的列表.

请参考 http://www.oodesign.com/singleton-pattern.html [



But it is important to understand that where do we use singleton and we we not.
If we want a single gateway to common set of functionalities we use singleton class. like Logging, Configuration of Application, Server Client achitecture, Resource access, Global/shared values/variables ( company email, address etc.). common Data store acess point. If wisely used it saves lots of memory and increase performance.

How to determine the singleton behaviour of a Class.

In the simplest form,

If a entity in your application co-exists with different property values then it is not a candidate for singleton. for e.g. In school System , "Student" : Student-1 , Student-2 has a common property / behaviour but property values are different. (their name,age,address class are different).

If a Entity exists in System that is common and its property values are same,its a candidate for Singleton class. for e.g. Playground, Auditorium, School Notice Board. these are single instance object throughout the School system.


A in-depth analysis is required to get a list of classes that we can make singleton.

please refer http://www.oodesign.com/singleton-pattern.html[^] for more detail