MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Working with Design Patterns in JAVA

This article will explain the concept of design patterns that are used in JAVA. After this, we will briefly discuss the category of creation design patterns.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

The term “design pattern” is most widely used in the area of software development. For the information of the users, the concept of design patterns came from the book named as Design Patterns - Elements of Reusable Object-Oriented Software. From this book, it is inferred that design pattern are based on the base principles of object orientated design. Design patterns are used to program to an interface and they favour object composition over inheritance. Design patterns provide a standard terminology and are specific to particular scenario i.e., a particular type of design pattern can only be used for a particular purpose. Design patterns are of various types and these types are further divided into different types. Basically, design patterns are of three types:

  • Creational Design Patterns: This type of design patterns provides a way to create objects while hiding the creation logic. They are further divided into five types which are Singleton Design Pattern, Factory Design Pattern, Abstract Factory Design Pattern, Builder Design Pattern and Prototype Design Pattern. In this article, we will be discussing these types of patterns in detail.
  • Structural Design Patterns: This type of design patterns concern class and object composition. In this design patterns, the concept of inheritance is used. They are further divided into seven types which are Adapter Design Pattern, Composite Design Pattern, Proxy Design Pattern, Flyweight Design Pattern, facade Design Pattern, Bridge Design Pattern and Decorator Design Pattern.
  • Behavioral Design Pattern: This type of design patterns are specifically concerned with communication between objects. They are further divided into ten types which are Template Method Design Pattern, Mediator Design Pattern, Chain of Responsibility Design Pattern, Observer Design Pattern, Strategy Design Pattern, Command Design Pattern, State Design Pattern, Visitor Design Pattern, Iterator Design Pattern and Memento Design Pattern.

Now, as mentioned above in description, we will learn about all the five types of creational design patterns. Let us begin with Singleton Design Pattern. It is one of the easiest types of design pattern as it provides the best way to create an object. Singleton design pattern involves a single class which is responsible to creates own object while making sure that only single object get created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class. The singleton class typically has a private constructor to prevent the singleton class from being instantiated via a constructor. Now let us create class using Singleton Design Pattern. The class is named as SingletonObject.

Shows the image of working of Singleton Design Pattern

Figure 1: Shows the image of working of Singleton Design Pattern

Listing 1: Shows the code to create the SingletonObject class

public class SingletonObject {

   //create an object of SingletonObject
   private static SingletonObject instance = new SingletonObject();

   //make the constructor private 
   private SingletonObject(){}

   // only object available
   public static SingletonObject getInstance(){
      return instance;
   }

   public void showMessage(){
      System.out.println("welcome to mrbool");
   }
}
Listing 2: Shows the code to get the only object from the singleton class

public class SingletonDemo {
   public static void main(String[] args) {

      // construct is illegal
      //Compile Time Error: The constructor SingleObject() is not visible
      //SingletonObject object = new SingletonObject();

      
      SingletonObject object = SingletonObject.getInstance();

      // message displayed
      object.showMessage();
   }

Now, we will discuss Factory design patterns. For the beginners, the word factory refers to the Java class that is used to encapsulate object creation code. In this type of design pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface. In implement factory design pattern, several steps are followed. The listing details will provide the details of each step.

Listing 3: Shows the code to create an interface

public interface figure {
   void draw();
}

Listing 4: Shows the code to create concrete classes which implements the same interface (for pentagon.java)

public class pentagon implements figure {

   @Override
   public void draw() {
      System.out.println("Inside pentagon::draw() method.");
   }
}

Listing 5: Shows the code to create concrete classes which implements the same interface (for triangle.java)

public class triangle implements figure {

   @Override
   public void draw() {
      System.out.println("Inside triangle::draw() method.");
   }
}

Listing 6: Shows the code to create concrete classes which implements the same interface (for circle.java)

public class circle implements figure {

   @Override
   public void draw() {
      System.out.println("Inside circle::draw() method.");
   }
}

Listing 7: Shows the code to create a Factory to generate object of concrete class

public class figureFactory {
	
   // getfigure method is used to get object of type figure 
   public figure getfigure(String figureType){
      if(figureType == null){
         return null;
      }		
      if(figureType.equalsIgnoreCase("TRIANGLE")){
         return new triangle();
      } else if(figureType.equalsIgnoreCase("CIRCLE")){
         return new circle();
      } else if(figureType.equalsIgnoreCase("PENTAGON")){
         return new pentagon();
      }
      return null;
   }
}

Listing 8: Shows the code to use the Factory to get object of concrete class

public class FactoryDemo {

   public static void main(String[] args) {
      figureFactory figureFactory = new figureFactory();

      //get an object of triangle and call its draw method.
      figure figure1 = figureFactory.getfigure("TRIANGLE");

      //call draw method of triangle
      figure1.draw();

      //get an object of circle and call its draw method.
      figure figure2 = figureFactory.getfigure("CIRCLE");

      //call draw method of circle
      figure2.draw();

      //get an object of pentagon and call its draw method.
      figure figure3 = figureFactory.getfigure("PENTAGON");

      //call draw method of pentagon
      figure3.draw();
   }
}

Listing 9: Shows the code to verify the output
Inside triangle::draw() method.
Inside circle::draw() method.
Inside pentagon::draw() method.

 
Shows the working of the Factory Design Pattern

Figure 2: Shows the working of the Factory Design Pattern

Now, we will have a look at the Abstract Factory Design Pattern. An abstract factory is a factory that returns factories. In this type of design pattern, an interface is responsible for creating a factory of related objects, without explicitly specifying their classes. Each generated factory can give the objects as per the Factory pattern. In order to implement this design pattern, we will create Figure and Shade interfaces and concrete classes implementing these interfaces.

This procedure follows certain steps and each step is described as a code.

Listing 10: Shows the code to create an interface for figures

public interface figure {
   void draw();
}

Listing 11: Shows the code to create concrete classes implementing the same interface (for pentagon.java)

