[JAVA] Introduction to Design Patterns (Strategy)

This article summarizes Strategy. According to wikipedia, "Algorithms can be changed in various ways while being independent of the user." Reference: Strategy pattern

** Main characters **

NO name role
1 context Instantiate a strategy
2 Strategy Algorithm interface
3 Concrete class of strategy Algorithm implementation

It is said that the algorithm is independent of the user, but the processing groups that can be replaced are hidden by the interface and summarized in detail. Localize the range of influence by replacing it if necessary.

** Implement the pattern ** We will implement it assuming that you will use the automatic ticket gate at the station. The Strategy pattern implements the act of passing through three types of ticket gates: "contactless IC card (Suica, ICOCA, etc.) ticket gates," "transfer ticket gates," and "ticket ticket gates."

** Strategy **


interface Strategy {
  void pass();

** Concrete class of strategy **


class ConcreteNonContactTurnstile implements Strategy {
  public void pass() {
    System.out.println("Set And Touch!");


class ConcreteTicketTurnstile implements Strategy {
  public void pass() {
    System.out.println("Ticket accept");


class ConcreteTransferTurnstile implements Strategy  {
  public void pass() {
     System.out.println("Transfer Line!");



class ContextStation {
  Strategy strategy;

  public ContextStation(Strategy strategy) {
    this.strategy = strategy;

  public void pass() {

** Execution class **


class Main {
  public static void main(String[] args) {
    ContextStation contextStation;
    contextStation = new ContextStation(new ConcreteNonContactTurnstile());


Set And Touch!

As mentioned above, we abstract similar processing groups by implementing one interface. We are instantiating the following classes in the strategy via a class in the role of context that holds a reference to that interface. At the time of implementation, the purpose of this pattern is to change the processing content at high speed by replacing the XX part of the main class "new ContextStation (new Concrete XX)". If you want to add a new type of ticket gate "manned ticket gate", you can implement it by adding a concrete class of a new strategy and replacing the part of the pattern mentioned above.

Recommended Posts

Introduction to Design Patterns (Strategy)
Introduction to design patterns (introduction)
Introduction to design patterns (Flyweight)
Introduction to design patterns Prototype
Introduction to Design Patterns (Iterator)
Introduction to Design Patterns (Factory Method)
Introduction to Design Patterns (Abstract Factory)
Important design patterns to improve maintainability
Introduction to Ruby 2
Various design patterns
Design pattern ~ Strategy ~
Java Design Patterns
Introduction to web3j
[Java] Introduction to Java
Introduction to migration
Introduction to Doma
Introduction to JAR files
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
Introduction to Practical Programming
Introduction to javadoc command
Introduction to jar command
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Introduction to java command
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Study GoF design patterns
Introduction to javac command
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to Android application development
Introduction to RSpec 3. Model specs
Introduction to Ratpack (5) --Json & Registry
Introduction to Metabase ~ Environment Construction ~
Introduction to Ratpack (7) --Guice & Spring
(Dot installation) Introduction to Java8_Impression
Introduction to Micronaut 2 ~ Unit test ~
Introduction to JUnit (study memo)
Introduction to Spring Boot ① ~ DI ~
[Java] Introduction to lambda expressions
Introduction to Spring Boot ② ~ AOP ~
Introduction to Apache Beam (2) ~ ParDo ~
[Ruby] Introduction to Ruby Error statement
Introduction to EHRbase 2-REST API
GitHub Actions Introduction to self-made actions
[Java] Introduction to Stream API
Introduction to Spring Boot Part 1
Introduction to Ratpack (1) --What is Ratpack?
XVim2 introduction memo to Xcode12.3
Why design patterns are needed
[Java] Summary of design patterns
Introduction to RSpec-Everyday Rails Summary-
[Introduction to rock-paper-scissors games] Java
Design patterns to try with Swift-Iterator patterns that support Arrays and Dictionary
Design patterns to enjoy with frequently used Java libraries --Factory pattern
I read "7 Design Patterns to Refactor MVC Components in Rails" (writing)
Introduction to Linux Container / Docker (Part 1)
Introduction to swift practice output Chapter5