YouTip LogoYouTip

Design Pattern Intro

# Introduction to Design Patterns Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time. A design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code. It is a description or template for how to solve a problem that can be used in many different situations. Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system. The use of design patterns promotes code reuse, which leads to more robust and maintainable code. It helps in preventing minor issues that can cause major problems in the future. Design patterns provide a standard terminology and are specific to particular scenarios. For example, a singleton design pattern signifies use of a single object, so all developers familiar with singleton design pattern will make use of single object and they can tell each other that the program is following the singleton pattern. ## What is GOF (Gang of Four)? In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides published a book titled **Design Patterns - Elements of Reusable Object-Oriented Software**. This book introduced the concept of design patterns in software development. The four authors are collectively known as **Gang of Four (GOF)**. The design patterns they proposed are mainly based on the following object-oriented design principles. * Program to an interface, not an implementation. * Favor object composition over inheritance. ## Usage of Design Patterns Design patterns have two main usages in software development. ### Common Platform for Developers Design patterns provide a standard terminology and are specific to particular scenarios. For example, a singleton design pattern signifies use of a single object, so all developers familiar with singleton design pattern will make use of single object and they can tell each other that the program is following the singleton pattern. ### Best Practices Design patterns have been evolved over a long period of time and they provide best solutions to certain problems faced during software development. Learning these patterns helps less experienced developers to learn software design in an easy and accelerated way. ## Types of Design Patterns As per the design pattern reference book **Design Patterns - Elements of Reusable Object-Oriented Software**, there are 23 design patterns. These patterns can be categorized into three main categories: Creational, Structural, and Behavioral patterns. We'll also discuss another category of design pattern: J2EE design patterns. | No. | Pattern & Description | Includes | | --- | --- | --- | | 1 | **Creational Patterns** These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator. This gives program more flexibility in deciding which objects need to be created for a given use case. | * Factory Pattern * Abstract Factory Pattern * Singleton Pattern * Builder Pattern * Prototype Pattern | | 2 | **Structural Patterns** These patterns are concerned with class and object composition. They use inheritance and composition to build larger structures from smaller ones. | * Adapter Pattern * Bridge Pattern * Filter Pattern * Composite Pattern * Decorator Pattern * Facade Pattern * Flyweight Pattern * Proxy Pattern | | 3 | **Behavioral Patterns** These patterns are concerned with communication between objects. They focus on how objects interact and distribute responsibilities. | * Chain of Responsibility Pattern * Command Pattern * Interpreter Pattern * Iterator Pattern * Mediator Pattern * Memento Pattern * Observer Pattern * State Pattern * Null Object Pattern * Strategy Pattern * Template Pattern * Visitor Pattern | | 4 | **J2EE Patterns** These design patterns are specifically concerned with the presentation tier. These patterns are identified by Sun Java Center. | * MVC Pattern * Business Delegate Pattern * Composite Entity Pattern * Data Access Object Pattern * Front Controller Pattern * Intercepting Filter Pattern * Service Locator Pattern * Transfer Object Pattern | The following image shows the relationships between these design patterns: ![Image 2: Relationships between design patterns](#) ## Advantages of Design Patterns * Provides a shared design vocabulary and concepts, enabling developers to better communicate and understand each other's design intentions. * Offers proven solutions that can improve software maintainability, reusability, and flexibility. * Promotes code reuse, avoiding redundant design and implementation. * Following design patterns can reduce errors and issues in the system, improving code quality. ## Six Principles of Design Patterns **1. Open Close Principle (OCP)** The Open Close Principle means: **Open for extension, closed for modification**. When a program needs to be extended, you should not modify the existing code to achieve a hot-pluggable effect. In short, it is to make the program's extensibility good, easy to maintain and upgrade. To achieve this effect, we need to use interfaces and abstract classes, which we will mention in the specific design later. **2. Liskov Substitution Principle (LSP)** The Liskov Substitution Principle is one of the basic principles of object-oriented design. It states that objects of a superclass should be replaceable with objects of its subclasses without breaking the application. LSP is the cornerstone of inheritance reuse. Only when derived classes can replace base classes and the functionality of the software unit is not affected, can the base class be truly reused, and derived classes can add new behavior based on the base class. The Liskov Substitution Principle is a supplement to the Open Close Principle. The key step to implementing the Open Close Principle is abstraction, and the inheritance relationship between base and derived classes is the concrete implementation of abstraction. Therefore, the Liskov Substitution Principle is a specification for the concrete steps of implementing abstraction. **3. Dependence Inversion Principle (DIP)** This principle is the foundation of the Open Close Principle. The specific content is: program to an interface, depend on abstraction, not on concretions. **4. Interface Segregation Principle (ISP)** This principle means: it is better to have many client-specific interfaces than one general-purpose interface. It also means: reduce the coupling between classes. From this, we can see that design patterns are software design ideas that start from large-scale software architecture and are convenient for upgrading and maintenance. They emphasize reducing dependencies and reducing coupling. **5. Law of Demeter (LoD) / Principle of Least Knowledge** The Principle of Least Knowledge states: a unit of software should only talk to its immediate friends. This means that an entity should interact with as few other entities as possible, making the system's functional modules relatively independent. **6. Composite Reuse Principle (CRP)** The Composite Reuse Principle states: favor object composition over inheritance. Use composition/aggregation instead of inheritance whenever possible.
← Factory PatternDesign Pattern Tutorial β†’