Patterns Flashcards

1
Q

What is the objective of a Singleton Patter?

A

Make sure that a unique instance of an Class is created along all the exceution of the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you declare a Singleton Class?

A
class SngA
{
  SngA(){}
public:
static SngA get()
{
static SngA* my_self{nullptr};
if(!my_self) 
  my_self = new SngA();
return my_self;
}
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the porpouse of a Factory Pattern?

A

Offers an interface for creating a instance of an class with its subclasess deciding which class to instantiate.

Usually, the factory patter requieres a parameter to know what kind of class needs to instantiate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you implement a Factory Patter?

A
class CardGame{
public:
static CardGame createACardGame(GameType type){
  if(type == 1)
	  return new GateA();
	else if(type == 2)
	  return GameB;
		
  return null;
}
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the Strategy Pattern?

A

Strategy is a behavioral design pattern that lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.

You need an interface of Strategy to be implemented by several instances (Concrete Strategy) and a context wich execute the concrete strategy.

https://refactoring.guru/design-patterns/strategy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you implement a Strategy Patter in C++?

A
class Strategy
{
public:
    virtual double execute(int x) = 0;
};

class StrategyPow : public Strategy
{
public:
    double execute(int x){ return pow(x,2);}
};

class StrategyDiv : public Strategy
{
public:
    double execute(int x){ return x/2;}
};

class Context
{
    Strategy* pStrg{nullptr};
public:
    void setStrategy(Strategy* strg){ pStrg = strg;}
    double doStrg(int x){return pStrg? pStrg->execute(x) : 0;}
};

int main()
{   
    Context ctx{};
    StrategyPow sp{};
    StrategyDiv sd{};

    ctx.setStrategy(&sp);
    cout << "StrategyPow: " << ctx.doStrg(6) << "\n";
    ctx.setStrategy(&sd);
    cout << "StrategyDiv: " << ctx.doStrg(6) << "\n";

    return 0;
}

https://refactoring.guru/design-patterns/strategy/cpp/example

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does Oberver Patter wok?

A

It is used to notify to subscribers for some events from the object that subscribers are looking forward to.

Java events work,like button evetns, with that pattern.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How an Observer Patterns is implemented?

A
class IObserver
{
public:
    virtual ~IObserver() = default;
    virtual void notifyEvent(string ev)=0;
};
class Observer: public IObserver
{
public:
    void notifyEvent(string ev)
    {
        cout << "Event Notified: " << ev << "\n";
    }
};

class Publisher
{
    list<IObserver*> subscribers{};
public:
    void addUSbscriber(IObserver* sub)
    {
        subscribers.push_back(sub);
    }

    void notify(string e)
    {
        for(auto s : subscribers)
            s->notifyEvent(e);
    }
};

https://refactoring.guru/design-patterns/observer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the Proxy Patter?

A

Proxy is a structural design pattern that lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.

https://refactoring.guru/design-patterns/proxy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How the Proxy Patter is implemented?

A
class RealService
{
    public:
    string doOperation(int x){return "Real Result";}
};

class IProxySrv
{
public:
    virtual string operation(int x);
};

class ProxySev : public IProxySrv
{
    RealService rserv{};
public:
    bool isAvailable(){return true;}
    string operation(int x)
    {
        return rserv.doOperation(x);
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which should be the four basic steps to design a class?

A
  1. Elimininate Ambiguity
  2. Define Core Objects
  3. Analize Relation Ships
  4. Investigate Actions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does Resolve Ambiguity mean?

A

Using the basic quiestions What, Who, Why, How, When and Where,; clarify all your quiestions and asumptions.

DON’T ASUME

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does Define Core Object mean?

A

Identiying all the main actors/entity that are considered as Core Objects, which are required to designe the Object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does Analize Relationships means?

A

Once you have all the Core Obects and another required objects, it’s important to define how they are interacting each other.

A Entity class could be useful at this moment,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does Investigate Actions mean?

A

In nutshells. is finding out all the actions (verbs) that define an interaction between the Objects; so, in this way, we should find all the actions that the Objects requiere.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly