DEV Community

loading...
Cover image for Common Design Patterns in Android

Common Design Patterns in Android

foroogh varmazyar
Android developer
Updated on ・2 min read

A design pattern provides a general reusable solution for the common problems that occur in software design. The pattern typically shows relationships and interactions between classes or objects. The idea is to speed up the development process by providing well tested, proven development/design paradigms. Design patterns are programming language independent strategies for solving a common problem. That means a design pattern represents an idea, not a particular implementation. By using design patterns, you can make your code more flexible, reusable, and maintainable.

Types of Design Patterns

There are mainly three types of design patterns:

  • Creational patterns: How you create objects.
  • Structural patterns: How you compose objects.
  • Behavioral patterns: How you coordinate object interactions.

It’s not mandatory to always implement design patterns in your project. Design patterns are not meant for project development. Design patterns are meant for common problem-solving. Whenever there is a need, you have to implement a suitable pattern to avoid such problems in the future. To find out which pattern to use, you just have to try to understand the design patterns and their purposes. Only by doing that, you will be able to pick the right one.

Creational Patterns :

These design patterns are all about class instantiation or object creation. These patterns can be further categorized into Class-creational patterns and object-creational patterns.

Common Creational design patterns in Android are:
* Builder : Separates object construction from its representation
* Singleton : A class of which only a single instance can exist
* Factory Method : Creates an instance of several derived classes

Structural Patterns :

These design patterns are about organizing different classes and objects to form larger structures and provide new functionality

Common Structural design patterns in Android are:

  • Adapter : Match interfaces of different classes
  • Facade : A single class that represents an entire subsystem
  • Decorator : Add responsibilities to objects dynamically
  • Composite : A tree structure of simple and composite objects

Behavioral Patterns :

Behavioral patterns are about identifying common communication patterns between objects and realizing these patterns.

Common Behavioral patterns in Android are :

  • Command : Encapsulate a command request as an object
  • Observer : A way of notifying change to a number of classes
  • Strategy : Encapsulates an algorithm inside a class
  • State : Alter an object's behavior when its state changes

Discussion (0)