Skip to main content

Posts

The Facade pattern

Facade design pattern is a structural design pattern and it's used widely. The aim of this design pattern is to provide a simple interface to the client for a complex underlying system. So, facade means face of the building. This design pattern hides all complexities of the system and just displays a simple face. Very common example could be URL interface of a browser, which hides all complexities behind and only accepts a URL which user intends to browse. Another common example could be withdrawal / deposit of money from banking system via ATM. To withdraw money we need following steps to achieve successfully: 1. Validate card / account number 2. Validate pin 3. In case of withdrawal check account balance and allow / disallow withdrawal 4. Follow steps 1 & 2 for account deposit (Steps 3 not needed). 5. Finally show the balance. Code Example:  <Code> File: WelcomeBank.h #pragma once #include < fmt/format.h > class WelcomeBank{     public :         // ctor...    

The Visitor pattern

What is visitor pattern? 1.      It encapsulates an operation executed on an object hierarchy in an object. That says, it allows to add   methods to classes of different types without much altering to those classes. 2.        Enables to define new operations without changing the object hierarchy. Use-Case: a. Operations shall be performed on object hierarchy b. The operations change frequently but object hierarchy is stable. Basic UML Class Diagram:         Visitor: ·   Defines the visit operation on the object structure     Tax Visitor: ·   Implements the visit operation for each type of object     Visitable: ·   Defines the accept operation which takes visitor as an argument      Visitable Element(s):                    ·   Implements accept operations        The Visitor pattern has two types of hierarchies . The object hierarchy (Visitable elements) and the operation hierarchy (TaxVisitor). The object hierarchy is pretty stable, but operation hierarchy may

Variadic template class to add numbers recursively during compilation

 The idea of having a class to add numbers (variable parameters) during compilation time recursively. Also wanted to restrict types to a single type while sending parameters to class member function. That said, if we mix int, float and double types to add function shall result in compilation error. How do we achieve this. The below is the code which actually helps to achieve this: <code> #include < fmt/format.h > template < typename T> class MyVarSumClass{     private :         T _sum = 0 ;     public :         template < typename ... TRest>         T add(T num, TRest... nums){             static_assert (std::conjunction<std::is_same<TRest, T>...>{}); /* Assert fails                if types are different */             _sum += num;             return add(nums...); // Next parameter packs gets picked recursively         }         // Base case         T add(T num){             _sum += num;             return _sum;         } }; int main() {     My

Bind lambda with a class and execute while class instance destroys (A final pattern)

 Let's have a template class like below: template< typename T > class finalGuard {     T m_func; public:     finalGuard(T func) : m_func(func) {}     ~finalGuard() { m_func(); } }; What is the requirement? I need to execute lambda when a template instance of the above class goes out of scope or any exception is caused during the execution of code. We can think of this as more or like a final pattern. When a class object's lifetime ends, a piece of code will start executing to wrap up things.  Now I have a factory-like function defined as below to resolve type dependency which lambda is expecting: template< typename T > finalGuard< T > finalCall(T obj) { return obj; }  The below piece of code is used to bind the lambda with the class instance and it gets executed as soon as a class object destructor gets invoked. int main() {     try {         auto guard = finalCall([]() noexcept {             cout << "Good bye via exception!\n";             });

C++ property and detection of property change event

In C++, property means a private field with a getter and a setter method. Unlike C# and other languages, it is part of the language where it is set or accessed just like a field. In C++ never had the notion of the way C# address gets or set of property field(s). However, in MSVC, Clang a property extension has been added to give a notion of get / set of property. How it looks like? Let's have a small code:  #include <iostream> #include <string> using std::cout; using std::string; class Animal {     string sName_; public:     string getName() const { return sName_; }     void setName(const string &asName)     {         sName_ = asName;     } public:     __declspec(property(get = getName, put = setName)) string sName; }; int main() {     Animal aObj;     // Set animal name     aObj.sName = "Cat";     cout << "The animal is: " << aObj.sName << "\n"; } The __declspec extension uses the get and set methods, and these two me

Find time from time ranges (including overlapping ranges)

 This is a very simple problem. In this trying to find a given time from ranges of times. So, I have a collection which stores different time ranges (including overlapping time ranges). The job is to find or search if a time provided by user exists in time ranges collection. Example: Let's assume I have following collections of time ranges: 1. 07:00 - 09:00  2. 13:45 - 15:15 3. 16:25 - 18:10 4. 08:30 - 10:00 Now, need to find if time 09:30 is present in that collection or not. If it finds it shall print true, otherwise false. Similarly it can be use to check other times too. Solution: To store time ranges, what I did is converted start and end time to decimal numbers respectively and stored them in STL's set (multiset) used to address overlapping ranges. As we know that set data structure in STL is a tree (Red Black Tree / Balanced Binary tree).  Time Complexity:  1. set::insert - If N elements are inserted, Nlog(size+N). Implementations may optimize if the range is already sor

Network Shell Utility (netsh, An useful usage)

 In Windows OS, there is a command known as netsh or network shell. It is a command line utility included in Microsoft Windows NT lines of the operating system, beginning with Windows 2000. It allows local/remote network configuration as well as it can be used to display network configuration. Today, we can see how 'netsh' can be leveraged to display wireless passwords. It happens we may forget the wireless password(s) after connecting a device to many wireless networks since it gets stored as a part of the network profile. That said, we enter password for a wireless network for the first time and it gets stored, and then the next time onwards the device gets connected to the wireless network once it is available. We don't need to key in a password again. As a result, we may forget passwords and may get challenged to remember and connect to any other device.  In this post, we will see how can we leverage netsh command to get/retrieve wireless network passwords from a device