Posts

Testing private method in Java

Private method in java is not accessible outside the Class which defines it. Since they cannot be accessed we cannot call them from any Test Class directly to test them. Following are some of the options to test the private method 1) Change the visibility of the private method to package or protected. 2) Create a public method in the Class and call the private method from the public method Above two solutions requires us to change the original Class. Some times we cannot change the original Class but still would like to have a Test Class for the same method. The other option is to use Reflection api to invoke the private method. For example lets try to test a private method multiple on Class CreditcardMask.java Class targetClass = (Class ) Class.forName(CreditcardMask.class.getName()); // get the method multiply Method testMethod = targetClass.getDeclaredMethod("multiply", Integer.class, Integer.class); Assert.assertNotNull("testMethod is Null", tes...

Proxy Design Pattern In java

Image
Type : Structural Design Pattern Summary Proxy Design pattern lets a Proxy Object rather then the real object to interact with Client. By doing so the proxy can control access to the real object or delay the creation of real object unless it is is really needed. Description In Proxy Design pattern the Client is served with a Proxy Object rather than the real object. Benefit of doing so is that, we have greater control in when to create the real object. It would help us in providing in lazy loading the real object, providing extra security checking etc.  Example For example lets consider a service called UserDetails Service which the Client calls to get UserDetails Object. Now say  UserDetails is a complex object, and we need to real the database to populate it. Also we would like to have a security authorization check by which we will not show the details at all or show only few attributes from UserDetails Object. Returning a Proxy rather than orig...

Flyweight Pattern in Java

Image
Type : Structural Design Pattern Summary Flyweight design pattern deals with sharing some of the internal details between different object instances, so that we can save memory by basically using less duplication of the attributes across its instances. Details In Flyweight design pattern the Object attributes are categorized into its extrinsic and intrinsic attributes.  Extrinsic  attributes  are the attributes that can be considered as sharable across other instances.  Intrinsic  attributes  are the attributes that are unique for each object, like the primary key, unique fields etc. In Flyweight design we can improve the memory footprint by reusing the extrinsic properties in multiple instances of the object. Flyweight can use Cache to store the different Extrinsic states. Factory method can be used to create the instances of the Extrinsic object and stored in cache. Example For instance say we are designing a Vehicle in OOP. ...

Adapter Design Pattern in Java

Image
Type : Structural Design Pattern Summary Adapter design pattern is a Structural design pattern. It is used to provides compatible interface for and Class so that can be used by Client. Adapter pattern is used to provide compatibility for Classes which already exists and cannot be modified. Details Need for adapter design pattern is very obvious. It is also very common in real world as well as in Software Engineering. For instance if we have any electrical appliances which runs on 110 Volt and then we travel to a country where the electricity supply is 220 V. We cannot change or modify the electric connection to provide us 110 V. Rather we can use a adapter to convert the 220 V from outlet to 110V. Adapter pattern is similar to Bridge pattern because both provide mechanism to connect incompatible Classes. Advantages Incompatible Object or Class can be used using an Adapter for it. Disadvantages Increases number of Classes Adapter Class needs to be modif...

Abstract Factory Design Pattern in Java

Image
Type:  Creational Design Pattern Summary Provide an indirect way to create object that abstracts the creation of families of related objects without directly specifying their concrete Classes. The Factory owns the responsibility create objects. Client is not aware of how to create the object. Details It is similar to Factory method design pattern. Only difference is that rather than returning instance of Subclasses, Abstract factory method can create object of Subclasses having different inheritance hierarchy. Generally Abstract Factory will return different Factories, which in turn can be used to Objects Abstract Factory is used when we want to create objects by Clients and control which instance should be returned. It is used in DAO (Data Access Object). It can be used to design Platform dependent software solution, by providing platform specific implementations.   Example We will extend the example of Sales Man and Differe...

Factory Design Pattern in Java

Image
Type:  Creational Design Pattern Summary Factory or Factory method Design pattern is a creational design pattern. It helps in loose coupling between client and the actual object that the Client needs. Rather than Client calling the constructor using new operator it calls the Factory and the factory returns the respective object. Details Factory method design pattern encapsulate the complexities around creating an object and also uses Inheritance by letting factory method to decide which Subclass instance is needed to be returned. Factory method pattern solves the issue of creating object without exposing internal details by Provide an interface (Could be an Interface or Abstract Class) and a factory method let the Client create the appropriate object by calling the factory method. Example Let's say we have a company ABC Cars Corp, which manufactures different type's of Cars (Sedan, SUV etc).  Now we want to provide a way to the Sales man to be able to ge...

Singleton Design Pattern

Image
Creational Design Pattern Summary Singleton Design pattern is used to provide a mechanism so that a Class can have only one instance. So when Client try's to use an instance of Singleton Class it gets the same instance.  Singleton. Details Singleton Design pattern make sure that Class which implement this pattern can have only one instance. In case of Java, only one instance in a single JVM. For instance if we want only one instance of Cache Class which would store temporary values in memory. Singleton pattern is also used in Logger Classes. Logger provide single instance for logging which is used by application code. Singleton design pattern is implemented by following mechanism 1) Making the constructor private. So that Client cannot create different instances 2) Provide a static method, which the client will interact to get the instance of the class 3) Do not offer any setter method for the object 4) If any getter for individual property return...