Tuesday, September 3, 2024

Abstract Factory Pattern


What

Abstract Factory Pattern is another layer of abstraction over Factory pattern. It is used to create families of related objects without specifying their concrete classes. 

It provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created.

Who

It has four key actors as below

  1. Abstract Factory is an interface for creating a family of related objects, but it does not specify the concrete classes of the objects to be created.
  2. Concrete Factory is a class that implements the Abstract Factory interface to create a family of related objects.
  3. Abstract Product is an interface for a type of product. 
  4. Concrete Product is a class that implements the Abstract Product interface to create a specific type of product.

How

End user promotes a clear separation between the creation of product families with cars and their specifications. It has the following key steps:

  • Create an interface for the Abstract Factory with a method to create a family of related objects.
  • Create an interface for the Abstract Product with a method to describe the product.
  • Create a Concrete Factory class that implements the Abstract Factory interface to create a family of related objects.
  • Create a Concrete Product class that implements the Abstract Product interface to describe the product.
  • Create a client class that uses the Abstract Factory and Abstract Product interfaces to create and describe a family of related objects.

When

Classic real-life example of Abstract Factory pattern is GUI toolkit like button, checkbox, menu across different operating systems like Windows, Linux Mac.

Where

Though Factory and Abstract Factory look similar, it has significant differences

Factory pattern has three key elements 

  1. create objects without mentioning exact class 
  2. single product type focus with less complexity
  3. lower-level abstraction

Abstract factory pattern has the following key elements

  1. create family of related objects without specification 
  2. multiple product type focus with more complexity
  3. higher level abstraction

Reference

Code repository https://github.com/gsenthilvel/DesignCreational/tree/main/abstractfactory

Sunday, September 1, 2024

Factory Pattern



Factory pattern is one of the most used creational design pattern in real world applications, like DB, Logging frameworks. It is used to create objects based on a common interface.

Without Factory

End user is responsible for creating the object by directly instantiating the class based on the input type during its construction.  It has limitation of tightly coupling, limited scalability and single responsibility violation.

<pre><code>

if (carModel == 1)

{

_evCar = new Tesla();

}

</code></pre>

With Factory

Two key actors in Factory design (1) underlying product (2) creator factory. Both actors are driven by interface with multiple derived classes.

It is a design pattern that provides an interface for creating objects in a superclass,  but allows subclasses to alter the type of objects that will be created.

<pre><code>

ICarFactory carFactory = new CarFactory();

_evCar = carFactory.GetCar(carModel);

</code></pre>

Benefits

The Factory pattern is useful when you need to create an object based on a specific input or condition. It has few design advantages like: decoupling of creation logic with client usage, code reusability, scalability with extensibility and single responsibility principle.

Reference

Code repository: https://github.com/gsenthilvel/DesignCreational/tree/main/factory 


Saturday, August 24, 2024

Singleton Pattern


One of the easiest models, used in Creational design pattern

What?

It is a design pattern that restricts the instantiation of a class to one object. 

This is useful when exactly one object is needed to coordinate actions across the system.

Why?

I can think of a few scenarios where the Singleton pattern is useful.

On usage of a shared resource, like a database connection, or a logger, Singleton pattern makes sure that it has one instance of that resource.

When?

When you need to have a single instance of a class, and you want to provide a global point of access to the object.

It can be instantiated in two ways, either lazy or eager.

1. Eager instantiation is when the object is created when the class is loaded.

2. Lazy instantiation is when the object is created only when it is needed.

<pre><code>

private static readonly Lazy<Singleton> _instance = 

            new Lazy<Singleton>(() => new Singleton());

</code></pre>

How?

Key components of Singleton pattern are:

1. Static member: 

Static member ensures that memory is allocated only once, preserving the single instance of the Singleton class.

<pre><code>

private static Singleton instance;

</code></pre>

2. Private constructor:

Private constructor ensures that the class cannot be instantiated from outside the class.

<pre><code>

private Singleton() {}

</code></pre>

3. Public static method:

Public static method provides a global point of access to the Singleton object.

<pre><code>

public static Singleton Instance

{

get

{

return _instance.Value;

}

}

