What is Design Pattern?

Asked

Viewed 5,756 times

33

I am starting my studies in Software Engineering, I have heard a lot about the term Design Patterns and its applicability and importance in software projects. Below is a definition:

In software engineering, a design pattern or design pattern (from English design Pattern) is a reusable general solution for a problem that occurs frequently within a given context in the software project.

Source: Wikipedia

I couldn’t visualize what they would be Design Patterns. But after all, what is it? Could anyone clarify what we should take into consideration in order to apply these concepts in a software project? Maybe this will improve my understanding.

  • 1

    See if that helps you understand, I gave a read and found it very enlightening.

  • Design Pattern is when you use Singleton (lol).

  • 1

    Did any help you more? You need something to be improved?

  • I’d even create a separate answer, but the other answers pretty much speak volumes. I’ll show you this site to check it out: https://refactoring.guru/design-patterns . In addition, I recommend taking a look at the courses of Alura (I’m not doing advertising, I just did them and found great) or others on the subject, to have a clearer idea about the theme and where to apply it in practice.

5 answers

33

Normally we see the design pattern as a cake recipe. It is a model to be followed. It’s a way to encode something to get a certain result, a certain solution. Usually these recipes were created by those who have had the problem, thought a lot about it and found a solution that can be universal.

Recipes focus more on very specific problems and probably form idiomatic. Dps are more conceptual and focus on the problem, more than on the implementation.

Design patterns are ideas, not tools, technologies, products, methodologies, paradigms, etc.

There are some famous patterns that are very common in many applications, but nothing prevents you from setting your own standards for specific applications if it can repeat in your work.

Some of these patterns are documented and examples are provided. Some may be more automated and a library or framework can be made available to facilitate its implementation and use.

Others are so common that they can be inserted within the programming languages to facilitate even more.

Patterns that have gained fame

When people think about Pattern design it is common to think of the main ones, defined by the Gang of Four group. They also think of patterns like MVC (though more of an architecture standard) and similar ones, but forget that everything is standard design.

Patterns are all over the place

High-level programming languages are all based on design standards that were used when programming in Assembly in the early days of computing. They made some code patterns often so it would be better to abstract that.

For example, a variable is a design pattern to access a value in a given memory address. You won’t read this in any list of patterns, after all it has become so common, so transparent, that no one else thinks of it as a pattern.

Language flow control constructs are Patterns design deciding and deviating the order of execution from the instructions.

Object-oriented programming is a paradigm composed of a set of design patterns. So up to can program OO without a proper language for this.

Inheritance is a form of code reuse that takes advantage of a standard set in another class.

When you use a dictionary you are using an access pattern to a key and value data structure. When you make a quicksort, is using a pattern.

Using right

It is a mistake to reduce the standards to these known ones, and even more, to try to adapt every code to a pattern. There are those who program "pattern-oriented," and usually commit atrocities in the code because of this. In fact the Gof came to disseminate the term, but also to misrepresent what it really is. Do not get carried away by the use of fashion. This limits his real usefulness.

You have to know when to adopt it, when to adapt it, and when to ignore it. Of course, if you don’t know all the best known ones you run the risk of reinventing the wheel in a worse way, but trying to use a pattern where it doesn’t fit, also brings disastrous results. No need to keep looking for pattern at all.

When you think of your application as always creating a new pattern it can be easier to find out if there is one that can already be applying, or it can make it easier to understand what you are doing. Reusable solutions are usually clearer.

Formalized standards have a meaningful name, explanation, context, and example of implementation.

Besides Gof, there are other collections of patterns: A Pattern Language, Portland, Coplien, POSA and EAA, only to name the best known.

Related.

22

Design Pattern is the term used to describe a solution to a problem that occurs frequently and also a way to summarize the idea of a code implementation, rather than describing the code you speak only, I’m using a Strategy. They are also applied to address some deficiencies of the language used.

