Showing posts with label Programming & Web Development. Show all posts
Showing posts with label Programming & Web Development. Show all posts

Wednesday, May 18, 2022

How I built a real estate sales representative website using Wordpress

This video explains how I made a real estate sales representative website using Wordpress. The website is built using "blogwaves" theme and a number of plugins like "Accordion Slider Gallery", "Easy Property Listings", "Ninja Forms", "MetaSlider", etc.

 

If you are interested in making a custom website, please give me a call at (+1) 647-401-8611 or email me at lokprakashpandey@gmail.com

Friday, January 3, 2020

SOLID Principles

The principles that SOLID outlines are important to writing clear maintainable code. SOLID is an acronym. It describes five principles that developers should always consider when writing code. Those five principles are:
S – Single Responsibility
O – Open for Extension / Closed for Modification
L – Liskov Substitution
I – Interface Segregation
D – Dependency Inversion


Single Responsibility – A class should have a single responsibility. Generally, responsibilities are potential points of change. So, it follows that the more a class is responsible for the more likely it will need to change as the application utilizing the class evolves changes. Keeping the classes focused on a specific responsibility ensures that they perform their task effectively and efficiently.


Open for Extension / Closed for Modification – Classes are generally instantiated in several different parts of an application. For example, let us take an example of a Customer class in an order/entry system. The Customer class will probably be instantiated when creating a new customer, updating the customer, adding orders to a customer and so on. If a developer needs to modify the Customer class according to a new requirement that developer will need to understand the where and the why that class is used in every part of the application. This can become overwhelming quickly. Providing extensibility points in a class through inheritance developers can override any methods or properties marked as virtual.


Liskov Substitution – This principle states that ‘if a child class C is a subtype of P then objects of type P may be replaced with objects of type C without altering the correctness of that program’. What it means is that the child classes cannot break the functionality of the parent. The classic example is one in which we have a rectangle parent class and a child square class. The square class is-a rectangle but when we constrain the square to have fixed length and width parameters we break the Liskov Substitution principal because the parent can no longer be replaced with the child class.


Interface Segregation – In alignment with the Single Responsibility principle, the Interface Segregation principle dictates that interfaces should be as simple and focused as possible. It’s better to have multiple interfaces that are focused than a single interface that tries to do everything. In a language like Java or C#, the designer and developer lose nothing by splitting a large interface into multiple interfaces. As we know we can implement several interfaces on a class. Classes that implement interfaces must implement all the properties the interface dictates.


Dependency Inversion – Dependencies should rely on abstractions rather than concretions. The Dependency Inversion principle specifies that we should move the instantiation of classes that a higher-level class depends on outside of that class thus inverting the dependency. This allows us to replace the dependency with another class and not break our open/closed principle.

For example: Let us analyze the following code:

using System;

public class Program
{
private static Computer _computer;
public static void Main()
{
_computer = new Computer();
_computer.Test();
}
}

public class Computer
{
private MotherBoard _mb;
private Cpu _cpu;
private Ram _ram;
private HardDrive _hd;
private PowerSupply _ps;
public Computer()
{
_mb = new MotherBoard();
_cpu = new Cpu();
_ram = new Ram();
_hd = new HardDrive();
_ps = new PowerSupply();
}

public void Test()
{
Console.WriteLine(_mb.Motherboarding());
Console.WriteLine(_cpu.Calculate());
Console.WriteLine(_ram.StoringData());
Console.WriteLine(_hd.WritingData());
Console.WriteLine(_ps.ProducePower());
}
}

internal class PowerSupply
{
public string ProducePower()
{
return "Producing Power";
}
}

internal class HardDrive
{
public string WritingData()
{
return "Writing Data";
}
}

internal class Ram
{
public string StoringData()
{
return "Storing Data";
}
}

internal class Cpu
{
public string Calculate()
{
return "Calculating";
}
}

internal class MotherBoard
{
public string Motherboarding()
{
return "Connecting everything";
}

}


