Skip to content

Commit

Permalink
SOLID:OCP intro
Browse files Browse the repository at this point in the history
  • Loading branch information
codex31373 committed Dec 29, 2024
1 parent a0acd51 commit 728078c
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 7 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ project(udemy_1_initial)
#warrning levels defines send to GCC
#add_definitions("-Wall" "-g")


set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_BUILD_TYPE Debug)

include_directories(${PROJECT_SOURCE_DIR})
file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/*.cpp ${PROJECT_SOURCE_DIR}/*.h)
add_executable(${PROJECT_NAME} ${SRC_FILES})

target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)



Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,34 @@
}
]
}



mmcli -m 0 --command=AT+CFUN=1


mmcli -m 0 -d
mmcli -m 0 --set-power-state-on // Set full power state in the modem
mmcli -m 0 -e

And then restart your device

APN: internet.a1.bg


nmcli connection add type gsm ifname ttyUSB3 con-name 5g
nmcli connection modify id "5g" gsm.apn "internet.a1.bg"

nmcli connection up id "5g"

nmcli connection show 5g

mmcli -m 3 --command=AT+CGDATA="PPP",internet.a1.bg


sudo rm /etc/NetworkManager/system-connections/5g
sudo systemctl restart NetworkManager

mmcli -m 0 --command=AT+CGDCONT?

```
96 changes: 91 additions & 5 deletions design_patterns/solid.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,99 @@
#include <vector>

// ---- single responsibility principle (SRP)
class book{}; // this class is responsible for storing information about a book
class magazine{}; // this class is responsible for storing information about a magazine
class printer{
void print(const book &b){};
void print(const magazine &m){};
class Book{}; // this class is responsible for storing information about a book
class Magazine{}; // this class is responsible for storing information about a magazine
class Printer{
void print(const Book &b){};
void print(const Magazine &m){};
}; // this class is responsible for printing
// every class should have only one responsibility

// ---- open-closed principle (OCP)
enum class Size{ SMALL, MEDIUM, LARGE };
enum class Color{ RED, GREEN, BLUE };
class Product{
public:
Size size;
Color color;
};

template <typename T> class AndSpecification;
template <typename T> class Specification{
public:
virtual bool is_satisfied(const T &item) const = 0;

AndSpecification<T> operator&&(Specification<T> &&right)
{
return AndSpecification<T>(*this, right);
}
};
template <typename T> class Filter{
public:
virtual std::vector<T*> filter(std::vector<T*> items, const Specification<T> &spec) = 0;
};

class BetterFilter : public Filter<Product>{
public:
std::vector<Product*> filter(std::vector<Product*> items, const Specification<Product> &spec) override
{
std::vector<Product*> result;
for(const auto &item : items){
if(spec.is_satisfied(*item)){
result.push_back(item);
}
}
return result;
}
};


class ColorSpecification : public Specification<Product>{
public:
Color mColor;

explicit ColorSpecification(const Color &color) : mColor{color}{}

virtual bool is_satisfied(const Product &item) const override{
return item.color == mColor;
}
};

class SizeSpecification : public Specification<Product>{
public:
Size mSize;

explicit SizeSpecification(const Size &size) : mSize{size}{}

virtual bool is_satisfied(const Product &item) const override{
return item.size == mSize;
}
};

template <typename T> class AndSpecification : public Specification<T>{
public:
explicit AndSpecification( Specification<T> &left, Specification<T> &right) : mLeft{left}, mRight{right}{}
virtual bool is_satisfied(const T &item) const override{
return mLeft.is_satisfied(item) && mRight.is_satisfied(item);
}
private:
Specification<T>& mLeft;
Specification<T>& mRight;
};

//usage
ColorSpecification red(Color::RED);
SizeSpecification large(Size::LARGE);

auto comboSpecs = ColorSpecification(Color::RED) && SizeSpecification(Size::LARGE);

std::vector<Product*> products;
BetterFilter bf;
std::vector<Product*> red_products = bf.filter(products, red);
std::vector<Product*> large_products = bf.filter(products, large);
std::vector<Product*> red_large_products = bf.filter(products, AndSpecification<Product>(red, large));
std::vector<Product*> red_k = bf.filter(products, comboSpecs);

// ---- Liskov substitution principle (LSP)
// ---- interface segregation principle (ISP)
// ---- dependency inversion principle (DIP)

0 comments on commit 728078c

Please sign in to comment.