Tutorial

Java Design Patterns - Example Tutorial

Updated on November 15, 2022
authorauthor

Pankaj and Bradley Kouchi

Java Design Patterns - Example Tutorial

Introduction

Design patterns are very popular among software developers. A design pattern is a well-described solution to a common software problem.

Some of the benefits of using design patterns are:

  1. Design patterns are already defined and provide an industry-standard approach to solving a recurring problem, so it saves time if we sensibly use the design pattern. There are many Java design patterns that we can use in our Java-based projects.
  2. Using design patterns promotes reusability that leads to more robust and highly maintainable code. It helps in reducing the total cost of ownership (TCO) of the software product.
  3. Since design patterns are already defined, it makes our code easy to understand and debug. It leads to faster development and new members of the team understand it easily.

Java design patterns are divided into three categories - creational, structural, and behavioral design patterns.

This article serves as an index for all the Java design pattern articles.

Creational Design Patterns

Creational design patterns provide solutions to instantiate an Object in the best possible way for specific situations.

1. Singleton Pattern

The singleton pattern restricts the instantiation of a Class and ensures that only one instance of the class exists in the Java Virtual Machine. The implementation of the singleton pattern has always been a controversial topic among developers.

Note: Learn more about the Singleton Design Pattern.

2. Factory Pattern

The factory design pattern is used when we have a superclass with multiple subclasses and based on input, we need to return one of the subclasses. This pattern takes out the responsibility of the instantiation of a Class from the client program to the factory class. We can apply a singleton pattern on the factory class or make the factory method static.

Note: Learn more about the Factory Design Pattern.

3. Abstract Factory Pattern

The abstract factory pattern is similar to the factory pattern and is a factory of factories. If you are familiar with the factory design pattern in Java, you will notice that we have a single factory class that returns the different subclasses based on the input provided and the factory class uses if-else or switch statements to achieve this. In the abstract factory pattern, we get rid of if-else block and have a factory class for each subclass and then an abstract factory class that will return the subclass based on the input factory class.

Note: Learn more about the Abstract Factory Pattern.

4. Builder Pattern

The builder pattern was introduced to solve some of the problems with factory and abstract Factory design patterns when the object contains a lot of attributes. This pattern solves the issue with a large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object.

Note: Learn more about the Builder Pattern.

5. Prototype Pattern

The prototype pattern is used when the Object creation is costly and requires a lot of time and resources, and you have a similar Object already existing. So this pattern provides a mechanism to copy the original Object to a new Object and then modify it according to our needs. This pattern uses Java cloning to copy the Object. The prototype design pattern mandates that the Object which you are copying should provide the copying feature. It should not be done by any other class. However, whether to use the shallow or deep copy of the object properties depends on the requirements and is a design decision.

Note: Learn more about the Prototype Pattern.

Structural Design Patterns

Structural design patterns provide different ways to create a Class structure (for example, using inheritance and composition to create a large Object from small Objects).

1. Adapter Pattern

The adapter design pattern is one of the structural design patterns and is used so that two unrelated interfaces can work together. The object that joins these unrelated interfaces is called an adapter.

Note: Learn more about the Adapter Pattern.

2. Composite Pattern

The composite pattern is used when we have to represent a part-whole hierarchy. When we need to create a structure in a way that the objects in the structure have to be treated the same way, we can apply the composite design pattern.

Note: Learn more about the Composite Pattern.

3. Proxy Pattern

The proxy pattern provides a placeholder for another Object to control access to it. This pattern is used when we want to provide controlled access to functionality.

Note: Learn more about the Proxy Pattern.

4. Flyweight Pattern

The flyweight design pattern is used when we need to create a lot of Objects of a Class. Since every Object consumes memory space that can be crucial for low-memory devices (such as mobile devices or embedded systems), the flyweight design pattern can be applied to reduce the load on memory by sharing Objects.

String pool implementation in Java is one of the best examples of flyweight pattern implementation.

Note: Learn more about the Flyweight Pattern.

5. Facade Pattern

The facade pattern is used to help client applications easily interact with the system.

Note: Learn more about the Facade Pattern.

6. Bridge Pattern

When we have interface hierarchies in both interfaces as well as implementations, then the bridge design pattern is used to decouple the interfaces from the implementation and to hide the implementation details from the client programs. The implementation of the bridge design pattern follows the notion of preferring composition over inheritance.

Note: Learn more about the Bridge Pattern.

7. Decorator Pattern

The decorator design pattern is used to modify the functionality of an object at runtime. At the same time, other instances of the same class will not be affected by this, so the individual object gets the modified behavior. The decorator design pattern is one of the structural design patterns (such as adapter pattern, bridge pattern, or composite pattern) and uses abstract classes or interface with the composition to implement. We use inheritance or composition to extend the behavior of an object, but this is done at compile-time, and it’s applicable to all the instances of the class. We can’t add any new functionality to remove any existing behavior at runtime – this is when the decorator pattern is useful.

Note: Learn more about the Decorator Pattern.

Behavioral Design Patterns

