0Pricing
Clean Architecture & Design Patterns in Practice · Aula

Categorização de padrões de design

Aprenda sobre as três categorias principais — padrões de criação, estruturais e comportamentais — e suas funções na arquitetura de software.

Categorização de padrões de design é uma aula grátis de Clean Architecture & Design Patterns in Practice no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Clean Architecture & Design Patterns in Practice, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Clean Architecture & Design Patterns in Practice inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

Organizing Design Patterns

Welcome back! In our last lesson, we learned what design patterns are. Now, let's explore how they are organized.

Just like books in a library, design patterns are grouped into categories. This helps us understand their purpose and when to use them effectively.

The Three Main Categories

Design patterns are traditionally divided into three main categories. Each category addresses a different type of problem in software design:

  • Creational Patterns: Deal with object creation.
  • Structural Patterns: Focus on object composition and relationships.
  • Behavioral Patterns: Concern object interaction and responsibilities.

Let's dive into each one!

Creational Patterns: Object Creation

Creational patterns are all about how objects are created. They aim to provide flexible and controlled ways to instantiate objects, rather than using direct constructors.

This helps make a system independent of how its objects are created, composed, and represented. They make your code more adaptable to change.

Creational Example: Simple Creator

Imagine you need to create different types of reports. A creational pattern might use a method to decide which report type to build, instead of you calling `new` directly.

Try running this simple example:

public class Main {
  public static String createReport(String type) {
    if ("PDF".equals(type)) {
      return "Generating PDF Report...";
    } else if ("CSV".equals(type)) {
      return "Generating CSV Report...";
    }
    return "Unknown Report Type.";
  }

  public static void main(String[] args) {
    System.out.println(createReport("PDF"));
    System.out.println(createReport("CSV"));
  }
}

Structural Patterns: Composition

Structural patterns deal with the composition of classes and objects. They help you assemble objects and classes into larger, more flexible structures.

These patterns focus on how objects are related to each other, often by identifying simple ways to realize relationships between entities.

Structural Example: Object Assembly

Structural patterns are like building blocks. They show how to combine objects to form new functionalities. Think of a computer having a CPU and RAM.

Run this code to see a simple composition:

public class Main {
  static class CPU {
    String process() { return "CPU processing..."; }
  }

  static class Computer {
    private CPU cpu; // Computer HAS-A CPU (composition)

    public Computer() {
      this.cpu = new CPU();
    }

    public String start() {
      return cpu.process() + " Computer booting up.";
    }
  }

  public static void main(String[] args) {
    Computer myPC = new Computer();
    System.out.println(myPC.start());
  }
}

Behavioral Patterns: Object Interaction

Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects. They describe how objects communicate and interact with each other.

These patterns help ensure that objects can work together efficiently and flexibly, managing complex processes and interactions.

Behavioral Example: Simple Action

Behavioral patterns often define how an action is performed or how objects respond to requests. Here's a simple example of a light switching on and off:

Run the code to see the light's behavior:

public class Main {
  static class Light {
    private boolean isOn = false;

    public String toggle() {
      isOn = !isOn;
      if (isOn) {
        return "Light is now ON.";
      } else {
        return "Light is now OFF.";
      }
    }
  }

  public static void main(String[] args) {
    Light livingRoomLight = new Light();
    System.out.println(livingRoomLight.toggle()); // First toggle
    System.out.println(livingRoomLight.toggle()); // Second toggle
  }
}

Why Categorize Patterns?

Categorizing design patterns helps you in several ways:

  • Easier Search: If you know you need to solve an object creation problem, you can focus on Creational patterns.
  • Better Understanding: It clarifies the primary purpose and scope of a pattern.
  • Improved Communication: Teams can discuss patterns using a common language based on their categories.

It's a powerful tool for navigating the world of design patterns!

Categorization Check

Which category of design patterns primarily focuses on how objects are put together to form larger structures?

Recap: The Pattern Categories

Great job! In this lesson, we explored the three main categories of design patterns:

  • Creational: For flexible object creation.
  • Structural: For building larger, more flexible object compositions.
  • Behavioral: For managing object interactions and responsibilities.

Understanding these categories is your first step to mastering design patterns. Next, we'll see how these patterns appear in everyday coding!

Perguntas Frequentes

A aula “Categorização de padrões de design” é grátis?

Sim — o texto completo de “Categorização de padrões de design” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Clean Architecture & Design Patterns in Practice, atualize para CoddyKit PRO. O curso de Clean Architecture & Design Patterns in Practice inclui 4 aulas no total.

O que vou aprender em “Categorização de padrões de design”?

Aprenda sobre as três categorias principais — padrões de criação, estruturais e comportamentais — e suas funções na arquitetura de software. Você pratica Clean Architecture & Design Patterns in Practice com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Clean Architecture & Design Patterns in Practice?

Nenhuma experiência prévia é necessária. Clean Architecture & Design Patterns in Practice no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “Categorização de padrões de design”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Clean Architecture & Design Patterns in Practice?

Sim. Cada aula de Clean Architecture & Design Patterns in Practice inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. O que são padrões de design?
  2. Categorização de padrões de design
  3. Padrões na programação cotidiana
  4. Antipadrões e o custo do uso indevido de padrões
← Voltar para Clean Architecture & Design Patterns in Practice