Design your code with design patterns

Hasan Al Mamun
4 min readJan 23, 2019

As an Android developer my primary language is Java, a beauty. A language that gives to give me 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 good for defining abstract data types.

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

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

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

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

· It implements real life scenario.

As a fan of Object Oriented Programming, we may feel that our code contains every one of the advantages given by the Object Oriented language. The code we have composed is sufficiently adaptable that we can roll out any improvements to it with less or any agony. Our code is re-usable so that we can re-use 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 piece 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 allow our code to be flexible, maintainable and re-usable.

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 which allow your code to be flexible, maintainable and re-usable.

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

Before, I starting to learn something I always ask myself 3 question, “What? Why? How?” In this write-up I try to breakdown everything in those 3 question. Hope the reader already come to learn about what is design patterns. Let’s dig down the other 2 questions to find the answer.

Why to Use Design Patterns?

Above, I mention the flexible, maintainable and re-usable 2 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 which makes 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 highly coupled 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 which have been successfully applied to problems. By learning these patterns and the related problem, an inexperienced developer learns 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 to how to choose one design pattern for your problem. But before that let’s learn one more thing that design patterns can be categorize 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 patterns. There could be plenty of design problems and solutions for them, but, to choose the pattern which fits exactly is depends on your knowledge and understanding about 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 classes and it represents a single thing, for that that you can use Singleton patterns for the design that creates a single instance. It also helps to decrease memory size.

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

The most important thing is you are the one who are going to design the code that helps other 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, “With great power comes great responsibility”.

--

--

Hasan Al Mamun

A Software Engineer by profession. Android enthusiastic, love to solve the problem.