标签: singleton

在类库中正确实现缓存以在asp.net应用程序中使用

我正在我在asp.net应用程序中使用的类库中实现缓存.

我使用静态方法创建了我的缓存对象作为单例模式来更新缓存,这实际上只是加载一个成员变量/属性,其中包含我需要缓存的数据集合(得到一些锁定逻辑的课程).我认为这是一个很好的方式,因为我可以通过调用访问我的数据

MyCacheObject.Instance.MyDataCollection
Run Code Online (Sandbox Code Playgroud)

我正在创建一个新的缓存对象来存储由某个键分区的大量数据.我所说的是我正在创建一个新的缓存,但是这个不会一次加载所有数据,而是为每个访问的密钥存储一个集合.

MyOtherCacheObject.Instance.MyOtherDataCollection(indexkey)
Run Code Online (Sandbox Code Playgroud)

这次提出了关于垃圾收集的问题.由于我存储了大量的数据,如果突然得到gc,它不会是浪费吗?由于它只是一个单例模式,因此无法确保数据保留在缓存中.

所以我的问题是 - 实现缓存来处理这种情况的最佳做法是什么?我真的不喜欢这个非常复杂的解决方案,我知道System.Web中有缓存,但这似乎有点"关闭",因为这只是一个类库,或者你怎么看?

c# asp.net singleton caching garbage-collection

8
推荐指数
1
解决办法
7756
查看次数

静态方法或Singletons性能方面(Android)?

在具有少量POJO和许多辅助方法的应用程序中运行它们,有什么更好的性能:使辅助类单例或使方法静态?

java performance singleton android static-methods

8
推荐指数
2
解决办法
8227
查看次数

Cocoa - Singleton对象:在哪里初始化成员变量?

我想知道初始化单身人士课程成员的最佳位置在哪里.

我正在使用Apple基本指南单例实现.你能指出一下这些线路是怎么发生的吗?代码如下:

static MyGizmoClass *sharedGizmoManager = nil;

+ (MyGizmoClass*)sharedManager
{
    @synchronized(self) {
        if (sharedGizmoManager == nil) {
            [[self alloc] init]; // assignment not done here
        }
    }
    return sharedGizmoManager;
}

