Friday 2 June 2023

Design Patterns

1) Singleton Design Pattern: In this pattern we create the object only once and reuse existing object as object creation is costly affair.

2) Factory Design Pattern: This pattern can be used when we have multiple concrete classes and we want to centralise object creation in one place called Factory class.

    Drawbacks:

       2.1)Data is exposed to the client which is not good.

       2.2)No abstraction in object creation.

       2.3)Many if else or switch cases in case concrete classes increase.

3)Abstract Factory Design Pattern:  This can be used to overcome the limitation of Factory design pattern. In this case we create factory classes of each concrete class and those factories are exposed to the client.

         Example: Let say we have Computer class as base class and has 2 concrete class say PC and Server. So in order to create object of PC and Server we will have two factories like PCFactory and ServerFactory 

public abstract class Computer {
     
    public abstract String getRAM();
    public abstract String getHDD();
    public abstract String getCPU();
     
    @Override
    public String toString(){
        return "RAM= "+this.getRAM()+", HDD="+this.getHDD()+", CPU="+this.getCPU();
    }
}


   

  public class PC extends Computer {
    private String ram;
    private String hdd;
    private String cpu;
     
    public PC(String ram, String hdd, String cpu){
        this.ram=ram;
        this.hdd=hdd;
        this.cpu=cpu;
    }
    @Override
    public String getRAM() {
        return this.ram;
    }
 
    @Override
    public String getHDD() {
        return this.hdd;
    }
 
    @Override
    public String getCPU() {
        return this.cpu;
    }
 
}


public class Server extends Computer {
 
    private String ram;
    private String hdd;
    private String cpu;
     
    public Server(String ram, String hdd, String cpu){
        this.ram=ram;
        this.hdd=hdd;
        this.cpu=cpu;
    }
    @Override
    public String getRAM() {
        return this.ram;
    }
 
    @Override
    public String getHDD() {
        return this.hdd;
    }
 
    @Override
    public String getCPU() {
        return this.cpu;
    }
 
}

import com.journaldev.design.model.Computer;

public interface ComputerAbstractFactory {

	public Computer createComputer();

}
public class PCFactory implements ComputerAbstractFactory {

	private String ram;
	private String hdd;
	private String cpu;
	
	public PCFactory(String ram, String hdd, String cpu){
		this.ram=ram;
		this.hdd=hdd;
		this.cpu=cpu;
	}
	@Override
	public Computer createComputer() {
		return new PC(ram,hdd,cpu);
	}

}
public class ServerFactory implements ComputerAbstractFactory {

	private String ram;
	private String hdd;
	private String cpu;
	
	public ServerFactory(String ram, String hdd, String cpu){
		this.ram=ram;
		this.hdd=hdd;
		this.cpu=cpu;
	}
	
	@Override
	public Computer createComputer() {
		return new Server(ram,hdd,cpu);
	}

}

public class ComputerFactory {

	public static Computer getComputer(ComputerAbstractFactory factory){
		return factory.createComputer();
	}
}
public class TestDesignPatterns {

	public static void main(String[] args) {
		testAbstractFactory();
	}

	private static void testAbstractFactory() {
		Computer pc = ComputerFactory.getComputer(new PCFactory("2 GB","500 GB","2.4 GHz"));
		Computer server = ComputerFactory.getComputer(new ServerFactory("16 GB","1 TB","2.9 GHz"));
		System.out.println("AbstractFactory PC Config::"+pc);
		System.out.println("AbstractFactory Server Config::"+server);
	}

 

Advantages:

       3.1)Provides approach to code for interfaces rather than classes.

       3.2)Easily accomodate more classes object creation 

       3.3)Its robust and avoid conditional logic like Factory pattern.

Examples:

         javax.xml.parsers.DocumentBuilderFactory # newInstance()

         javax.xml.parsers.transform.TransformerFactory #newInstance()

        javax.xml.parsers.xpath.XPathFactory # newInstance()


4)Builder  Design Pattern: Builder is a creational patterns used to create complex objects when there are lot of attributes for object and some of them are optional and some are required. In order to create object it takes help of builder object and the idea is not expose all attributes to the client as well.



 

  

      








No comments:

Post a Comment