C ++ problem finishing notes (b) ------- Design Patterns

Summarize the problems encountered, and the interview subject may encounter.
1. What is a design pattern ?

2. What is a singleton ?

3. In singleton mode, what is the lazy mode , what is a hungry man mode , both What is the difference?

4. In the case of singleton mode, regardless of multithreaded, how to ensure that only one instance of a class is created, in addition to this kind, which method can also prohibited generating object ?

5. For example, a single-mode embodiment using C ++?


Answers

1. What is a design pattern ?
Design pattern is a tradition of experience , the former is found, through a summarized form a class of problems of a general solution , instead of being designed qualitative rules, it can be copied as it is not according to the algorithm used.
A total of 23 kinds of classic design patterns, due to the limited level below which describes only a few.

2. What is a singleton?
Singleton, by definition, only one example, is a common software design patterns. The core structure contains only one is called singleton particular class. Guarantee single mode system embodiment, a class of the application class model only one instance. I.e., a class has only one instance of an object .
Features singletons:
1 is only one example.
2. Providing access to a global object pointer (public on the inside), the pointer is shared by all modules.
Example 3. Single mode allows future more instances (scalability).

3. In singleton mode, what is the lazy mode, what is a hungry man mode, both What is the difference?
Lazy mode name kinda funny, by definition lazy, etc. used in this case refers to the time will instantiate the object,
a hungry man mode the contrary, because of hunger, when the singleton class definition to instantiate better.
From the above definition, lazy mode consumes more time, and the spatial mode consumes more starving.
So:
access than larger, or more threads when a long running time, suitable for a hungry man mode,
when a small number of visits, time is running short of time, you can use lazy mode.

4. In the case of singleton mode, regardless of multithreaded, how to ensure that only one instance of a class is created, in addition to this kind, there is a method which can generate prohibited objects?

Singleton by the constructor sets the private members of class ( (or protected member) manner prohibited by the new objects by a general class constructor.

In addition, the abstract class can be instantiated prohibited.
That is added pure virtual function in the class, it is in front of the function to add a virtual, not later defined function, add = 0, the following wording:

class Example
  {
       virtual int function1(int a)=0;
       …
  };

The C ++ code using lazy write mode, respectively, after starving mode and the single locking embodiment mode code?
Singleton constructor except privatization, but also guarantee a global call interface.
Lazy mode as follows:

class Singleton1
{
private:
     Singleton1(){}
     static Singleton1 *psig;
public:
     static Singleton1* GetInstance()
     {
          if(psig==nullptr)
          {
            psig=new Singleton1();
          }
               return psig;
      }
};

Guess you like

Origin blog.csdn.net/alexhu2010q/article/details/81876312