Go to content
The Codest
  • About Us
    • Staff Augmentation
    • Project Development
    • Cloud Engineering
    • Quality Assurance
    • Web Development
  • Our Team
  • Case studies
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
  • About Us
    • Staff Augmentation
    • Project Development
    • Cloud Engineering
    • Quality Assurance
    • Web Development
  • Our Team
  • Case studies
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
Content
2018-12-18
The CodestSoftware Development

That’s the Strategy! Strategy Design Pattern

Sara Klimek

A design pattern is a conceptual model that helps you solve design problems in various contexts. It is like a reusable template you can apply to a structuring code.

While working on a project, it is crucial to remember that you do not want to reinvent the wheel. If a solution for a problem is proved to be effective, reusing it will save you tons of time and a few headaches. In case of system architecture dilemmas, we have developed some patterns to ease a design process.

What is a design pattern?

A design pattern is a conceptual model that helps you solve design problems in various contexts. It is like a reusable template you can apply to a structuring code. The thing is, design patterns are well-documented, well-proven and commonly known. In other words, use a proper design pattern and be sure that other design pattern insiders (aka your coworkers) will get the hang of your ideas smoothly.

Eager to learn? Here comes the Strategy Design Pattern.

The strategy design pattern:

  • defines a family of algorithms,

  • implements each of the algorithms as a separate entity,

  • allows each of the algorithms to be used interchangeably in a given context.

Real life example? Authentication

In one of our apps we have implemented a few strategies for authenticating requests. We allow authentication with cookies, JWT tokens and API keys. Sounds like a family of algorithms to me.

Let’s have a look at a sample implementation.

We have our family of authentication algorithms divided into three separate classes. Each class defines the concrete behavior; it is where all the implementation details go:

module AuthenticationStrategies
  class CookiesAuthentication
    def self.authenticate_request(request)
      puts "I implement a cookie authentication algorithm"
    end
  end
class JWTAuthentication
    def self.authenticate_request(request)
      puts "I implement a jwt authentication algorithm"
    end
  end
class ApiKeyAuthentication
    def self.authenticate_request(request)
      puts "I implement an api key authentication algorithm"
    end
  end
end

Then, we have a context/client class:

class Authentication
  def self.authenticate(authentication_strategy, request)
    authentication_strategy.authenticate_request(request)
  end
end
request = "I pretend to be a request"
Authentication.authenticate(AuthenticationStrategies::CookiesAuthentication, request)
Authentication.authenticate(AuthenticationStrategies::JWTAuthentication, request)
Authentication.authenticate(AuthenticationStrategies::ApiKeyAuthentication, request)

The above script outputs:

I implement a cookie authentication algorithm I implement a jwt authentication algorithm I implement an api key authentication algorithm

Summing it up

  • design patterns are useful tools for solving architectural problems,

  • knowledge of design patterns improves communication on system architecture with other developers,

  • the Strategy Design Pattern suits your needs if you have a bunch of algorithms that can be used interchangeably in a given context.

Related articles

The Codest

Project management in SCRUM

SCRUM is a project management methodology based on empirical process control theory, which is consistent with the values of the Agile manifesto (2001). This is not a restrictive work methodology but rather a framework that allows...

Mateusz Lesniak
The Codest

The Codest Core Value #1

The Codest believes in four crucial values that are the core of all actions taken by The Codest teams. In this article, our CEO and co-founder, Greg Polec, explains what acting fast and focusing on the bigger picture means to us.

Greg Polec
The Codest

The Codest's Success Story: Meet Lukasz Brzeszcz

This time the star of The Codest Success Story is Lukasz Brzeszcz, our Ruby software developer and programmer. How did Lukasz’s journey with The Codest begin and what helped him to become an awesome manager? Read this article to...

Monika Krupa
The Codest

The Codest's Success Story: Meet our Finance Lead Iza

In this article series, we share the stories of The Codest teammates who have had an impact on the present shape of our company. In the following piece, we ask our Financial Lead, Iza, some questions about her career at The...

Monika Krupa

Subscribe to our knowledge base and stay up to date on the expertise from industry.

About us

The Codest – International Tech Software Company with tech hubs in Poland.

    United Kingdom - Headquarters

  • Office 303B, 182-184 High Street North E6 2JA London, England

    Poland - Local Tech Hubs

  • Fabryczna Office Park, Aleja Pokoju 18, 31-564 Kraków
  • Brain Embassy, Konstruktorska 11, 02-673 Warsaw, Poland

    The Codest

  • Home
  • About us
  • Services
  • Case studies
  • Know how
  • Careers

    Services

  • PHP development
  • Java development
  • Python development
  • Ruby on Rails development
  • React Developers
  • Vue Developers
  • TypeScript Developers
  • DevOps
  • QA Engineers

    Resources

  • What are top CTOs and CIOs Challenges? [2022 updated]
  • Facts and Myths about Cooperating with External Software Development Partner
  • From the USA to Europe: Why do American startups decide to relocate to Europe
  • Privacy policy
  • Website terms of use

Copyright © 2023 by The Codest. All rights reserved.

We use cookies on the site for marketing, analytical and statistical purposes. By continuing to use, without changing your privacy settings, our site, you consent to the storage of cookies in your browser. You can always change the cookie settings in your browser. You can find more information in our Privacy Policy.