한국어 | English | 日本語
Senior Web Application Developer (8.8+ years)
Tech & Dev
engineering
Focusing on web frontend and backend development

1. What is a 'Design Pattern'?

Overview of Design Patterns
I remember learning about design patterns briefly in university. While they were always a topic for graduate school or job interviews, I had little practical experience, so I never truly felt their importance. Perhaps it's different in today's project-heavy, theory-light bootcamp era, but I only empirically understood *why* patterns are used after joining a company.
Before diving into various design patterns, let's explore why they are necessary and briefly learn about class diagrams, which will aid in understanding them.

Design Patterns

In short, design patterns are the ‘result of contemplating what constitutes better code within the object-oriented paradigm.’ This concept first emerged through the book Design Patterns written by four programmers known as the Gang of Four. It encompasses development approaches that can be applied across various languages, not just object-oriented programming.

Before exploring various patterns, let’s look at three fundamental principles that cut across all patterns and explain why they came into existence:

Three Categories of Design Patterns

Design patterns are categorized into these three types, each containing a wide variety of specific patterns. I highly recommend referring to the Refactoring Guru page. There, you can see which patterns exist in each category, why they came about, and what concepts they embody, complete with example code, which will be a great help in your future learning.

Design Patterns: Too Far for Juniors

It’s understandable if you haven’t grasped design patterns without having worked on large projects.

I’ve never created a large project where a single line of code change would lead to dramatic changes elsewhere.

Even when I created an occupancy indicator for a university team project, which received AP information from users’ phones and relayed their locations to a Raspberry Pi, there was no complexity across its two servers to warrant applying or even considering patterns. Students might relate: after 60% planning and construction, and 20% development, the remaining 20% is often dedicated to less academic pursuits.

Moreover, team project code is rarely revisited. No professor asks for requirement changes, nor is there a need to reuse it for other projects.

‘Design patterns’ are a recurring topic when preparing for interviews, but ultimately, you must experience them directly by developing projects with code. The design pattern articles I’ll be compiling on this blog will focus on understanding, aiming to be concise for easy review. While I’ll strive to explain each pattern clearly in text, an understanding of class diagrams is essential for a more implementation-oriented grasp.

Class Diagrams - The Arrows

As the name implies, a class diagram is a simplified representation of relationships between Classes. It can be seen as a blueprint for project implementation in Object-Oriented Programming (OOP), where the concept of Class exists. To understand class diagrams, you only need to focus on three core arrows for explaining the relationships between Classes and Interfaces: Implements, Extends, and Composition. Code examples will be based on Java-like pseudo-code.

Implements

Implements

If you’ve read the previous article on Object-Oriented Programming, you’ll already know that OOP code is structured around Interfaces rather than Classes. While the Interface specifies ‘what task to perform,’ the desired Concrete Class is injected appropriately to actually perform ‘how to perform the task.’

When first learning about Classes, you might define and use them like this:

ConcreteClass clazz = new ConcreteClass();

Then, after learning about Interfaces and polymorphism, you can assign a Concrete Class to an Interface:

Interface clazz = new ConcreteClass();

In practice, for flexibility, the specific concrete class is not immediately specified. Instead, the desired concrete class is dynamically injected into the Interface from an external source. Because a concrete class is injected to perform a task, this is called Dependency Injection, or sometimes Inversion of Control because the code itself doesn’t hold the ‘implementation decision-making power.’ This will be covered in depth separately in a Spring article.

private Interface clazz;
public void setClass(Interface clazz) {
    this.clazz = clazz;
}
setClass(new ConcreteClass();

Extends

Extends

Inheritance is used to extend certain functions of a Class with additional features or to replace them with different logic. Back in my day, inheritance was taught as ‘parent class’ Animal with ‘child classes’ like Cat and Dog. However, this kind of superior-subordinate concept is better suited for Implements, and inheritance should only be used for simple functional/definition extension. In other words, we learned it wrong.

When defining a striped Cat as StripeCat, all functions and variables except for the stripe information are identical to Cat.

Composite (Composition)

Has-A

Inheritance is not the only way to extend Class functionality/definitions; composition is another.

Why does Object-Oriented Programming advocate using composition over inheritance? To review once more:

If we add an interface (implements) to the composition example above, it can express the Cat species much better:

Has-A and Implements

It gains extensibility to handle ‘striped species,’ ‘black species,’ etc., under the higher concept of ‘species.’ Comparing this to the previous inheritance (extends) example, the Sprite information that was held by the child class SpriteCatClass has now moved outside the Cat class to SpriteClass. This is where you can understand why it’s called Inversion of Control.

Openness, when we use Has-A and Implements

Let’s assume a Cat can have ‘odd eyes’ in addition to being a ‘striped species.’ This too can be handled by specifying OddEyesClass within EyesClass via interface (implements) and composition, giving Cat extensibility for both ‘species’ and ‘eyes.’

This allowed us to reiterate the first principle of Object-Oriented Programming, “Prefer ‘interface-composition’ over ‘class-inheritance’,” and the second principle, “Compose with ‘interfaces’ over ‘concrete classes’.”

Now, let’s explore design patterns using these two principles and three arrows (implementation, inheritance, composition).


  1. https://martinfowler.com/articles/injection.html
  2. http://www.nextree.co.kr/p11247/
  3. http://www.nextree.co.kr/p6753/
1. What is a 'Design Pattern'?
Author
Aaron
Posted on
Licensed Under
CC BY-NC-SA 4.0
CC BY-NC-SA 4.0
More in this category
Recent posts
The Erosion of Conversational Muscle and Communication Styles by LLM Filters
In an era where LLM tools, which filter out conversational impoliteness and deliver refined responses, have become commonplace, are we truly engaging in more thoughtful conversations? This article examines the phenomenon of conversational ability, which should be honed through countless failures in real-time communication, degenerating due to reliance on external tools. It further explores the potential societal anxieties and shifts in generational behavioral patterns that this trend may bring.
Optimal Timing and Strategy for Salary Negotiation with Senior Candidates
Salary negotiation is more than just an exchange of figures; it's a strategic dance of psychological timing. This analysis explores why engaging in a gradual negotiation process from the initial stages of recruitment, rather than waiting until after a final offer (when candidates tend to adopt a more calculative stance), proves more efficient for companies and fosters a more honest sharing of resources.
The Limits of the Rule of Law and Human Diversity
The belief that all human actions can be regulated by a single legal system may be an act of hubris. This article offers a sharp analysis of the paradox of the rule of law faced by humanity, which, having escaped the hierarchical controls of the Middle Ages, has now embraced infinite modern freedom. It further examines the deepening social coercion and the demonization of others that arise under the guise of diversity.
토스트 예시 메세지