A project standard has several sessions that are important to know whether or not it should be adopted as

Setting: Where the solution applies and why.

Perks: Because this approach gracefully solves the problem.

Disadvantages: In which cases the solution does not apply or ends up making your project more complex, as usually several new classes added to the project, which work together to solve the problem.

Diagram of class/organisation: Shows the class structure of the pattern and how they relate.

Implementation: It is the code itself, the implementation can vary quite a lot from one language to another, remember the default is just an idea how to solve the problem and not a silver bullet.

12

Design Standards

Although specific, corporate systems have several similar characteristics. Consequently, many problems are repeated in different contexts.

Suppose that a given problem will occur in two hundred different systems. In each system, this problem can be solved differently. So, globally, we would have two hundred solutions to the same problem. Probably, some solutions would be better than others or even one of them better than all the others.

Hence the concept of design pattern or Pattern design. A design standard is a consolidated solution to a recurring problem in object-oriented software development and maintenance.

The most important reference related to design patterns is the book Design Patterns: Elements of Reusable Object-Oriented Software (1995) by authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides. These four authors are known as "Gang of Four" (Gof). The UML diagrams presented in this booklet are based on the diagrams in this book.

Standards Gof

The patterns set in the book Design Patterns: Elements of Reusable Object-Oriented Software sane Gof standards. They are classified into three categories: Gof standards creating, structural and behavioral.

Source:
K51 - Design Patterns in Java

Recommended reading
Source Making - Design Patterns
Webpro - Programming Principles

9

The problem of explaining what they are Design Patterns is that the explanation tends to be abstract, because the concept itself is somewhat abstract, and therefore for those who are beginners it becomes difficult to understand.

A more concrete way of explaining it is like this: a Design Pattern is a certain combination of classes/interfaces that together solve a type of problem that is common to happen in software development. These classes/interfaces may need adaptation to the problem itself.

Examples:

  • The Pattern Observer, whose class diagram is thus, solves the problem of notifying other classes that an event has happened.
  • The Pattern Iterator, that has this class diagram, solves the problem of traversing the elements stored in an object container (a list, set, dictionary, etc.) in a way that does not depend on how the container is implemented.
  • The Pattern Singleton prevents a class from being instantiated more than once.

These solutions in general are elegant and offer good maintainability, i.e., make the software well prepared to accept changes at that point in the system where the Pattern was applied.

It has become interesting to catalog these combinations so that people know and, if they run into a problem of the same kind, use the Pattern instead of reinventing the wheel with solutions that can often have low maintainability.

However, care must be taken to choose the Pattern right to solve the problem, this if the best solution is a Pattern.

When a person finds a situation and thinks "maybe Pattern X applies here", she needs to carefully assess whether the Pattern is actually applicable to that situation, using your knowledge of the Pattern (scope, intention, etc.) and seeking to compare the problem being faced with the type of problem that the Pattern proposes to resolve.

A Pattern misapplied can unnecessarily increase software complexity.

5

A Pattern design is a general solution to solve a problem that occurs frequently in a software development/design context.

The biggest reference I know about Pattern design is the book Design Patterns: Elements of Reusable Object-Oriented Software. The four authors are known as Gang of Four (The gang of four - in free translation), because the book is a catalog of the 23 best known project patterns.


Standards can be classified into 3:

  • Creation Patterns - They abstract the instantiation process, make a system independent of the way objects are created, composed and represented
  • Structural Patterns. Are those that deal with the composition of classes (or objects) to form large structures in the system;
  • Behavioral Patterns. They characterize the way classes (or objects) interact and distribute responsibility;

What are the existing standards?

  • Authored by: Abstract Factory, Builder, Factory Method, Prototype and Singleton;

  • Structural Patterns: Adapter, Bridge, Composite, Decorator, Facade, Flyweight and Proxy;

  • Behavior Patterns: Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method and Visitor

later I will complete this reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.