[Java] Design pattern starting from You can do that with language specifications ①-Basics-

4 minute read

At the beginning

Various people have written articles about GoF design patterns, and I am always studying. On the other hand, however, those articles are intended for high-level people who can share the premise to some extent, such as “There are such merits by designing classes like this” and “The conventional methods have such problems”. I got the impression that it was an article aimed at, and a beginner like me does not understand it in the first place, “I can write this way due to the language specification”, “If you design a class like this, the program works like this” I don’t think I can find any articles that focus on “what can be done first.”

Therefore, this time, based on the theme of Design pattern to learn in Java language, from the viewpoint of “Java can do such things in terms of language specifications”, I will look at the pattern.

It may be off the subject, but since the subject dealt with here is basically Java, what can already be specified as specifications in other languages (eg, __iter__ in Python) or conversely cannot be done in other languages. However, I will also give examples of things that can only be done by writing it in a very elaborate manner (such as c++ dependency injection?). If you think so, there may be no design patterns common to all languages, but there may be design patterns for each language. However, I’m not so familiar with various programs, and I don’t have the skills to discuss design patterns in other languages, so I won’t go deep into this article. I can do it”. Then, the introduction became longer, but we will start.

Basic knowledge ① About types of variables

The variables handled in the GoF design pattern are roughly classified into the following three types. The advantages and disadvantages of each are as follows.

Variable Usage Advantages Disadvantages
Integer type (int, boolean, etc.) 8 8 8
Official class or basic type
(double,String etc.)
5 6 3
Own class 2 3 3

Basic knowledge ② How to handle variables

There are four ways to handle variables in a method. The range that can be handled and the points to note when handling are described below. Regarding the points to note, I feel that it deviates from the main subject of the article, but on the other hand, I think that the merit of the design pattern cannot be seen unless the features are grasped, so I will post it.

Variable type Range that can be handled Attention
Local variable In method This is the most recommended usage, but on the other hand, if you dynamically allocate a large class with new, the calculation cost may increase, so consult with the performance.
Member variable In each instance
(Share all classes if you add static)
Because it is a global variable whose access is limited, do not create it unnecessarily. Do not reuse counter variables.
Arguments User of the function If possible, state should not be changed in the function (add the const modifier if possible). Also, avoid using large classes that have multiple statuses as arguments.
Return value User side of function As with arguments, do not return in a very large class.

Basic knowledge (3) Member variable/function modifier

Modifier Modifiable Usage
static Member variables/functions 2
public Member variables/functions 2
plivate Member variables/functions 2
protected Member variables/functions 2
@Override Member function 2
abstract Member function A
final Member function A

Basic knowledge ④ Class modifier

Modifier Insertion position Usage  
extends Modifier for inheriting the class that follows extends after the class declaration. Be aware of the is-a relationship. Only one class can be inherited.  
abstract This is a modifier that prevents the creation of an instance of a class with abstract before the class declaration. You can inherit it without adding it, but add it if necessary to reduce mistakes.  
A class with final before the final class declaration cannot inherit. There is no problem if you do not add this too, but be sure to add it to the very final class that you do not want to be overwritten.
implements Modifier for implementing interface after class declaration. Be aware of the can-do relationship. Unlike extends, multiple interfaces can be implemented (multiple inheritance).  
Instead of the inteface class declaration, it is not actually a class qualifier, but here it is because the sequence is good. A type that can be written instead of class and can only define methods. The difference from abstract class is that you cannot define member variables.  

You can do that ② Dynamic switching of methods using inheritance


public class baseA {
public void func() {}


public class A extends baseA {
public void func() {System.out.println("A");}


public class A_ extends baseA {
public void func() {System.out.println("A_");}


public class Main {
public static void main(String[] args) {
It's a sequel.
baseA a;
a = new A();
It's a sequel.
baseA a_;
a_= new A_();

Execution result


I can’t do it ① Call a method that is not implemented in the parent class

Comment out the func implemented in baseA.


public class baseA {
//public void func() {}

Then, the compilation passes here,


A a;

This is a compilation error.


baseA a;
a = new A();

After creation

I’m sorry, I’ve posted a draft that I’m working on while I was working on it. As a result of investigation, it seems that once submitted, it is not possible to draft again, so complete the article as soon as possible. Until then, it will be awkward to everyone, but thank you in advance.