design_pattern

basic principle

  • SOLID >> S: single resposibility
    O: open-closed: open for extension, closed for modification
    simple shoud not go back to code/modify somthing haveing writtern adn tested L: liskov substitution
    if interface is ok to type A, it should also is ok to its inherited type B I: interface segregation
    D: dependency inversion
    high level module should not depend on low level; absraction should not depend on detail

factory method pattern

  1. Delegation of object creation to factory method
  2. Extensibility by permitting arbitrary many factory methods
  3. Code reuse through runtime polymorphism
  4. Decouple initialization of factories from creating connections
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
struct Connection {
virtual ~Connection() = default;
virtual send() = 0;
};

struct TcpConnection: public Connection {
send() override {
implementations....
}

};

struct UdpConnection: public Connection {
send() override {
implementations....
}

};

struct ConnectionFactory {
virtual ~ConnectionFactory() = default;
virtual std::unique_ptr<ConnectionFactory> make() = 0;
};

struct TcpConnectionFactory: public ConnectionFactory {
std::unique_ptr<ConnectionFactory> make() override {
implementations...
}
};

struct UdpConnectionFactory: public ConnectionFactory {
std::unique_ptr<ConnectionFactory> make() override {
implementations...
}
};

structural: composite

  • array backed property
    1
    2
    3
    4
    5
    class widget {
    enum abilities {strenth, agi, teli, count};
    std::array<int, count> properties;
    };

NON-Virtual Interface pattern(NVI) -- Template Method Pattern

M&M rule

mutable and mutex(or atomic) should go together

-------------本文结束感谢您的阅读-------------