Design your code with design patterns

As an Android developer, my primary language is Java, a beauty. A language that gives to give me the power to do everything. And the most beautiful part of it is OOP (Object Oriented Programming). The advantages of OOP are mentioned below:

· OOP provides a clear modular structure for programs.

· It is suitable for defining abstract data types.

· Implementation details are hidden from other modules, and other modules have a clearly defined interface.

· It is easy to maintain and modify existing code as new objects can be created with slight differences from existing ones.

· Objects, methods, instance, message passing, and inheritance are some essential properties provided by these particular languages

· Encapsulation, Polymorphism, abstraction are also counting in these fundamentals of programming language.

· It implements real-life scenarios.

As a fan of Object-Oriented Programming, we may feel that our code contains every one of the advantages of the Object-Oriented language. The code we have composed is sufficiently adaptable to roll out any improvements to it with less or any agony. Our code is reusable so that we can reuse it with no inconvenience. We can keep up our code effectively, and any progressions to a piece of the code will not influence some other part of the code.

As uncle Ben said, “With great power comes great responsibility.” The quoted facts are also applicable in the field of Object-Oriented Programming. As a developer or programmer, it is our responsibility to design the codes in such a way that allows our code to be flexible, maintainable, and reusable.

Design is an art. Assume you as a designer design a UI (User Interface), you have to keep in mind many things like the UX (User Experience), color, components size, visibility, and many more. Also, when your role as a backend developer, it’s your sole responsibility to design the code in such a way that allows your code to be flexible, maintainable, and reusable.

Design is an art, and it comes with experience. Design patterns are the experience in designing object-oriented code. Design Patterns are the general reusable solution to commonly occurring problems. These are the best practices used by experienced developers. Patterns are not complete code, but they can use as a template that can apply to a problem.

Before I start to learn something, I always ask myself three questions, “What? Why? How?” In this write-up, I try to break down everything in those three questions. Let’s dig down the other two questions to find the answer. I hope the reader has already come to learn about what is design patterns. two

Why Use Design Patterns?

Above, I mention the flexible, maintainable, and reusable two or more times. Let’s learn more about that-

Flexibility: Using design patterns, your code becomes flexible. It helps to provide the correct level of abstraction due to which objects become loosely coupled to each other, making your code easy to change.

Reusability: Loosely coupled and cohesive objects and classes can make your code more reusable. This kind of code becomes easy to be tested as compared to the high associated code.

Shared Vocabulary: Shared vocabulary makes it easy to share your code and thought with other team members. It creates more understanding between the team members related to the code.

Capture best practices: Design patterns capture solutions that have been successfully applied to problems. By learning these patterns and the related issue, an inexperienced developer knows a lot about software design. Design patterns make it easier to reuse successful designs and architectures.

How to Choose One?

So the third part of the puzzle is how to choose one design pattern for your problem. But before that, let’s learn one more thing that design patterns can be categorized in the following category-

1. Creational Pattern.

2. Structural Pattern.

3. Behavior Pattern.

A design pattern can be used to solve more than one design problem, and one design problem can be solved by more than one design pattern. There could be plenty of design problems and solutions for them but choosing the pattern that fits precisely depends on your knowledge and understanding of the design patterns. It also depends on the code you already have in place. Some examples are shown below-

1. There are too many instances of a class, and it represents a single thing that you can use Singleton patterns for the design that creates a single sample. It also helps to decrease memory size.

2. Classes are dependent on other classes. So, a change in style can affect the other dependent courses. You can design Bridge, Mediator, or Command to solve this design problem.

The most important thing is you are the one who is going to design the code that helps another developer to develop or understand it more and work on it. So it’s your call what kind of patterns do you want to use today to solve what kind of problem you are facing. All you have to keep in mind is, “With great power comes great responsibility.”

Exit mobile version