Software System Design

I do not know how the perfect software architectures are build. It is possible that perfect software designs are myth. …

October 23, 2021 · 5 min · 921 words

MVC & MVVM Android Design Pattern (Android)

Recently, I developed an Android application. The application was developed focusing on simplicity. The application was small thus skipped writing unit tests. I know skipping the unit tests is not a good practice. In past, I was working on backend projects where tons of unit tests exists for API’s. I am new to application development and spent some time to learn about MVC and MVVM design patterns. For next project, the preference is MVVM. At end of this post I will share the reason to choose MVVM. The example code for this post is written for Android application. MVC (Model-View-Controller) Model: Model is data layer. Model call services or database to get data from external systems. View: View is user interface layer. Controller: Controller is triggered first. Controller has reference to Model as well as View. Controller get data from Model and send to View. ...

March 24, 2021 · 5 min · 913 words

Adapter Design Pattern

This design patterns guides to create a wrap around existing class to reuse existing class for a new requirement. Assume, I have two logging classes named WriteLogsToLocalFile and WriteLogsToServer. class WriteLogsToLocalFile { public void Log(string fileName, string message) { //write logs to file } } class WriteLogsToServer { public void Log(string urL, string message, string token) { //call rest API with oAuth // Note: here token is used for oAuth } } static void Main(string[] args) { WriteLogsToLocalFile fileLogs = new WriteLogsToLocalFile(); fileLogs.Log("hello.xml", "login success"); WriteLogsToServer serverLogs = new WriteLogsToServer(); serverLogs.Log("https://server.logs/upload", "login success", "oAuthToken"); } Here user is manually creating objects to write logs separately to local file and server.My requirement is to create a common class for logging, so that client can call common class. The common class should blindly recurse through list of loggers to log message. In below code, a new interface ILog is not compatible with WriteLogsToLocalFile and WriteLogsToServer. We created WriteLogsToServerAdapter and WriteLogsToServerAdapter. Logging is helper class. ...

February 13, 2021 · 2 min · 333 words

Composite Design Pattern

The Composite Design pattern is structural design pattern. This design pattern is used when we want to treat a group of objects in same way. The composite design pattern has following elements. Component This is a class that contain all the members that needs to be implemented by all the objects. Composite This class is used to add, remove and traverse components. Leaf The leaf object in tree structure is defined as leaf element. Code Sample I want to print salaries of all employees in my organization with names and designation. The root of organization tree is CEO. The CEO has directors as child composite. The director has managers as child composite. Finally, engineers at level 1 as leaf because no one is reporting them. // ignore this, just to print static int i = 0; interface IComponent { void print(); } class Composite : IComponent { private string name; private string designation; private decimal salary; private List<IComponent> listComponent = new List<IComponent>(); public Composite(String name, String designation, decimal salary) { this.name = name; this.designation = designation; this.salary = salary; } public void addComponent(IComponent component) { listComponent.Add(component); } public void printDesignation() { Console.Write(designation); Console.Write(" "); } public void printName() { Console.Write(name); Console.Write(" "); } public void printSalary() { Console.Write(salary); Console.Write(" "); } public void print() { printName(); printDesignation(); printSalary(); Console.WriteLine(" "); i++; foreach (var component in listComponent) { for(int k =0;k <i; k++) { Console.Write(" - "); } component.print(); } } } class Leaf : IComponent { private string name; private string designation; private decimal salary; public Leaf(String name, String designation, decimal salary) { this.name = name; this.designation = designation; this.salary = salary; } public void print() { printName(); printDesignation(); printSalary(); Console.WriteLine(" "); } public void printDesignation() { Console.Write(designation); Console.Write(" "); } public void printName() { Console.Write(name); Console.Write(" "); } public void printSalary() { Console.Write(salary); Console.Write(" "); } } static void Main(string[] args) { Leaf ram = new Leaf("Ram", "SE1", 1000000); Leaf sham = new Leaf("Sham", "SE1", 1000000); Composite teamLead = new Composite("Mohan", "Team Lead", 1000000); teamLead.addComponent(ram); teamLead.addComponent(sham); Composite manager = new Composite("John", "Manager", 1000000); manager.addComponent(teamLead); Composite director = new Composite("kuku", "Director", 1000000); director.addComponent(manager); //traverse through all director.print(); } } Output: ...

January 24, 2021 · 2 min · 385 words

DoubleDispatch

To understand Double Dispatch, understanding of Overloading and Overriding is must. I already talked about Overloading and Overriding in post Overloading & Overriding. One level of virtual dispatching derived types override a base types: as shown below class Farms { public virtual void Irrigation() { Console.WriteLine("Farm Type"); } } class WheatFarm : Farms { public override void Irrigation() { Console.WriteLine("WheatFarm"); } } class RicaFarm : WheatFarm { public override void Irrigation() { Console.WriteLine("RicaFarm"); } } static void Main(string[] args) { var a = new Farms(); var b = new WheatFarm(); var c = new RicaFarm(); a.Irrigation(); b.Irrigation(); c.Irrigation(); } //output Farm Type WheatFarm RicaFarm Two level of virtual dispatching (Double Dispatch) This concept is used in Visitor Design Pattern. use polymorphic static binding technique to ensure that proper overload is called class Crop { public virtual void CropName(CropWatering obj) { obj.WaterSupply(this); } } class Wheat : Crop { public override void CropName(CropWatering obj) { obj.WaterSupply(this); } } /* An example of overloading (Method with same name but different parameter type) * */ class CropWatering { public virtual void WaterSupply(Crop crop) { Console.WriteLine("CropWatering working on type Crop"); } public virtual void WaterSupply(Wheat wheat) { Console.WriteLine("CropWatering working on type Wheat"); } } class CropWatringSpring : CropWatering { public override void WaterSupply(Crop crop) { Console.WriteLine("CropWatringSpring working on type Crop"); } public override void WaterSupply(Wheat wheat) { Console.WriteLine("CropWatringSpring working on type Wheat"); } } static void Main(string[] args) { Crop crop = new Crop(); Wheat wheat = new Wheat(); CropWatering cropWatering = new CropWatering(); crop.CropName(cropWatering); wheat.CropName(cropWatering); CropWatringSpring cropWatringSpring = new CropWatringSpring(); crop.CropName(cropWatringSpring); wheat.CropName(cropWatringSpring); } //output CropWatering working on type Crop CropWatering working on type Wheat CropWatringSpring working on type Crop CropWatringSpring working on type Wheat Reference

December 28, 2020 · 2 min · 282 words

Overloading & Overriding

To learn about Single & Double Dispatch, many design patterns, we need to understand Overloading and Overriding. Overloading Overloading is compile-time polymorphism. The methods/functions with same name but different number/type parameters are example of Overloading. As Overloading is compile-time, means during run-time the base type is considered. Example: class Crop { public virtual void CropName() { Console.WriteLine("Hey, My type is Crop"); } } class Wheat : Crop { public override void CropName() { Console.WriteLine("Hey, My type is Wheat"); } } /* An example of overloading (Method with same name but different parameter type) * */ class CropWatering { public void WaterSupply(Crop crop) { Console.WriteLine("I am working on type Crop"); } public void WaterSupply(Wheat wheat) { Console.WriteLine("I am working on type Wheat"); } } In main, If I create an object of type Crop and Wheat, an expected result will appear in console. ...

December 15, 2020 · 2 min · 364 words

Book Summary: Don’t make me think - Steve Krug

The book “Don’t make me think” is written by Steve Krug. This is the first book that I read about UX(User Experience Design) design. This book is concise and has four sections. The “guiding principles” talks about not puzzling the user to find required content. These principles guide to omit needless content, using conventions, images and explains the web page scanning habit of user (not reading complete content). The section “Things you need to get right” has content to design navigation that helps user to find their way. The big bang theory of web design is about designing home page, using tag lines (nothing beats a good tagline), welcome blurb and testing home page usability. The section “Making sure you got them right” is all about usability testing. Resolving conflicting thoughts between developers, designers, marketing and project managers, this section also talks about usability testing on 10 cents a day. Focusing on early usability tests, do it yourself usability testing and how often, this section importantly explains feedback loop. Finally, this section has guidance about “deciding what to fix” from feedback. ...

December 14, 2020 · 2 min · 312 words