Introduction to Design Patterns (Abstract Factory)

This article summarizes the Abstract Factory. According to wikipedia, "Provides a way to properly generate a set of related instances depending on the situation." Reference: [Design pattern (software)](https://ja.wikipedia.org/wiki/%E3%83%87%E3%82%B6%E3%82%A4%E3%83%B3%E3%83 % 91% E3% 82% BF% E3% 83% BC% E3% 83% B3_ (% E3% 82% BD% E3% 83% 95% E3% 83% 88% E3% 82% A6% E3% 82% A7% E3% 82% A2)))

I referred to the following article. Contemplation: Abstract Factory pattern Abstract Factory for implementing and understanding GoF design patterns in Java

If you want to use the methods of another class from one class, you need to instantiate "new class name ()" except for the static class. In this design pattern, what was new to the calling class is moved to the class group called Factory, and the instance of the class you want to use is acquired via the method held by the Factory class.

Implementation example

Abstract Factory represents the case of refueling high octane and regular at a gas station.

NO file name Class contents
1 AbstractFactory.java An abstract class that defines refueling gasoline
2 ConcreteHighFactory.java Concrete class to refuel high octane
3 ConcreteRegularFactory.java Concrete class to refuel regular
4 AbstractRefueling.java Refueling abstract class
5 ConcreteHighRefueling.java High-octane refueling concrete class
6 ConcreteRegularRefueling.java Regular refueling concrete class
7 Main.java Class to execute

Java

AbstractFactory.java


public abstract AbstractFactory {
    abstract AbstractRefueling getAbstractRefueling();
}

ConcreteHighFactory.java


public class ConcreteHighFactory extends AbstractFactory {
    @Override
    public AbstractRefueling getAbstractRefueling();
        return new ConcreteHighRefueling();
    }
}

ConcreteRegularFactory.java


public class ConcreteRegularFactory extends AbstractFactory {
    @Override
    public AbstractRefueling getAbstractRefueling();
        return new ConcreteRegularRefueling();
    }
}

AbstractRefueling.java


public abstract class AbstractRefueling {
    abstract payment();
    abstract refueling();
}

ConcreteHighRefueling.java


public class ConcreteHighRefueling extends AbstractRefueling {
    @Override
    public payment() {};
    @Override
    public refueling() {};
}

ConcreteRegularRefueling.java


public class ConcreteRegularRefueling extends AbstractRefueling {
    @Override
    public payment() {};
    @Override
    public refueling() {};
}

Main.java


public class Main {
    public static void main(String... args) {
        Factory fa = exec("High");
        AbstractRefueling ab = fa.getAbstractRefueling();
    }
    private static Factory exec(String env) {
        if (env == "high") {
                return new ConcreteHighFactory();
        } else if (env == "regular") {
                return new ConcreteRegularFactory();
        }
    }
}

As mentioned above, we are receiving a class to refuel high octane gasoline on the assumption that high octane gasoline will be added on the Main method. If necessary, you can replace it with other functions defined in the Factory class by rewriting Conclete ~ Factory in the exec method in Main.java. Also, in this case, we implemented two types, high-octane and regular, but if you need to add a processing type such as light oil, you can simply add a class because it is abstracted, and the existing one. It can be said that it has little effect on high-octane and regular classes. However, although it is flexible to add classes, when adding parts (in this example, adding a window cleaning method in addition to the refueling method), the range of influence extends to all Factory classes.

List of design patterns

Recommended Posts

Introduction to Design Patterns (Abstract Factory)
Introduction to Design Patterns (Factory Method)
Introduction to design patterns (introduction)
Introduction to Design Patterns (Builder)
Introduction to Design Patterns (Composite)
Introduction to design patterns (Flyweight)
Introduction to design patterns Prototype
Introduction to Design Patterns (Iterator)
Introduction to Design Patterns (Strategy)
Design pattern ~ Abstract Factory ~
Important design patterns to improve maintainability
Design patterns to enjoy with frequently used Java libraries --Factory pattern
abstract Factory Pattern
Introduction to Ruby 2
Various design patterns
Introduction to SWING
Abstract Factory pattern
Java Design Patterns
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to migration
Introduction to java
Introduction to Doma
Introduction to JAR files
Introduction to Ratpack (8)-Session
Design pattern ~ Factory Method ~
Introduction to RSpec 1. Test, RSpec
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
I read Hiroshi Yuki "Introduction to Design Patterns Learned in Java Language" (SB Creative)
Introduction to Android Layout
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
Introduction to RSpec 4. Create test data with Factory Bot
Study GoF design patterns
Introduction to javac command
Read design patterns in Ruby
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 Ratpack (7) --Guice & Spring
(Dot installation) Introduction to Java8_Impression
Introduction to Micronaut 2 ~ Unit test ~
[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