 //class pentagon
 public class pentagon implements figure {

   @Override
   public void draw() {
      System.out.println("Inside pentagon::draw() method.");
   }
}

Listing 12: Shows the code to create concrete classes implementing the same interface (for triangle.java)

// class triangle
public class triangle implements figure {

   @Override
   public void draw() {
      System.out.println("Inside triangle::draw() method.");
   }
}

Listing 13: Shows the code to create concrete classes implementing the same interface (for circle.java)

public class Circle implements Shape {

   @Override
   public void draw() {
      System.out.println("Inside Circle::draw() method.");
   }
}

Listing 14: Shows the code to create interfaces for shades

public interface Shades {
   void fill();
}

Listing 15: Shows the code to create concrete classes implementing the same interface (for pink.java)

public class pink implements Shade {

   @Override
   public void fill() {
      System.out.println("Inside pink::fill() method.");
   }
}

Listing 16: Shows the code to create concrete classes implementing the same interface (for purple.java)

public class purple implements Shade {

   @Override
   public void fill() {
      System.out.println("Inside purple::fill() method.");
   }
}

Listing 17: Shows the code to create concrete classes implementing the same interface (for cyan.java)

public class cyan implements Shade {

   @Override
   public void fill() {
      System.out.println("Inside cyan::fill() method.");
   }
}

Listing 18: Shows the code to create an Abstract class to get factories for Figure and Shade Objects

public abstract class AbstractFactory {
   abstract Shade getShade(String shade);
   abstract figure getShape(String figure) ;
}

Listing 19: Shows the code to create Factory classes extending AbstractFactory to generate object of concrete class based on given information (for figurefactory.java)

public class figureFactory extends AbstractFactory {
	
   @Override
   public figure getfigure(String figureType){
      if(figureType == null){
         return null;
      }		
      if(figureType.equalsIgnoreCase("TRIANGLE")){
         return new triangle();
      } else if(figureType.equalsIgnoreCase("CIRCLE")){
         return new circle();
      } else if(figureType.equalsIgnoreCase("PENTAGON")){
         return new pentagon();
      }
      return null;
   }
   
   @Override
   Shade getShade(String shade) {
      return null;
   }
}

Listing 20: Create Factory classes extending AbstractFactory to generate object of concrete class based on given information (for shadefactory.java)

public class SahdeFactory extends AbstractFactory {
	
   @Override
   public figure getfigure(String figureType){
      return null;
   }
   
   @Override
   Shade getShade(String Shade) {
      if(Shade == null){
         return null;
      }		
      if(shade.equalsIgnoreCase("PINK")){
         return new pink();
      } else if(shade.equalsIgnoreCase("PURPLE")){
         return new purple();
      } else if(shade.equalsIgnoreCase("CYAN")){
         return new cyan();
      }
      return null;
   }
}

Listing 21: Shows the code to create a Factory generator/producer class to get factories by passing information

public class FactoryProducer {
   public static AbstractFactory getFactory(String choice){
      if(choice.equalsIgnoreCase("FIGURE")){
         return new figureFactory();
      } else if(choice.equalsIgnoreCase("SHADE")){
         return new ShadeFactory();
      }
      return null;
   }
}

Listing 22: Shows the code to use the Factory Producer to get Abstract Factory in order to get factories of concrete classes by passing information

public class AbstractFactoryDemo {
   public static void main(String[] args) {

      //get figure factory
      AbstractFactory figureFactory = FactoryProducer.getFactory("FIGURE");

      //get an object of figure triangle
      figure figure1 = figureFactory.getfigure("TRIANGLE");

      //call draw method of figure triangle
      figure1.draw();

      //get an object of figure circle
      figure figure2 = figureFactory.getfigure("CIRCLE");

      //call draw method of figure circle
      figure2.draw();
      
      //get an object of figure pentagon 
      figure figure3 = figureFactory.getfigure("PENTAGON");

      //call draw method of figure pentagon
      figure3.draw();

      //get shade factory
      AbstractFactory shadeFactory = FactoryProducer.getFactory("SHADE");

      //get an object of Shade pink
      Shade shade1 = shadeFactory.getShade("PINK");

      //call fill method of pink
      shade1.fill();

      //get an object of Shade purple
      Shade shade2 = shadeFactory.getShade("purple");

      //call fill method of  purple
      shade2.fill();

      //get an object of Shade cyan
      Shade shade3 = shadeFactory.getShade("CYAN");

      //call fill method of Shade CYAN
      shade3.fill();
   }
}

Listing 23: Shows the code to verify the output

Inside triangle::draw() method.
Inside circle::draw() method.
Inside pentagon::draw() method.
Inside pink::fill() method.
Inside purple::fill() method.
Inside cyan::fill() method.

Conclusion

So, in brief we discussed the three main type of creational design pattern in details. Hope you like reading it. See you next time!



I have done my bachelor in Computer Science and I am well versed with programming languages such as JAVA, C#, html and done courses on Android development.

What did you think of this post?

Did you like the post?

Help us to keep publishing good contents like this.

SUPPORT US

funded

remaining

[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login