Discussion on Singleton Mode for Polymorphism

zhaozj2021-02-16  47

Intimate: I don't like this solution very much, but some people ask, and other similar solutions are found, so they are sorted out, for everyone to refer to discussion. Discussion on polymorphism - SmileMac

Although the Singleton mode is simple, but it is discussed the most model, why? Not a concept problem, but an algorithm problem. Specifically, the SINGLETON object life and the management algorithm problem of the domain will have different needs in different scenarios, and the structure of structural design is that the portions that may have changed from each other, so that they can independently The shaft changes, reducing each other's coupling, so it is meaningful to isolate algorithms that may have arguments in a specific application environment.

How to implement the reusability of the Singleton mode, that is, if there are several classes to implement Singleton, how can we avoid each class must have a separate Singleton algorithm copy? Whether all classes share a copy, this, this, If you need to change the algorithm, you can only change one place. Further, can I reuse this algorithm in different projects? In C , Master Andrei AlexandRescu has given a very good solution, but in a template Language, how to use some basic technologies, such as classes and running polymorphisms, to achieve reusable Singleton mode? This article tried to give a solution shown below. [This code Write with a dip java: Because the author is very unfamiliar for Java, I have to be pseudo-Java, please reader Han Han. Thank User Henry_zhou to make problems, so I have the opportunity to think. This solution may not be correct, so please point out To discuss the correct answer together. If the discussion is discussed, please indicate the source. Since the author does not understand the generic support of Java, it does not rule out that there is a better solution in Java. This article is only assumed to provide language support only the most basic features of object-oriented] // package1; class Singleton {protected Singleton _instance; public Singleton (Singleton _inst) {_instance = _inst;} public virtual Singleton getInstance () {if (_instance.get () == null ) _INSTANCE.CREATE (); return _INSTANCE.GET ();} protected virtual singleton get () {// error handler code;} protected virtueal void code () {// error handler code;}} class SingletonThreadSafe: public Singleton {public SingletonThreadSafe (Singleton _inst); public virtual synchronized Singleton getInstance () {if (_instance.get () == null) _instance.create (); return _instance. get ();}} // package2 class MyBaseClass: public Singleton {protected MySubClassXXX (); ......} class Instance_base: public Singleton {private static MyBaseClass _instance; public Instance_base () {}; protected virtual MyBaseClass get ( ) {Return _INSTANCE;

} Protected virtual void create () {if (_instance == null) _instance = new MyBaseClass;}} // package3; class MySubClassXXX: public MySubClass {protected MySubClassXXX (); ......} class Instance_XXX: public Instance_base { private static MySubClassXXX _instance; public Instance_base () {}; protected virtual MySubClassXXX get () {return _instance;} protected virtual void create () {if (_instance == null) _instance = new MySubClassXXX;}} // client eg ,; Singleton Singgen = New Singletonthreadsafe (new instance_xxx ()); mysubclassxxx myobj = down_cast (Singgen.GetInstance ()); this code takes advantage of a feature of the so-called class-Oriented language: the same class objects can access each other's internal members That is, the access control is at the class level, not the object level. In Java, access control is relaxed to the same package. In this way, we can design these functions that may make Singleton crash to protected as protected For each subs class, it needs to implement its Instance class, which is actually I Nstance Holder. In most Singleton's implementation code, Instance Holder is incorporated with the user class. Here, if the subscriber class can create a secure object at low cost, the so-called security is meant to create Impact Singleton instance objects, and so-called low costs refer to the cost of initialization can be accepted. If these conditions are met, then instance holder can be incorporated into the user class, create a dummy with a specific constructor to replace NEW. Instance_xxx (). However, Singleton and SingletonthreadsaDSAFE can be reused, and this inherited tree can expand themselves. Provide Singleton under different scors, such as Thread Scope Singleton (concurrently used in use Used in servers), Process Scope Singleton, DCE Scope Singleton, etc., or Singleton, such as specific machines, efficiency, etc.

转载请注明原文地址:https://www.9cbs.com/read-25236.html

New Post(0)