Sign in

Technical Writer | Tech Enthusiast | Open source contributor
Photo by Kelly Sikkema on Unsplash

Singleton design pattern is the one of easiest design pattern and it is a creational design pattern. Let's go through what is singleton, where should we user it, how to implement it and implementation in goLang.

What is singleton design pattern

It says single. Yes singleton design pattern use only single object throughout the application. It saves memory and help garbage collector by not giving many objects to clean.

Then What If I initiate an object with the Class? Basically it gives the same object and it does not create a new one.

Where should we use it

  • Where we need only one instance of the class. …


Photo by CHUTTERSNAP on Unsplash

Should I make the object in my own or let some factory to make it?

Programs must be expandable and they eventually expands hence you need to write codes which can be expand without much effort.

Factory design pattern is a creational design pattern to predefined blueprint of a solution for a creational problem. First let's dive in to the problem that we are going to solve using factory design pattern.

Problem

Let's take a simple example and illustrate the problem. There you asked to make a program to manage shoe making and you make a program as follows.

Then…


Photo by Markus Winkler on Unsplash

This is a simple approach which we need on daily basis which is find and kill a port.

Following are the steps to find and kill the port.

  1. You should find the process id which is known as pid using the port as follows.
sudo lsof -i :<port_id>

Following is a sample output for sudo lsof -i :3000

COMMAND      PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
docker-pr 299618 root 4u IPv4 3950094 0t0 TCP *:3000 (LISTEN)

2. Then get the pid which is bolded in the above example and kill it as follows. kill [option] <pid>

kill -15 299618…


Photo by Kelly Sikkema on Unsplash

Let’s do those with command line interface which is much easier.

First thing first. You should update and upgrade first.

$ sudo apt update
$ sudo apt upgrade

Install git

$ sudo apt install git

Yes it is that simpler.

Install npm

$ sudo apt install npm

Install VS code

$ sudo apt install software-properties-common apt-transport-https wget
$ wget –q https://packages.microsoft.com/key/microsoft.asc -O- | sudo apt-key add -
$ sudo add-apt-repository “deb[arch=amd64] https://packages.microsoft.com/repos/vscode stable main”
$ sudo apt install code

Then simply run the VS code using code . .

Install mysql-server

$ sudo apt install mysql-server

Hope this will be helpful.

If you have found this helpful please hit that 👏 and share it on social media :).


Photo by Nihal Demirci on Unsplash

I love Ballerina functions since it gives more flexibility and super easy to do a task.

This will cover basics of Ballerina language functions and following is the basic structure of a function.

function add(int numOne, int numTwo) returns int {
return numOne + numTwo;
}

Let’s play with Ballerina functions.

Required parameters

In the above function numOne, numTwo are required parameters and you should use the function as follows.

import ballerina/io;function main() {
int sum = add(1, 3);
io:println(sum);
}

Important : In Ballerina you can swipe the required parameter using names as follows.

import ballerina/io;function main() {…


Photo by Jeffrey Wegrzyn on Unsplash

Stream API is data flows from one side to other side.

Stream API is introduced in Java 8 in order to bring functional programming into Java.

What Stream API is

  • It is a data pipeline.
  • It use to process collection of objects or arrays.
  • It will not changed the original source.
  • It is efficient in coding.

In Stream API there are basically three major components as follows.


Photo by Thomas Franke on Unsplash

Is it use for data hiding? Yes it is Encapsulation is data hiding. But what is the point of hiding data.

  • Encapsulation limits the access for data.

Some cases we need to provide read only access for that we use encapsulation. Let’s take an example — For a student, student id should not be able to change after it assigned as follows. Then we have to make that variable private and make a method to read it. Then it cannot be changed.

public class Students {  private String name;
private String address;
private String id;
Student(String name, String…


Photo by Christopher Gower on Unsplash

Jdbc is heavily used in Spring boot to CRUD data. Let’s go through how to insert, retrieve, update and delete data using JdbcTemplate. JdbcTemplate is inside the org.springframework.jdbc.core.JdbcTemplate.

Insert, Update and Delete data

In JdbcTemplate insert, update, delete are done using query function as follows and it will return 1 if successfully done the job or else it will return 0.

query = "INSERT INTO Student Values("Yashod", 25)";
int updated = this.jdbcTemplate.update(query);

Wait what if we need to inject data to the query? Okay following is some example.

query = "INSERT INTO Student Values(?, ?)";
this.jdbcTemplate.update(query, "Yashod", 25);

Retrieve data

This is bit tricky. There are several…


Photo by Camille Orgel on Unsplash

What are hooks?

Hooks are basically used to manage states in functional components and there are several considerations you have to follow when using hooks which are,

  • Hooks cannot be used inside a condition, loop or nested function.
  • Hooks cannot be called from a class component or regular java script functions.

You can find out about class component life cycle methods from following.

useState hook

useState hook initialise the state in the functional component. It will output two things,

  • A value to access.
  • A function to change the value.
const [name, setName] = useState("Yashod");

Following is a basic demo of useState hook.

import React, {…


Photo by Matthew T Rader on Unsplash

Have you ever noticed that in react components every element must wrap inside a parent or else give an error as follows.

import React, { Component } from 'react';class Fragments extends Component {

render() {
return (
<h3>Name</h3>
<p>Please insert the name and tap enter</p>
);
}
}
export default Fragments;

And pop up following error.

Yashod Perera

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store