Behavioral patterns provide a solution for better interaction between objects and how to provide loose-coupling and flexibility to extend easily.

1. Template Method Pattern

The template method pattern is a behavioral design pattern and is used to create a method stub and to defer some of the steps of implementation to the subclasses. The template method defines the steps to execute an algorithm, and it can provide a default implementation that might be common for all or some of the subclasses.

Note: Learn more about the Template Method Pattern.

2. Mediator Pattern

The mediator design pattern is used to provide a centralized communication medium between different objects in a system. If the objects interact with each other directly, the system components are tightly-coupled with each other which makes maintainability cost higher and not flexible to extend easily. The mediator pattern focuses on providing a mediator between objects for communication and implementing loose-coupling between objects. The mediator works as a router between objects, and it can have its own logic to provide a way of communication.

Note: Learn more about the Mediator Pattern

3. Chain of Responsibility Pattern

The chain of responsibility pattern is used to achieve loose-coupling in software design where a request from the client is passed to a chain of objects to process them. Then the object in the chain will decide who will be processing the request and whether the request is required to be sent to the next object in the chain or not.

We know that we can have multiple catch blocks in a try-catch block code. Here every catch block is kind of a processor to process that particular exception. So when an exception occurs in the try block, it’s sent to the first catch block to process. If the catch block is not able to process it, it forwards the request to the next Object in the chain (i.e., the next catch block). If even the last catch block is not able to process it, the exception is thrown outside of the chain to the calling program.

Note: Learn more about the Chain of Responsibility Pattern.

4. Observer Pattern

An observer design pattern is useful when you are interested in the state of an Object and want to get notified whenever there is any change. In the observer pattern, the Object that watches the state of another Object is called observer, and the Object that is being watched is called subject.

Java provides an built-in platform for implementing the observer pattern through the java.util.Observable class and java.util.Observer interface. However, it’s not widely used because the implementation is limited and most of the time we don’t want to end up extending a class solely for implementing the observer pattern as Java doesn’t provide multiple inheritances in classes. Java Message Service (JMS) uses the observer pattern along with the mediator pattern to allow applications to subscribe and publish data to other applications.

Note: Learn more about the Observer Pattern.

5. Strategy Pattern

Strategy pattern is used when we have multiple algorithms for a specific task, and the client decides the actual implementation be used at runtime. A strategy pattern is also known as a policy pattern. We define multiple algorithms and let client applications pass the algorithm to be used as a parameter.

One of the best examples of this pattern is the Collections.sort() method that takes the Comparator parameter. Based on the different implementations of comparator interfaces, the objects are getting sorted in different ways.

Note: Learn more about the Strategy Pattern.

6. Command Pattern

The command pattern is used to implement loose-coupling in a request-response model. In this pattern, the request is sent to the invoker and the invoker passes it to the encapsulated command object. The command object passes the request to the appropriate method of receiver to perform the specific action.

Note: Learn more about the Command Pattern.

7. State Pattern

The state design pattern is used when an Object changes its behavior based on its internal state. If we have to change the behavior of an Object based on its state, we can have a state variable in the Object and use if-else condition block to perform different actions based on the state. The state pattern is used to provide a systematic and loosely-coupled way to achieve this through context and state implementations.

Note: Learn more about the State Pattern.

8. Visitor Pattern

The visitor pattern is used when we have to perform an operation on a group of similar kinds of objects. With the help of a visitor pattern, we can move the operational logic from the objects to another class.

Note: Learn more about the Visitor Pattern.

9. Interpreter Pattern

The interpreter pattern is used to define a grammatical representation of a language and provides an interpreter to deal with this grammar.

10. Iterator Pattern

The iterator pattern is one of the behavioral patterns and is used to provide a standard way to traverse through a group of objects. The iterator pattern is widely used in Java Collection Framework where the iterator interface provides methods for traversing through a Collection. This pattern is also used to provide different kinds of iterators based on our requirements. The iterator pattern hides the actual implementation of traversal through the Collection and client programs use iterator methods.

Note: Learn more about the Iterator Pattern.

11. Memento Pattern

The memento design pattern is used when we want to save the state of an object so that we can restore it later on. This pattern is used to implement this in such a way that the saved state data of the object is not accessible outside of the Object, this protects the integrity of saved state data.

Memento pattern is implemented with two Objects – originator and caretaker. The originator is the Object whose state needs to be saved and restored, and it uses an inner class to save the state of Object. The inner class is called “Memento”, and it’s private so that it can’t be accessed from other objects.

Miscellaneous Design Patterns

There are a lot of design patterns that don’t come under Gang of Four design patterns. Let’s look at some of these popular design patterns.

1. DAO Design Pattern

The Data Access Object (DAO) design pattern is used to decouple the data persistence logic to a separate layer. DAO is a very popular pattern when we design systems to work with databases. The idea is to keep the service layer separate from the data access layer. This way we implement the separation of logic in our application.

Note: Learn more about the DAO Pattern.

2. Dependency Injection Pattern