This code is not SOLID. Now, what we must do is we have to be able to build mobile, computer, laptop, etc. as needed using SOLID principles. The final code may look like this:
using System;
using System.Collections.Generic;
namespace Test
{    
public class Program
    {
        private static void Build(Device Dev)
        {
            Dev.Test();
        }
        public static void Main()
        {
            Build(new Device(new List<IPart>{
                new MotherBoard(),new Cpu(),
                new Ram(), new HardDrive(),
                new PowerSupply() }));
            Build(new Device(new List<IPart>{
                new MotherBoard(),new Cpu(),
                new Ram(), new HardDrive(),
                new PowerSupply(), new Battery() }));
          }
    }
    //Open Closed Principle
    internal class Device
    {
        private List<IPart> _parts;
        //Dependency Inversion
        public Device(List<IPart> parts)
        {
            _parts = parts;
        }
        public void Test()
        {
             //Liskov Substitution Principle
            foreach(IPart p in _parts)
                Console.WriteLine(p.MyMessage());
        }
    }
    //single responsibility principle
    internal class Battery : IPart
    {
        public string MyMessage()
        {
                return "Charging";
        }
    }
    internal class PowerSupply : IPart
    {
        public string MyMessage()
        {
                return "Producing Power";
        }
    }
    internal class HardDrive : IPart
    {
        public string MyMessage()
        {
                return "Writing Data";
        }
    }
    internal class Ram : IPart
    {
        public string MyMessage()
        {
                return "Storing Data";
        }
    }
    internal class Cpu : IPart
    {
        public string MyMessage()
        {
            return "Calculating";
        }
    }
    internal class MotherBoard: IPart
    {
        public string MyMessage()
        {
                return "Connecting everything";
        }
    }    //Interface Segregation
    interface IPart
    {
        string MyMessage();
    }

Monday, November 11, 2019

Object-oriented programming and design



When creating a software system for a particular problem domain, the problem is carefully analyzed and a design or working model is worked upon [1]. If this process involves an object-oriented point of view, then it is called object-oriented analysis and design (OOAD) process [1]. The design or model is created in the OOAD process using class diagrams [4]. To implement such model, object-oriented programming (OOP) is needed [1]. OOP is currently the world’s most widely practiced programming paradigm [1].

History
Alan Kay is considered to be the inventor of what we call today “object-oriented development” [8]. Before the notion of having real world objects to solve problems, structured programming was in place [2]. The major drawback of this approach was that data was not secure [2]. Additionally, there was problem with manageability of software system for large systems [2]. OOP was introduced to tackle these problems [2]. The OOP introduced ways to limit access to data and many design patterns and architectures were introduced around OOP to manage complex systems. There were many different works by many different inventors around the world for OOP starting from the 1960s [8]. Among them, the main two notable contributions were created by Bjarne Stroustrup in 1979 with the invention of C++ and by James Gosling in 1995 with the invention of Java [3, 6]. The essence of OOP has remained the same throughout these years however various OOP languages, frameworks and design patterns have come out and become popular [9]. The frameworks provide boilerplate code for rapid application development and the design patterns plus architecture provide best practice guidelines for maintainable code [9, 10]. Some popular frameworks are Spring (Java), Express (JavaScript), Laravel (PHP), etc. SOLID and GRASP are examples of prevalent OOP architectures and Singleton, Factory, etc. are popular design patterns nowadays [9, 10].


Major Concepts
OOP involves the following major three concepts: Encapsulation, Inheritance and Polymorphism [2]. Encapsulation is the representation of a real-world entity by combining the characteristics and the associated behaviors of that entity [2]. It is implemented by the use of a class that encapsulates data and methods that act on the data [2]. It provides data hiding by utilizing access modifiers thereby providing data security on need basis [2]. Inheritance provides hierarchical categorization of real-world entities, so as to conveniently model the system [2]. It is implemented by the use of superclass-subclass relationship where common characteristics and behaviors are implemented in superclass and specialized characteristics and behaviors are implemented in subclass [2]. It provides code reusability where once written and tested class can be reused later in the inheritance hierarchy [2]. Polymorphism means having multiple behavioral forms depending on the entity or context [2]. It is implemented by the use of method overloading which provides context at compile time and method overriding which provides context at runtime [2]. It increases code readability, reduces code redundancy and supports lose coupling thereby making code more maintainable for developers [2].

The OOP languages are basically divided into following three types according to their inclination towards object-orientation [8]:
  • Pure OO languages: In these languages, everything is object including primitives [8]. Most popular example is Ruby [8].
  • OO with procedural components: In these languages, there are procedural components like primitives but they support all features of pure OO languages [8]. Most popular example is Java [8].
  • Object based languages / Prototype-based languages: In these languages, they do not include all the features of object orientation or have support in a different way [7]. Most popular example is JavaScript which is used to provide prototype-based full object orientation but currently can provide object-based also [8].


Implementation
The most pervasive programming language in the world currently is Java [6]. Java supports all features of OOP plus contains some procedural features like primitives and lambdas [6]. It provides encapsulation by the use of a class utilizing access modifiers like private, package-private (default), protected and public as:




Similarly, Java provides inheritance with superclass-subclass relationship as:




Further, Java provides polymorphism in many ways out of which one example is:
 




In this implementation of polymorphism, the appropriate version of myMethod() is called depending on the object that is in context. This is called dynamic polymorphism or late binding in Java [6]. We also have static polymorphism or early binding in Java via method overloading [6].

JavaScript (JS) is another popular language in the world. But it is used for more of functional programming rather than object-oriented [7]. In current version of JS, functions are objects [5,7]. Earlier JS used to be object based only but with the introduction of class in ECMAScript 6, it can now support object orientation [5]. Also, with the introduction of asynchronous programming in JS, it is currently one of the most powerful programming languages [5].



For OO, we can write encapsulation code in JS as:



The same code with class can be written nowadays as:





Inheritance in JS can be achieved similar to Java as:
 


Polymorphism in JS can be realized as:



Thus, we can conclude that JavaScript and Java are both powerful languages as both can be used to implement OOP and solve real-world problems. However, Java is more preferred in electronic devices and large financial applications such as banks whereas JavaScript is more chosen for web development for both client-side and server-side programming [6, 7].

Further Reading
Due to the limitation of time and other constraints, this work cannot get into more detail explaining every aspect of OOP. As an enthusiast on the art of OOP, it might be a good idea to take a deep dive on Gang of Four, SOLID, and GRASP techniques [10]. Further newer technologies like asynchronous programming may be a good topic for another study.


References
  1. Deitel, P., & Deitel, Harvey M. (2012). Java : How to program (9th ed., How to program series). Upper Saddle River, N.J.: Prentice Hall.
  2. Ganesh, S.G., Kiran H., & Sharma T. (2016). Oracle Certified Professional Java SE 8 Programmer Exam 1Z0-809: A Comprehensive OCPJP 8 Certification Guide. Apress.
  3. https://en.wikipedia.org/wiki/C++
  4. https://en.wikipedia.org/wiki/Class_diagram
  5. https://en.wikipedia.org/wiki/ECMAScript
  6. https://en.wikipedia.org/wiki/Java_(programming_language)
  7. https://en.wikipedia.org/wiki/JavaScript
  8. https://en.wikipedia.org/wiki/Object-oriented_programming
  9. https://en.wikipedia.org/wiki/Software_design_pattern
  10. https://en.wikipedia.org/wiki/SOLID