</code></pre>

Where?

Code repository: DesignCreational/singleton at main · gsenthilvel/DesignCreational (github.com)


Friday, August 16, 2024

sarvam.ai


Sarvam-2B: India's first open-source small language model trained on 10 India languages (Hindi, Tamil, Telugu, Malayalam, Punjabi, Odia, Gujarati, Marathi, Kannada, and Bengali) & English

Founded by a team of passionate experts, they aim to lead transformative research in AI that will make development, deployment, and distribution of GenAI apps in India significantly robust, performant, and cost-effective.

With Sarvam's generative AI building blocks, enterprises can unlock new market opportunities, establish direct and deeper connect with their customers.

Demo website: https://www.sarvam.ai/

HF checkpoint sarvam-2b-v0.5: https://huggingface.co/sarvamai/sarvam-2b-v0.5


Friday, August 2, 2024

String Compare performance


In .NET code, "ToUpper" and "ToLower" methods can impact performance due to memory allocation, string copying, and potential garbage collection, especially in situations involving large strings or frequent conversions.

Performance-wise, string.Equals is faster than the above methods due to direct character comparison, avoiding memory allocation, and reducing overhead for case-insensitive string comparison.

Best approach is to leverage SringComparion.OrdinalIgnoreCase to compare strings using ordinal(binary) sort rules and ignoring the case of the strings being compared.

Saturday, July 20, 2024

System outage 7/19


What?

Yday, Microsoft experienced a global outage due to an issue with CrowdStrike's Falcon Sensor software, causing widespread disruptions and triggering the 'Blue Screen of Death' on Windows PCs.

Computers with Mac and Linux operating systems were not impacted, and CrowdStrike said the incident was not caused by a cyberattack.

Who?

CEO George Kurtz, said the system was sent an update, and that update had a software bug in it and caused an issue with the Microsoft operating system

Cybersecurity programs like CrowdStrike’s frequently and automatically update themselves to account for new tactics that malicious hackers have discovered. And there’s always a slight risk that any software update will be incompatible with other programs.

Why?

Logical system flow is depicted in the given diagram.

Where?

In computing, memory is organized as a large array of numbers, often represented in hexadecimal (base 16) for simplicity.

The patch attempted to read memory address 0x9c (156 in decimal), which is an invalid memory region. Any program that tries to read from this region is immediately terminated by Windows, as shown in the stack dump image.  Programmatically, it was caused by a NULL pointer reference in the code.

The affected code was part of a system driver, which has privileged access to the system hardware. When such a driver crashes, the operating system must immediately crash to protect the system, causing the infamous Blue Screen of Death (BSOD).

How?

CrowdStrike immediately published workaround steps for individual hosts and cloud environment at their portal https://www.crowdstrike.com/blog/statement-on-falcon-content-update-for-windows-hosts/

5 years ago, importance of null pointer handler was inked at https://medium.com/trimble-maps-engineering-blog/nullable-business-value-91c31df8f20d 




Wednesday, July 17, 2024

Godmother of AI


Dr. Fei-Fei Li, the renowned computer scientist known as the "godmother of AI", who has created a startup World Labs. In just four months, its already valued at more than $1 billion as per Financial Times

As Princeton University alumni, shared her vision for artificial intelligence (AI) 25 years ago. "This is a profound technology that will change human civilization" Li said from the same stage Albert Einstein discussed his theory of relativity in 1921. 

Li headed AI at Google Cloud from 2017 to 2018 and currently advises the White House task force on AI.

In a Ted Talk in April, Li further explained the field of research her startup will work on advancing, which involves algorithms capable of realistically extrapolating images and text into three-dimensional environments and acting on those predictions, using a concept known as "spatial intelligence" 

This could bolster work in various fields such as robotics, augmented reality, virtual reality, and computer vision. If these capabilities continue to advance in the ambitious ways Li plans, it has the potential to transform industries like healthcare and manufacturing.

The investment in World Labs reflects a trend where venture capitalists eagerly align themselves with ambitious AI companies, spurred by the surprise success of OpenAI’s ChatGPT, which rapidly achieved a valuation exceeding $80 billion.