The dependency injection pattern allows us to remove the hard-coded dependencies and make our application loosely-coupled, extendable, and maintainable. We can implement dependency injection in Java to move the dependency resolution from compile-time to runtime. Spring framework is built on the principle of dependency injection.

Note: Learn more about the Dependency Injection Pattern.

3. MVC Pattern

Model-View-Controller (MVC) Pattern is one of the oldest architectural patterns for creating web applications.

Conclusion

This article summarized Java design patterns.

You can check out Java design patterns example code from our GitHub Repository.

Continue your learning with more Java tutorials.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Category:
Tutorial

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 17, 2013

Hey your blog is really good. congratulation

- Nestor

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
September 23, 2015

how to print 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1

- Deepika Gurav

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
November 5, 2015

public class Pattern { public static void main(String[] args) { for(int i=1;i<6;i++){ for(int j=1;j<i;j++){ if((i+j)%2==0){ System.out.print(“0”); } else { System.out.print(“1”); } } System.out.println(); } } }

- jay

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
May 28, 2016

Thanks for the help buddy.

- Pankaj

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 11, 2017

    thanks

    - parth

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      March 2, 2017

      You can refer this code class demo { public static void main(String args[]) { int i,j,zero=0,one=1; for(i=1;i0;j–) { if(j==1 || j==3 || j==5) { System.out.print(one+ “t”); } else if(j==2 || j==4) { System.out.print(zero+ “t”); } } System.out.print(“n”); } } }

      - Ymg

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        August 27, 2013

        Hi Pankaj, Thanks a lot for the tutorial. Could you please also include J2EE Design Patterns also. (MVC, Business Delegates…)

        - Syam

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        August 27, 2013

        They are coming next, mostly in next month.

        - Pankaj

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          August 28, 2013

          Hi your blog is really good, I request you to post more on struts2 from basics to advance.

          - Naveen

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          May 28, 2016

          Struts2 tutorials are posted, i hope you find them useful.

          - Pankaj

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            January 22, 2014

            This tutorial was very helpful ; thanks Pankaj …waiting for J2EE Design Patterns tutorial

            - Varun

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            May 28, 2016

            I have added it on my TODO list, hope to start soon on them.

            - Pankaj

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              March 4, 2014

              One of the best article on Design pattern, Thanks.

              - Usha

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              May 28, 2016

              Thanks Usha for kind words.

              - Pankaj

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                March 5, 2014

                Excellent Explanation, Wonderful examples to understood easily… Thanks

                - Ravikumar

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                May 28, 2016

                You are welcome Ravikumar.

                - Pankaj

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  March 12, 2014

                  This is a really good blog, and I always follow your blog whenever I need any clearance.

                  - job@basware.com

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  May 28, 2016

                  Thanks for appreciation.

                  - Pankaj

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    April 9, 2014

                    Excellent site to know about all design patterns.

                    - SRK

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    May 28, 2016

                    I tried to cover as much as possible, to the best of my knowledge. :)

                    - Pankaj

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      April 20, 2014

                      Great work! Thanks.

                      - chandrani

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      May 28, 2016

                      You are welcome Chandrani.

                      - Pankaj

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        April 26, 2014

                        This is excellent work sir !!! really helpful

                        - Delli Babs

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        May 28, 2016

                        You are welcome Delli.

                        - Pankaj

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          April 29, 2014

                          great work…

                          - Pranay

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          May 28, 2016

                          Thanks Pranay.

                          - Pankaj

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            May 30, 2014

                            very useful. thanks a lot.

                            - jerry

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            May 28, 2016

                            Thanks Jerry, I hope Tom is not giving you a lot of trouble these days. :D

                            - Pankaj

                              JournalDev
                              DigitalOcean Employee
                              DigitalOcean Employee badge
                              June 17, 2014

                              This is excellent tutorial with best & simplified examples, compared to other tutorials on design pattern its easy to understand with great knowledge. The description given about each pattern helps you to decide which pattern should be used where. I read it like a story book.

                              - jitendra

                              JournalDev
                              DigitalOcean Employee
                              DigitalOcean Employee badge
                              May 28, 2016

                              Thanks Jitendra, you just made me smile. :)

                              - Pankaj

                                JournalDev
                                DigitalOcean Employee
                                DigitalOcean Employee badge
                                June 18, 2014

                                Thanks a lot for the instructive tutorial…

                                - Hossein Moradi

                                JournalDev
                                DigitalOcean Employee
                                DigitalOcean Employee badge
                                May 28, 2016

                                You are welcome brother.

                                - Pankaj

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  June 24, 2014

                                  Hello I’ve been struggling with design patterns, but this document explains them so well. Thank you for the lesson. Best regards, Cristian

                                  - Cristian Manoliu

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  May 28, 2016

                                  Hey Cristian, thanks for nice words. You can get my Design Pattern PDF eBook too by subscribing to my email newsletter.

                                  - Pankaj

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    July 14, 2014

                                    2 words Thank you!

                                    - Marquis

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    May 28, 2016

                                    2 words - “Appreciate it” :)

                                    - Pankaj

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      August 4, 2014

                                      Really good for refreshing the concepts. Thanks a lot :)

                                      - Rama

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      May 28, 2016

                                      Yeah I know, I also come by this once in a while to refresh my learning too. :)

                                      - Pankaj

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        August 20, 2014

                                        Some developers becomes so obsessed with design patterns that they over-engineer.

                                        - groovy

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        September 16, 2014

                                        But for most, it is a good learning.

                                        - vijay

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        May 28, 2016

                                        Design patterns are good to know, but don’t use it just because you know it. See your requirement and if there is a benefit in using design pattern, then only apply it. Simple code is the Best Code. :)

                                        - Pankaj

                                          JournalDev
                                          DigitalOcean Employee
                                          DigitalOcean Employee badge
                                          September 8, 2014

                                          Awesome tutorials Pankaj. Keep up the good work.

                                          - Chidambar Dorairaj

                                          JournalDev
                                          DigitalOcean Employee
                                          DigitalOcean Employee badge
                                          May 28, 2016

                                          Thanks Chidambar, appreciate nice words.

                                          - Pankaj

                                            JournalDev
                                            DigitalOcean Employee
                                            DigitalOcean Employee badge
                                            October 6, 2014

                                            This is also very nice simple explanation : public interface Duck { public void quack() ; public void fly() ; } public interface Turkey { public void gobble() ; public void fly() ; } public class TurkeyAdapter implements Duck { private Turkey mTurkey; public void quack() { // TODO Auto-generated method stub mTurkey.gobble() ; } public void fly() { // TODO Auto-generated method stub for(int i=0;i<5;i++) mTurkey.fly() ; } } TurkeyAdapter link between Duck and Turkey

                                            - Ashakant

                                            JournalDev
                                            DigitalOcean Employee
                                            DigitalOcean Employee badge
                                            May 28, 2016

                                            You need implementation of Turkey interface and then inject it to TurkeyAdapter private variable mTurkey by setter method or through constructor. My 2 cents, however I am not sure if there was a query here. ;)

                                            - Pankaj

                                              JournalDev
                                              DigitalOcean Employee
                                              DigitalOcean Employee badge
                                              October 15, 2014

                                              how to do 1 24 369 481216

                                              - jayesh

                                              JournalDev
                                              DigitalOcean Employee
                                              DigitalOcean Employee badge
                                              January 5, 2015

                                              I am not sure whether you solved it or not. But it is pretty simple. Below is the program for this pattern public static void main(String args[]) { int lines = 4; for (int i = 1; i <= lines; i++) { for (int j = 1; j <= i; j++) { System.out.print(i*j); } System.out.println(); } }

                                              - Vijay Bharwani

                                              JournalDev
                                              DigitalOcean Employee
                                              DigitalOcean Employee badge
                                              May 28, 2016

                                              i hope your program helped Jayesh.

                                              - Pankaj

                                                JournalDev
                                                DigitalOcean Employee
                                                DigitalOcean Employee badge
                                                October 22, 2014

                                                how to do this program 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

                                                - Tanmoy

                                                JournalDev
                                                DigitalOcean Employee
                                                DigitalOcean Employee badge
                                                December 22, 2014

                                                here is your program!!! class Pattern{ public static void main(String a[]){ int k=5; int i,j; int z; for( i=1;i<=15;i=z){ for( j=i;j<=k;j++){ System.out.print(j);} System.out.println(); k=(k+j)-i-1; z=j; } } }

                                                - rao arsalan

                                                JournalDev
                                                DigitalOcean Employee
                                                DigitalOcean Employee badge
                                                December 23, 2014

                                                cnt=1; for(i=n;i<0;n–) { for(j=0;j<i;j++) { System.out.print(cnt); } System.out.println(); cnt=cnt+1; }

                                                - Ganesh Ahiwale

                                                JournalDev
                                                DigitalOcean Employee
                                                DigitalOcean Employee badge
                                                May 28, 2016

                                                Thanks guys for pitching in and helping Tanmoy.

                                                - Pankaj

                                                  JournalDev
                                                  DigitalOcean Employee
                                                  DigitalOcean Employee badge
                                                  October 23, 2014

                                                  Simple explanations on common design patterns. Thanks!

                                                  - j

                                                  JournalDev
                                                  DigitalOcean Employee
                                                  DigitalOcean Employee badge
                                                  May 28, 2016

                                                  You are welcome ‘J’. :)

                                                  - Pankaj

                                                    JournalDev
                                                    DigitalOcean Employee
                                                    DigitalOcean Employee badge
                                                    October 28, 2014

                                                    It would be creat to see tutotials about DAO and MVC patterns and how to use them with JDBC.

                                                    - Oleg

                                                    JournalDev
                                                    DigitalOcean Employee
                                                    DigitalOcean Employee badge
                                                    May 28, 2016

                                                    Added to my TODO list, will publish it soon.

                                                    - Pankaj

                                                    JournalDev
                                                    DigitalOcean Employee
                                                    DigitalOcean Employee badge
                                                    September 28, 2016

                                                    still waiting for the same.

                                                    - abhinav

                                                      JournalDev
                                                      DigitalOcean Employee
                                                      DigitalOcean Employee badge
                                                      November 21, 2014

                                                      nice …well manged data…i read most of your blog and get confident in java…keep it up…

                                                      - Atmprakash Sharma

                                                      JournalDev
                                                      DigitalOcean Employee
                                                      DigitalOcean Employee badge
                                                      May 28, 2016

                                                      Thanks Atmprakash, good to hear from a regular read.

                                                      - Pankaj

                                                        JournalDev
                                                        DigitalOcean Employee
                                                        DigitalOcean Employee badge
                                                        January 8, 2015

                                                        In One For Loop public class Test { public static void main(String arg[]){ int c =1; int d =1; int till =15; for(int b=till;b>0;b–){ System.out.print(b+" “); if(d==c){ System.out.println(”"); c++; d=0; } d++; } } }

                                                        - Neeraj

                                                        JournalDev
                                                        DigitalOcean Employee
                                                        DigitalOcean Employee badge
                                                        January 8, 2015

                                                        What is your query here?

                                                        - Pankaj

                                                          JournalDev
                                                          DigitalOcean Employee
                                                          DigitalOcean Employee badge
                                                          January 12, 2015

                                                          is MVC pattern belongs to which category?

                                                          - sree

                                                          JournalDev
                                                          DigitalOcean Employee
                                                          DigitalOcean Employee badge
                                                          May 28, 2016

                                                          It belongs to Java EE patterns.

                                                          - Pankaj

                                                            JournalDev
                                                            DigitalOcean Employee
                                                            DigitalOcean Employee badge
                                                            January 12, 2015

                                                            Nice article, good examples, easy to follow. I am using it to prepare for Software Engineering exam which is in two days. Thank you.

                                                            - Aryan

                                                            JournalDev
                                                            DigitalOcean Employee
                                                            DigitalOcean Employee badge
                                                            May 28, 2016

                                                            I am replying late but I wish you did well.

                                                            - Pankaj

                                                              JournalDev
                                                              DigitalOcean Employee
                                                              DigitalOcean Employee badge
                                                              March 24, 2015

                                                              Great work. I’m a great believer that understanding the context where a Design Pattern is applicable is more important than the actual implementation details. This article gives a good overview. I think this video will also be a good guide. Video

                                                              - Ranga

                                                              JournalDev
                                                              DigitalOcean Employee
                                                              DigitalOcean Employee badge
                                                              May 28, 2016

                                                              Thanks for the nice words Ranga.

                                                              - Pankaj

                                                                JournalDev
                                                                DigitalOcean Employee
                                                                DigitalOcean Employee badge
                                                                April 30, 2015

                                                                Hi, Blog information is really nice. Please provide a “Scroll to Top” option in your website. Many thanks :)

                                                                - Himalaya

                                                                JournalDev
                                                                DigitalOcean Employee
                                                                DigitalOcean Employee badge
                                                                May 28, 2016

                                                                Hmm, nice suggestion. I want to do that but it will add some more resources and slow down page speed. I have kept bare minimum things to make sure page load fast for everyone. :)

                                                                - Pankaj

                                                                  JournalDev
                                                                  DigitalOcean Employee
                                                                  DigitalOcean Employee badge
                                                                  September 5, 2015

                                                                  Simply awsome. I llike your write ups and examples.

                                                                  - suresh atta

                                                                  JournalDev
                                                                  DigitalOcean Employee
                                                                  DigitalOcean Employee badge
                                                                  May 28, 2016

                                                                  Thanks Suresh, I hope you will like my eBook for design patterns too. It’s absolutely free for my email subscribers.

                                                                  - Pankaj

                                                                    JournalDev
                                                                    DigitalOcean Employee
                                                                    DigitalOcean Employee badge
                                                                    October 19, 2015

                                                                    Thank you so much…This is really helpful to learn.

                                                                    - Vidita Daga

                                                                    JournalDev
                                                                    DigitalOcean Employee
                                                                    DigitalOcean Employee badge
                                                                    May 28, 2016

                                                                    Appreciate your lovely words, Vidita. :)

                                                                    - Pankaj

                                                                      JournalDev
                                                                      DigitalOcean Employee
                                                                      DigitalOcean Employee badge
                                                                      December 28, 2015

                                                                      There should be some real time problem also as a example to complete this tutorial.

                                                                      - Chirag Sharma

                                                                      JournalDev
                                                                      DigitalOcean Employee
                                                                      DigitalOcean Employee badge
                                                                      May 28, 2016

                                                                      If I will provide real life problem, it will become too specific and too lengthy. Also there are many ways to solve a real life problem, it depends on your project specific situation. You should learn these design patterns and use them wisely. You should not use a design pattern just because you know it. Always remember, simple code is the best code.

                                                                      - Pankaj

                                                                        JournalDev
                                                                        DigitalOcean Employee
                                                                        DigitalOcean Employee badge
                                                                        January 15, 2016

                                                                        Very simple examples and nicely explained. It would not have been possible to learn design patterns without your book. Thanks a ton for your effort sir.

                                                                        - Kush

                                                                        JournalDev
                                                                        DigitalOcean Employee
                                                                        DigitalOcean Employee badge
                                                                        May 28, 2016

                                                                        Wow, one of the nicest comment I have got recently.

                                                                        - Pankaj

                                                                          JournalDev
                                                                          DigitalOcean Employee
                                                                          DigitalOcean Employee badge
                                                                          February 29, 2016

                                                                          plzz tell me how to do this NO Data Pattern 1 5 3 5 10 5 5 10 15

                                                                          - Santeepa

                                                                          JournalDev
                                                                          DigitalOcean Employee
                                                                          DigitalOcean Employee badge
                                                                          March 14, 2016

                                                                          public class Pattern { static int c = 1; static int p = 5; static int x = 2; public static void main(String[] args) { for (int i = 2; i < 5; i++) { for (int j = 0; j < x; j++) { if (j == 0) System.out.print©; else { System.out.print(p); p += 5; } System.out.print(‘\t’); } c += 2; System.out.print(‘\n’); x++; p = 5; } } }

                                                                          - Vishnu

                                                                          JournalDev
                                                                          DigitalOcean Employee
                                                                          DigitalOcean Employee badge
                                                                          May 28, 2016

                                                                          Thanks Vishnu for responding and quick solution, I hope it helps Santeepa.

                                                                          - Pankaj

                                                                            JournalDev
                                                                            DigitalOcean Employee
                                                                            DigitalOcean Employee badge
                                                                            April 20, 2017

                                                                            plz solve it anybody if user gives(3,2)as an input then the output should be 7 9 11 3 5 1 1 3 5 plz solve this question in and share thiis code.

                                                                            - khushi

                                                                              JournalDev
                                                                              DigitalOcean Employee
                                                                              DigitalOcean Employee badge
                                                                              March 17, 2016

                                                                              wow Super Pankaj ,very easy to understand

                                                                              - mahesh

                                                                              JournalDev
                                                                              DigitalOcean Employee
                                                                              DigitalOcean Employee badge
                                                                              May 28, 2016

                                                                              Thanks Mahesh, get the eBook too. :)

                                                                              - Pankaj

                                                                                JournalDev
                                                                                DigitalOcean Employee
                                                                                DigitalOcean Employee badge
                                                                                March 26, 2016

                                                                                1 3 5 7 9 3 5 7 9 1 5 7 9 1 3 7 9 1 3 5 9 1 3 5 7 help please!!!

                                                                                - shreya

                                                                                JournalDev
                                                                                DigitalOcean Employee
                                                                                DigitalOcean Employee badge
                                                                                May 23, 2016

                                                                                int[] arr = new int[]{1, 3, 5, 7, 9}; for (int i = 0; i < 5; i++) { for (int j = 0; j < arr.length; j++) { System.out.print(arr[j] % 10 + " “); arr[j] = arr[j] + 2; } System.out.println(”\n"); }

                                                                                - Suyash

                                                                                JournalDev
                                                                                DigitalOcean Employee
                                                                                DigitalOcean Employee badge
                                                                                May 28, 2016

                                                                                Thanks Suyash for responding and helping Shreya.

                                                                                - Pankaj

                                                                                  JournalDev
                                                                                  DigitalOcean Employee
                                                                                  DigitalOcean Employee badge
                                                                                  March 27, 2016

                                                                                  1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16

                                                                                  - ejakhan

                                                                                  JournalDev
                                                                                  DigitalOcean Employee
                                                                                  DigitalOcean Employee badge
                                                                                  May 28, 2016

                                                                                  I don’t understand what you are asking, and how it’s anywhere related to java design patterns.

                                                                                  - Pankaj

                                                                                    JournalDev
                                                                                    DigitalOcean Employee
                                                                                    DigitalOcean Employee badge
                                                                                    April 5, 2016

                                                                                    Help full

                                                                                    - Don

                                                                                    JournalDev
                                                                                    DigitalOcean Employee
                                                                                    DigitalOcean Employee badge
                                                                                    May 28, 2016

                                                                                    Thanks Don, appreciate the nice words.

                                                                                    - Pankaj

                                                                                      JournalDev
                                                                                      DigitalOcean Employee
                                                                                      DigitalOcean Employee badge
                                                                                      April 26, 2016

                                                                                      Guru, This is great, no words the way you wrote the article. Love it, Love it, Love it. Thanks

                                                                                      - Java Learner

                                                                                      JournalDev
                                                                                      DigitalOcean Employee
                                                                                      DigitalOcean Employee badge
                                                                                      May 28, 2016

                                                                                      You made my day brother.

                                                                                      - Pankaj

                                                                                        JournalDev
                                                                                        DigitalOcean Employee
                                                                                        DigitalOcean Employee badge
                                                                                        May 20, 2016

                                                                                        Awesome articles on Design patterns so far I have read.

                                                                                        - Vijay Kumar

                                                                                        JournalDev
                                                                                        DigitalOcean Employee
                                                                                        DigitalOcean Employee badge
                                                                                        May 28, 2016

                                                                                        Thanks Vijay, make sure to get the eBook by subscribing to the newsletter.

                                                                                        - Pankaj

                                                                                          JournalDev
                                                                                          DigitalOcean Employee
                                                                                          DigitalOcean Employee badge
                                                                                          June 10, 2016

                                                                                          Hi Sir, what about Service Locator patter? under which category will it come?

                                                                                          - Boovaragan

                                                                                            JournalDev
                                                                                            DigitalOcean Employee
                                                                                            DigitalOcean Employee badge
                                                                                            June 14, 2016

                                                                                            Hi Pankaj, Singleton Design Pattern hyperlink not working.

                                                                                            - chetan

                                                                                            JournalDev
                                                                                            DigitalOcean Employee
                                                                                            DigitalOcean Employee badge
                                                                                            June 14, 2016

                                                                                            Thanks Chetan for noticing and pointing out, I was doing some post update and mistakenly removed the link. I have corrected it. Thanks again.

                                                                                            - Pankaj

                                                                                              JournalDev
                                                                                              DigitalOcean Employee
                                                                                              DigitalOcean Employee badge
                                                                                              July 7, 2016

                                                                                              Pankaj, Write a program to print this series. 129876 245335 483728 092872 387763

                                                                                              - Atul

                                                                                              JournalDev
                                                                                              DigitalOcean Employee
                                                                                              DigitalOcean Employee badge
                                                                                              November 24, 2016

                                                                                              public class numbers () { system.out.println (129876 “+” 245335 “+” 483728 “+” 092872 “+” 387763); return null; }

                                                                                              - Hayawan

                                                                                                JournalDev
                                                                                                DigitalOcean Employee
                                                                                                DigitalOcean Employee badge
                                                                                                November 24, 2016

                                                                                                public class Numbers { public static void main(String[] args) { System.out.println(129876); System.out.println(245335); System.out.println(483728); System.out.println(992872); } }; this is the right way, i was rushing before, sorry.

                                                                                                - Hayawan

                                                                                                  JournalDev
                                                                                                  DigitalOcean Employee
                                                                                                  DigitalOcean Employee badge
                                                                                                  July 16, 2016

                                                                                                  Very Nice article and well written with very good examples

                                                                                                  - Vikram

                                                                                                    JournalDev
                                                                                                    DigitalOcean Employee
                                                                                                    DigitalOcean Employee badge
                                                                                                    July 18, 2016

                                                                                                    Brother, add examples or link it in another page. It would be quite helpful.

                                                                                                    - Prashanth

                                                                                                    JournalDev
                                                                                                    DigitalOcean Employee
                                                                                                    DigitalOcean Employee badge
                                                                                                    July 18, 2016

                                                                                                    Nice examples and easily understandable. Seems links are not working only for me.

                                                                                                    - Prashanth

                                                                                                      JournalDev
                                                                                                      DigitalOcean Employee
                                                                                                      DigitalOcean Employee badge
                                                                                                      August 9, 2016

                                                                                                      Very nice articles.

                                                                                                      - Padam Dhariwal

                                                                                                        JournalDev
                                                                                                        DigitalOcean Employee
                                                                                                        DigitalOcean Employee badge
                                                                                                        August 10, 2016

                                                                                                        how to print 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1 1

                                                                                                        - a

                                                                                                        JournalDev
                                                                                                        DigitalOcean Employee
                                                                                                        DigitalOcean Employee badge
                                                                                                        August 11, 2016

                                                                                                        public void printStars(int num, int limit) { if (num > limit) return; else { for (int q = 1; q <= num; q++){ System.out.print(q); } System.out.print(“\n”); printStars(num +1, limit); for (int q = 1; q <= num; q++){ System.out.print(q); } System.out.print(“\n”); } }

                                                                                                        - Lalit Sharma

                                                                                                          JournalDev
                                                                                                          DigitalOcean Employee
                                                                                                          DigitalOcean Employee badge
                                                                                                          January 18, 2017

                                                                                                          public void printStars(int num, int limit) { int count =0; if (num > limit) return; else { for (int q = 1; q 1;q–) { System.out.print(q-1); count++; } if(count==(limit-1)){ } else{ System.out.print(“\n”); } printStars(num +1, limit); for (int q = 1; q 1;q–) { if(count==(limit-1)){ } else{ System.out.print((q-1)); } } if(count==(limit-1)){ System.out.print(“\n”); } else{ System.out.print(“\n”); } } }

                                                                                                          - Veera

                                                                                                            JournalDev
                                                                                                            DigitalOcean Employee
                                                                                                            DigitalOcean Employee badge
                                                                                                            August 23, 2016

                                                                                                            Hi Pankaj, I just want to thank you. The work you are doing so great, it’s making developers life easier. Thank a lot for your great work again. One query i have that might be wrong but just to clarify: 1. In Decorator Pattern - We are using inheritance or aggregation to extend the behavior of an object… and not the “Composition”, I think Aggregation is correct to say here than Composition. what you say?

                                                                                                            - Balaji

                                                                                                              JournalDev
                                                                                                              DigitalOcean Employee
                                                                                                              DigitalOcean Employee badge
                                                                                                              August 28, 2016

                                                                                                              Great post… Thanks!

                                                                                                              - Ranga

                                                                                                                JournalDev
                                                                                                                DigitalOcean Employee
                                                                                                                DigitalOcean Employee badge
                                                                                                                September 7, 2016

                                                                                                                This blog post on design patterns is great! I particularly like your links that show examples. Very helpful. I use a lot of design patterns in my Java work. I come to your blog often, to clarify their differences. Thank you for posting it. Ranga

                                                                                                                - Ranga Kalyan

                                                                                                                  JournalDev
                                                                                                                  DigitalOcean Employee
                                                                                                                  DigitalOcean Employee badge
                                                                                                                  November 17, 2016

                                                                                                                  Hi i need the ebook java design patterns

                                                                                                                  - anjali

                                                                                                                    JournalDev
                                                                                                                    DigitalOcean Employee
                                                                                                                    DigitalOcean Employee badge
                                                                                                                    December 28, 2016

                                                                                                                    I need help.Can you help me in this: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

                                                                                                                    - tathagata bhattacharya

                                                                                                                      JournalDev
                                                                                                                      DigitalOcean Employee
                                                                                                                      DigitalOcean Employee badge
                                                                                                                      February 8, 2017

                                                                                                                      It’s good for me. Thank you for these.

                                                                                                                      - Luke Fan

                                                                                                                        JournalDev
                                                                                                                        DigitalOcean Employee
                                                                                                                        DigitalOcean Employee badge
                                                                                                                        February 10, 2017

                                                                                                                        Great blog

                                                                                                                        - ArsalImam

                                                                                                                          JournalDev
                                                                                                                          DigitalOcean Employee
                                                                                                                          DigitalOcean Employee badge
                                                                                                                          May 9, 2017

                                                                                                                          Slight typo on the section for Bridge Pattern it says " When we have interface hierarchies in both interfaces as well as implementations, then builder design pattern "… but we are talking about the Bridget Pattern and Structural Patterns

                                                                                                                          - Stefan

                                                                                                                            JournalDev
                                                                                                                            DigitalOcean Employee
                                                                                                                            DigitalOcean Employee badge
                                                                                                                            June 28, 2017

                                                                                                                            Hi Pankaj, Consider a situation where our management has decided to integrate a new jar instead of old one. The new jar provides the same set of operations which was used by old jar, but the number of arguments or the datatype of the argument ( header parameter of the function) is different from the existing. It will be very difficult to change the method parameters across the application. Which design pattern suits well in this case with minimum number of modification in the application? Thanks, Geethu

                                                                                                                            - Geethu

                                                                                                                              JournalDev
                                                                                                                              DigitalOcean Employee
                                                                                                                              DigitalOcean Employee badge
                                                                                                                              August 10, 2017

                                                                                                                              How to do this please is very important W I N D H O E K W I N D H O E W I N D H O W I N D H W I N D W I N W I W L L I L I N L I N G L I N G U L I N G U A

                                                                                                                              - Emanuel

                                                                                                                                JournalDev
                                                                                                                                DigitalOcean Employee
                                                                                                                                DigitalOcean Employee badge
                                                                                                                                November 9, 2017

                                                                                                                                This is by far the most comprehensive tutorial / guide I’ve come across.

                                                                                                                                - novapattern.com

                                                                                                                                  JournalDev
                                                                                                                                  DigitalOcean Employee
                                                                                                                                  DigitalOcean Employee badge
                                                                                                                                  November 13, 2017

                                                                                                                                  How do we adapt the version changes of common library like Apache in our existing code ? For example , if I am using method of some common library in multiple classes and method signature get changed in upgrade version ? what is the best solution not to make changes in all classes rather then in single point ? My guess : Creat a separate class and use the common method inside the class and use this class reference everywhere .

                                                                                                                                  - Uday

                                                                                                                                    Try DigitalOcean for free

                                                                                                                                    Click below to sign up and get $200 of credit to try our products over 60 days!

                                                                                                                                    Sign up

                                                                                                                                    Join the Tech Talk
                                                                                                                                    Success! Thank you! Please check your email for further details.

                                                                                                                                    Please complete your information!

                                                                                                                                    Become a contributor for community

                                                                                                                                    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                                                                                                                                    DigitalOcean Documentation

                                                                                                                                    Full documentation for every DigitalOcean product.

                                                                                                                                    Resources for startups and SMBs

                                                                                                                                    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                                                                                                                                    Get our newsletter

                                                                                                                                    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                                                                                                                                    New accounts only. By submitting your email you agree to our Privacy Policy

                                                                                                                                    The developer cloud

                                                                                                                                    Scale up as you grow — whether you're running one virtual machine or ten thousand.

                                                                                                                                    Get started for free

                                                                                                                                    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                                                                                                                                    *This promotional offer applies to new accounts only.