design patterns using java

Design Patterns using Java

Posted by

Introduction

In this post we are going to discuss Design patterns, Design patterns are introduced to solve the problems that come repeatedly while developing software using object-oriented design.

Design patterns are recurring solutions to common problems encountered in software design. They represent best practices evolved by experienced software developers.

Design patterns are part of object-oriented design.

Object-oriented programming is a way to develop complex systems. and a few concepts need to be learned while developing software using object-oriented programming.
These are prerequisites for design patterns.

Here is the list of topics to learn before learning design patterns.

  1. Class
  2. Object
  3. Inheritance
  4. Polymorphism
  5. Abstraction
  6. Encapsulation
  7. Aggregation
  8. Interfaces
  9. Composition
  10. Association

I will be adding a new article covering all these topics in a separate post.

Design patterns are mainly of three types.

  1. Creational Design Pattern
  2. Behavioral Design Pattern
  3. Structural Design Pattern

Each Design pattern offers a unique solution for different kinds of problems, they are not any inventions but the proven solutions to the problems faced by other developers. They provide high-level details of the solution implementation that can be changed as per our needs.

Creational Design Pattern

Deals with the mechanism of object creation. They provide a mechanism for class instantiation. Like how to create a new object which is used in the design of a new system. common patterns are.

  1. Singelton Design Pattern: Whenever we need only one instance of the class throughout the application, then we use the Singelton pattern to create the object of that class. Some examples could be Database connection and logger classes.
  2. Factory Method Pattern: Defines an interface for creating an object but leaves the choice of its type to the subclasses. Using this pattern we can create objects as per our choice. Where each component has multiple variants then we use this pattern.
  3. Abstract Factory Pattern: Provides an interface for creating families of related or dependent objects without specifying their concrete classes, This pattern is a kind of factory of abstract patterns, i.e. grouping of components.
  4. Builder Pattern: Allows the same construction process to create different representations i.e. using this pattern we can create objects as per our needs like the fields whenever we want an object with some other fields we can change them.
  5. Prototype Pattern: Creates new objects by copying an existing object, known as the prototype. Useful when the cost of creating an object is more expensive than copying an existing one.

Structural Design Pattern

Structural design patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.

Now let us discuss different types of Structural design patterns.

Adapter Pattern: An adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate. 

An example of this would be suppose your method is taking int format data and it is working fine but then you found a library which worked better with long format data. So you need a way to convert your int input to long input this is where the adapter comes into the picture.

Bridge Pattern: Separates abstraction from implementation so that both can vary independently. This pattern is useful when supposing for the Shape class two new subclasses being added Circle and Triangle and later Color class with two subclasses like Red and Green added. So now the number of subclasses will increase double every time.

To overcome this problem, just add another top class and connect them with a property that will work as a bridge.

Composite Pattern: Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects.

Decorator Pattern: A decorator is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.
 
Facade Pattern: Provides a unified interface to a set of interfaces in a subsystem.
 
Flyweight Pattern: Flyweight is a structural design pattern that lets you fit more objects into the available amount of RAM by sharing common parts of the state between multiple objects instead of keeping all of the data in each object.
 
Proxy Pattern: Proxy is a structural design pattern that lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.

Behavioural Design Pattern

Behavioral design patterns are concerned with the interaction and responsibility distribution among objects. They focus on how objects collaborate and communicate with each other, providing solutions for the delegation of responsibilities.

Chain of Responsibility Pattern: Passes a request along a chain of handlers. Each handler decides either to process the request or pass it to the next handler.

Event handling systems where multiple handlers process events sequentially

Command Pattern: Encapsulates a request as an object, allowing users to parameterize clients with queues, requests, and operations.

Remote control where each button has some task to do.

Interpreter Pattern: Defines the grammar for a language and provides an interpreter to interpret sentences in the language.

Useful for different language interpretation.

Iterator Pattern: Provides a way to access elements of an aggregate object sequentially without exposing its underlying representation. Like through a Java collection.

Mediator Pattern: Defines an object that centralizes communication between objects in a system. Like an air control system where all the planes communicate using an Air tower.

Momento Pattern: Captures and externalizes an object’s internal state so that the object can be restored to this state later. Like Undo functionality

Observer Pattern: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Implement an Event-handling system using this.

State Pattern: Allows an object to alter its behavior when its internal state changes.

Strategy Pattern: Defines a family of algorithms, encapsulates each one and makes them interchangeable

Like sorting algorithm family where the type of algorithm can be changed dynamically.

Template Method Pattern: Defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure. 

All methods are implemented in the main class but few methods are given to be implemented by child classes.

Visitor Pattern: Represents an operation to be performed on the elements of an object structure. 

So these all are the patterns which mainly decides the business logic of application and any event-driven large scale application will use these patterns.

Advantages of Using Design Patterns:

  1. Reusability:

    • Design patterns promote code reuse by providing proven solutions to common problems.
    • Developers can leverage existing patterns rather than reinventing solutions.
  2. Scalability:

    • As projects grow in complexity, design patterns help maintain a scalable and maintainable codebase.
    • Patterns provide a structured approach to handling complexity.
  3. Communication:

    • Design patterns serve as a common vocabulary for developers to communicate about software design.
    • They facilitate understanding and collaboration among team members.
  4. Flexibility:

    • Design patterns contribute to a flexible and adaptable design that can evolve with changing requirements.
    • Modifications can be made with minimal impact on the overall system.
  5. Performance:

    • Well-established design patterns often come with optimized implementations that contribute to better performance.
    • They have been refined over time to achieve efficiency.

In summary, design patterns offer a systematic and proven approach to solving software design challenges. They contribute to the creation of robust, maintainable, and scalable software systems. While design patterns are powerful tools, it’s essential to apply them judiciously, considering the specific requirements of each project.

I will add more details of a few of the important patterns which are generally used.

Leave a Reply

Your email address will not be published. Required fields are marked *