You are on page 1of 5

Design Patterns in Java

A design patterns are well-proved solution for solving the specific problem/task in


a particular scenario or use case.

Why to use design patterns in programming:


1. They are reusable in multiple projects.
2. They provide the solutions that help to define the system architecture.
3. They capture the software engineering experiences.
4. They provide transparency to the design of an application.
5. They are well-proved and testified solutions since they have been built upon the
knowledge and experience of expert software developers.

We use the design patterns during the analysis and requirement phase of Software
Development.

Design patterns ease the analysis and requirement phase of product by


implementing solution based on prior hands-on experiences.

Types of design patterns


We use mainly three types of design patterns in programming languages – specially
object oriented programming paradigm.
 Creational Design Pattern
o Singleton Design Pattern
o Factory Design Pattern
 Structural Design Pattern
 Behavioral Design Pattern
Singleton Pattern

To create the singleton class, we need to have static member of class, private
constructor and static factory method.

o Static member: It gets memory only once because of static, itcontains the
instance of the Singleton class.
o Private constructor: It will prevent to instantiate the Singleton class from
outside the class.
o Static factory method: This provides the global point of access to the
Singleton object and returns the instance to the caller.

In such case, we create the instance of the class at the time of declaring the static data
member, so instance of the class is created at the time of classloading.

1. class A{  
2.  private static A obj;  
3.  private A(){}  
4.    
5.  public static A getA(){  
6.    if (obj == null){  
7.       synchronized(Singleton.class){  
8.         if (obj == null){  
9.             obj = new Singleton();//instance will be created at request time  
10.         }  
11.     }              
12.     }  
13.   return obj;  
14.  }  
15.   
16.  public void doSomething(){  
17.  //write your code  
18.  }  
19. } 
Factory Method Pattern
A Factory Pattern or Factory Method Pattern says that just define an interface or
abstract class for creating an object but let the subclasses decide which class to
instantiate. In other words, subclasses are responsible to create the instance of the
class.

Advantage of Factory Design Pattern


o Factory Method Pattern allows the sub-classes to choose the type of objects to
create.
o It promotes the loose-coupling

Proxy Pattern
Simply, proxy means an object representing another object.

According to GoF, a Proxy Pattern "provides the control for accessing the original
object".

Use of Proxy Pattern


o It provides the protection to the original object from the outside world.

 Create an OfficeInternetAccess interface.

public interface OfficeInternetAccess {  
    public void grantInternetAccess();  
}  

Create a RealInternetAccess class that will implement OfficeInternetAccess interface


for granting the permission to the specific employee.

public class RealInternetAccess implements OfficeInternetAccess {  
    private String employeeName;  
    public RealInternetAccess(String empName) {  
        this.employeeName = empName;  
    }  
    @Override  
    public void grantInternetAccess() {  
        System.out.println("Internet Access granted for employee: "+ employeeNam
e);  
    }  
}  
Create a ProxyInternetAccess class that will implement OfficeInternetAccess interface for
providing the object of RealInternetAccess class

public class ProxyInternetAccess implements OfficeInternetAccess {  
           private String employeeName;  
           private RealInternetAccess  realaccess;  
               public ProxyInternetAccess(String employeeName) {  
            this.employeeName = employeeName;  
        }  
        @Override  
        public void grantInternetAccess()   
        {  
            if (getRole(employeeName) > 4)   
            {  
                realaccess = new RealInternetAccess(employeeName);  
                realaccess.grantInternetAccess();  
            }   
            else   
            {  
                System.out.println("No Internet access granted. Your job level is below 5");  
            }  
        }  
        public int getRole(String emplName) {  
            // Check role from the database based on Name and designation  
            // return job level or job designation.  
            return 9;  
        }  
}  
Now, Create a ProxyPatternClient class that can access the internet actually.

public class ProxyPatternClient {  
    public static void main(String[] args)   
    {  
        OfficeInternetAccess access = new ProxyInternetAccess("Ashwani Rajput");  
        access.grantInternetAccess();  
    }  
}  

You might also like