+ (id)allocWithZone:(NSZone *)zone
{
    @synchronized(self) {
        if (sharedGizmoManager == nil) {
            sharedGizmoManager = [super allocWithZone:zone];
            return sharedGizmoManager;  // assignment and return on first allocation
        }
    }
    return nil; //on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain
{
    return self;
}

- (unsigned)retainCount …
Run Code Online (Sandbox Code Playgroud)

iphone cocoa singleton initialization

8
推荐指数
1
解决办法
5603
查看次数

单例通过C++中的静态实例 - 进入源文件或进入头文件?

干杯,

我在"Programming Game AI by Example"中遇到了这段代码:

/* ------------------ MyClass.h -------------------- */
#ifndef MY_SINGLETON
#define MY_SINGLETON

class MyClass
{
private:

  // member data
  int m_iNum;

  //constructor is private
  MyClass(){}

  //copy ctor and assignment should be private
  MyClass(const MyClass &);
  MyClass& operator=(const MyClass &);

public:

  //strictly speaking, the destructor of a singleton should be private but some
  //compilers have problems with this so I've left them as public in all the
  //examples in this book
  ~MyClass();

  //methods
  int GetVal()const{return m_iNum;}
  static MyClass* …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-construction singleton

8
推荐指数
1
解决办法
5748
查看次数

GenericFactory as Singleton

我读了Jim Hyslop和Herb Sutter 的文章抽象工厂,模板风格.该工厂实现为Singleton.它们提供了一种使用RegisterInFactory助手类自动注册类的简便方法.

现在我已经多次阅读过Singletons应该避免的内容,有些人甚至将它们视为反模式,并且只有少数情况下它们是有用的.这是其中之一吗?或者是否有另一种方法可以提供自动批准类的简单方法?

c++ generics singleton factory

8
推荐指数
1
解决办法
1086
查看次数

带有继承的Java单例

我有一组单例类,我想避免样板代码.这就是我现在拥有的:

public class Mammal {
    protected Mammal() {}
}

public class Cat extends Mammal {
    static protected Cat instance = null;
    static public Cat getInstance() {
        if (null == instance) {
            instance = new Cat();
        }
        return instance;
    }

    private Cat() {
        // something cat-specific
    }
}
Run Code Online (Sandbox Code Playgroud)

这有效,并没有任何问题,除了我有许多Mammal必须复制getInstance()方法的子类.如果可能的话,我更喜欢这样的东西:

public class Mammal {
    protected Mammal() {}

    static protected Mammal instance = null;

    static public Mammal getInstance() {
        if (null == instance) {
            instance = new Mammal();
        } …
Run Code Online (Sandbox Code Playgroud)

java inheritance singleton code-formatting

8
推荐指数
1
解决办法
3662
查看次数

使用dispatch_once为类层次结构创建单例类

我有一个继承自'MyClass'的2个子类,每个子类应该是一个单例.

当我没有继承任何其他类时,我已经使用此模式获取静态实例:

+ (MyClass *)getInstance
{
    static dispatch_once_t once;
    static MyClass *instance;

    dispatch_once(&once, ^{
        instance = [[MyClass alloc] init];
    });

    return instance;
}
Run Code Online (Sandbox Code Playgroud)

这很有用.现在,如果我添加两个新的子类,FirstClass和SecondClass,它们都继承自MyClass,我如何确保返回相应的ChildClass?

dispatch_once(&once, ^{
    // No longer referencing 'MyClass' and instead the correct instance type
    instance = [[[self class] alloc] init];
});

FirstClass *firstClass = [FirstClass getInstance]; // should be of FirstClass type
SecondClass *secondClass = [SecondClass getInstance]; // should be of SecondClass type
Run Code Online (Sandbox Code Playgroud)

执行上述操作意味着我总是回到我实例化的第一类作为我的第二类类型:

first: <FirstClass: 0x884b720>
second: <FirstClass: 0x884b720>
// Note that the address and type …
Run Code Online (Sandbox Code Playgroud)

singleton multithreading objective-c class-hierarchy

8
推荐指数
1
解决办法
1464
查看次数

Singleton类中的实例变量由多个线程访问

我有一个单身人士课程:

public class School {
    private HashMap<String, String> students;

    private static School school;

    private School(){
        students = new HashMap<String, String>();   
    }

    public static School getInstance(){
       if(school == null){
           school = new School();
       }
       return school;
    }

    //Method to add student
    protected void addStudent(String id, String name){
          students.put(id,name);
    }
    //Method to remove student
    protected void removeStudent(String id){
          students.remove(id);
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您在上面所看到的,在单例类中,我有一个students变量(a HashMap),有一些方法可以在类中添加和删除学生.

在我的应用程序,有可能是使用这个多线程School类getInstance(),然后添加和删除学生.为了使访问(特别是对 students 实例的访问)是线程安全的,我想在方法中使用synchorized关键字getInstanc(),如: …

java performance singleton multithreading

8
推荐指数
1
解决办法
1万
查看次数

Python,使用单例模式或仅全局变量

在python中,使用单例模式而不是使用全局变量更好吗?

class Singleton(type):
    def __call__(self, *args, **kwargs):
        if 'instance' not in self.__dict__:
            self.instance = super(Singleton, self).__call__(*args, **kwargs)
        return self.instance
Run Code Online (Sandbox Code Playgroud)

或只是创建一个全局变量:

SINGLETON_VARIABLE = None
def getSingleton():
    if SINGLETON is None:
        SINGLETON_VARIABLE = SOME_ININ_CLASS()
    return SINGLETON_VARIABLE
Run Code Online (Sandbox Code Playgroud)

制作单例模式是否需要使生活复杂化?先感谢您。

python singleton global-variables

8
推荐指数
1
解决办法
1338
查看次数

我是否使用Singleton与Realm数据库正确?

我有一个应用程序,用户可以从DialogFragment创建/删除/编辑列表.在我的DialogFragments中有这样的方法之前:MainActivtity.adapter.add(String name, String location, double price);

adapter是我的recyclerView的适配器对象.适配器的类具有我对recyclelerView中的项的创建/删除/编辑方法.如上所示,这就像我所理解的那样,也是一种叫做mehtods的可怕方式.

所以我选择将所有这些CRUD方法放在一个单例类中,并像这样调用这些方法: Service.getInstance().add(...);

这是一种正确的方法,我可以做得更好吗?

这就是我如何创建现在包含我的CRUD方法的单例类,而不是像以前一样将它们放在我的适配器类中.

public class Service {

private static Realm realm;
private static Service service = new Service();

private Service() {
    realm = Realm.getInstance(App.getAppContex());
}

public static Service getInstance(){

    if(service == null){
        service = new Service();
    }
    return service;
}



   public void add(String name, String location, double price) {


    ShopListItem shopListItem = new ShopListItem();

    shopListItem.setName(name);
    shopListItem.setLocation(location);
    shopListItem.setPrice(price);
    shopListItem.setTimestamp(System.currentTimeMillis());
    shopListItem.setIsBought(0);

    realm.beginTransaction();
    realm.copyToRealm(shopListItem);
    realm.commitTransaction();
}


public void removeItem(int position, List<ShopListItem> shopListItems) { …
Run Code Online (Sandbox Code Playgroud)

java singleton android design-patterns realm

8
推荐指数
1
解决办法
2917
查看次数