Singleton Design Pattern

Singleton Design Pattern
(Creational Design Pattern)

Singleton design pattern is the simplest design pattern we can found. It allows only to create only one instance at a time from the given class.

e.g. clipboard in windows and mobile devices

A singleton class,

  • Should be a private class
  • provides a global access point to the class (private static)


Class diagram

Facade Design Pattern

Facade Design Pattern
(Structural Design Pattern)

When it come to software development, following a design pattern is must if we need to develop a proper software application

Sometimes, earlier we used this structure of the facade design pattern while development without know the name of it.

It hides the complexity of the code by providing an easy interface to the user that can access the whole system. A single class provides all the methods required by the user from the system.

Implementation with Java

Lets assume a scenario where simple bank transaction process of withdraw money and deposit money.

Class diagram for bank transaction


Delete files from FTP server

When we need to delete files which hosted on FTP server we need to get permissions to write. So to do that We need to connect to the FTP server.


1
2
3
4
<?php
$ftp_server = "Ftp_server_address_goes_ehre";
ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
?>


complete PHP code to delete file from FTP server.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
// connect and login to FTP server
$ftp_server = "FTP_server_address_goes_here";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");

$ftp_username = "ftp_username";
$ftp_userpass = "ftp_password";
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
//provide the file name with the path starting from default root directory
$file = "public_html/files/file.txt";

// try to delete file
if (ftp_delete($ftp_conn, $file))
  {
  $msg = "$file deleted";
  }
else
  {
  $msg = "Could not delete $file";
  }
echo $msg;
// close connection
ftp_close($ftp_conn);
?>



Delete files using PHP

Delete uploaded file in PHP using unlink() method.


We can delete files by providing the path to the file in sever. Unlink command work only if  write permissions given for the file.otherwise unlink command will get failed.


//path starting from default root directory
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
$path = "photos/wallpaper.jpg"; 

if(unlink($path)){
$msg = "deleted"; 
}else{
$msg = "cannot delete"; 
}
echo $msg;
?>

Sometimes we will get warning If either the file is not exists or due to write permissions not enabled.
To ignore the warning we can suppress by adding @ symbol 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
//path starting from default root directory
$path = "photos/wallpaper.jpg"; 

if(@unlink($path)){
$msg = "deleted"; 
}else{
$msg = "cannot delete"; 
}
echo $msg;
?>


Load Data to Spinner view in Android

This is a simple example to load spinner view by an array-list.

First of all we need to create an android project. (I used eclipse)
File > New > Android Application Project


Then we need to declare an array-list in order to load values in spinner view. create array-list on Strings.xml file located under values folder.