What are Design Patterns

What are Design Patterns

In this tutorial, we are going to discuss about the What are Design Patterns. Before discussing What are Design Patterns lets discuss about what are anti patterns.

Anti Patterns

Anti patterns are certain patterns in software development that is considered a bad programming practice. God Object is example of Anti-Pattern. Here God Object means all operations are performed only one object. For example,

public class GodObject {
   function PerformInitialization () {}
   function ReadFromFile () {}
   function WriteToFile () {}
   function DisplayToScreen () {}
   function PerformCalculation () {}
   function ValidateInput () {}
   // and so on... //
}

Here the object performed all operations. The basic idea behind object-oriented programming is that a big problem is separated into several smaller problems (a divide and conquer strategy) and solutions are created for each of them. Once the small problems have been solved, the big problem as a whole has been solved. Therefore an object need only know everything about itself.

The opposed patterns of Anti patterns are Designed patterns.

Design Patterns

Design patterns are typical solutions to common problems in software design and architecture. Consider yourself building a house and constantly facing the same problem, such as where to place a door to ensure proper house flow. A design pattern is similar to a manual that recommends door placement based on what has been successful in many previous homes.

Design Patterns represent best practices, evolved over time by experienced software developers, to address recurring design challenges. They provide a standardized and efficient approach to software development, ensuring code is more understandable, flexible, and maintainable. They are not ready-made bits of code that you can just insert into your application but rather guidelines or templates that you can customize to meet your individual requirements.

In short, design patterns are essentially the combined knowledge of numerous developers condensed into a collection of useful templates that help create cleaner, more effective, and more comprehensible code.

We can device above program using design patterns as following.

public class FileInputOutput {
   function ReadFromFile () {}
   function WriteToFile () {}
}

public class UserInputOutput {
   function DisplayToScreen () {}
   function ValidateInput () {}
}

public class Logic {
   function PerformInitialization () {}
   function PerformCalculation () {}
}

My main intention is due to the problems faced in designing the software applications. Most of the time what happen is we design the software we implement them and by the time we ready to deliver the product to the customer. When that time client changes the requirements and due to this change of requirement then we have to do lots of changes in the code before deliver it to the customer. Due to this we face many problems as for as the implementation goes. In order to prevent these problems we use design patterns.

Why Use Design Patterns?

Here are the top reasons why we should use design patterns:

  1. Solve Common Design Problems: Design patterns provide solutions for common design challenges. They represent proven strategies for solving specific problems that have been developed and refined over time.
  2. Avoid Reinventing the Wheel: Instead of finding a new solution to a recurrent problem, developers can use a design pattern that encapsulates a best practice. This saves time and effort.
  3. Improve Code Readability and Maintainability: Design patterns are well-documented and understood by experienced developers. Using them makes code more standardized and easier to understand, as they provide a common language of sorts.
  4. Facilitate Communication: They offer a shared lexicon for developers. When a developer says they are using a Singleton or a Factory, others can quickly understand the general design of that section of the code.
  5. Promote Best Practices: By encapsulating good design principles, patterns promote the production of high-quality, testable, and maintainable code.
  6. Adaptability and Scalability: Many patterns are about structuring code in a way that makes it more adaptable to change and scalable for future requirements.
  7. Enhance Efficiency: They can speed up the development process by providing tested, proven development paradigms.
Types of Design Patterns

Design patterns are typical solutions to common problems in software design and architecture. They represent best practices, evolved over time by experienced software developers, to address recurring design challenges. Design patterns provide a standardized and efficient approach to software development, ensuring code is more understandable, flexible, and maintainable.

What are Design Patterns

Design patterns can be broadly classified into three categories, each serving different aspects of software design and development

Creational Patterns

Creational design patterns are concerned with the way of creating objects. These design patterns are used when a decision must be made at the time of instantiation of a class (i.e. creating an object of a class). But everyone knows an object is created by using new keyword in java. For example:

Student s1 = new Student (); 

Hard-Coded code is not the good programming approach. Here, we are creating the instance by using the new keyword. Sometimes, the nature of the object must be changed according to the nature of the program. In such cases, we must get the help of creational design patterns to provide more general and flexible approach. Examples include:

  • Singleton
  • Factory Method
  • Abstract Factory
  • Builder
  • Prototype

Structural Patterns

Structural patterns provide different ways to create a class structure, for example using inheritance and composition to create a large object from small objects. Structural design patterns are concerned with how classes and objects can be composed, to form larger structures. The structural design patterns simplifies the structure by identifying the relationships. Examples include:

  • Adapter (or Wrapper)
  • Composite
  • Proxy
  • Flyweight
  • Facade
  • Bridge
  • Decorator

Behavioral Patterns

Behavioral design patterns are concerned with the interaction and responsibility of objects. In these design patterns,the interaction between the objects should be in such a way that they can easily talk to each other and still should be loosely coupled. That means the implementation and the client should be loosely coupled in order to avoid hard coding and dependencies. Examples include:

  • Observer
  • Strategy
  • Command
  • Iterator
  • State
  • Visitor
  • Mediator
  • Memento
  • Chain of Responsibility
  • Template Method
Summary

Design patterns are essential for crafting elegant and efficient software. They provide standard approaches to solving known problems, resulting in code that is easier to understand and maintain. However, it’s also important to remember that they should be used judiciously – not every problem needs a pattern, and using a pattern where it’s not needed can lead to unnecessarily complex code.

That’s all about What are Design Patterns and types of Design Patterns. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Design patterns.!!

What are Design Patterns
Scroll to top