Why design patterns are needed

At first

It is not an article that introduces the contents of design patterns in detail. I see, I want to remember! This is an article to increase your motivation.

Target

Someone who can write working programs. But people who can't write in an object-oriented way.

Design pattern example (Adapter pattern)

In the first place, the design pattern is introduced by the Adapter pattern.

Originally there were the following classes.

Adaptee.java


public class Adaptee{
    public void method1(){
	 System.out.println("method1");
    }

    public void method2(){
	 System.out.println("method2");
    }
}

Here, ** method1 (), newMethod2 () ** have the same function as ** newMethod1 (), newMethod2 () ** Suppose you are asked to implement as an interface. If you make this request without knowing the design pattern Maybe you're trying to implement it in the Adaptee class. According to the Adapter pattern, it will be as follows.

First, create the following class that includes the request interface (method) defined.

Target.java


public interface Target{
    public abstract void newMethod1();
    public abstract void newMethod2();
}

Then create the following class that incorporates Target.java and Adaptere.java.

Target.java


public class Adapter extends Adaptee implements Target{
    public void newMethod1(){
	 method1();
    }

    public void newMethod2(){
	 method2();
    }
}

When using it, it is as follows.

Client.java


public class Client{
    public static void main(String[] args){
	Target target = new Adapter();
	target.newMethod1();
	target.newMethod2();
    }
}

Like this, when you do this, write it like this! The design pattern is a collection of standard stones.

merit

** ・ Understand the policy when creating a program ** If you have a pattern in yourself, it's much easier than creating it from scratch when you come across a program that it can adapt to. Also, you don't have to memorize the pattern in its entirety, just grasp the outline and read it back when you encounter the pattern.

** ・ Become able to read the intent of the code of others ** Not only when you make it, but when you read other people's code, you will think "Oh, this is that pattern". And that seems to be the intent of the code.

Referenced site

http://www.ie.u-ryukyu.ac.jp/~e085739/java.it.6.html

Referenced book

[Introduction to Design Patterns Learned in the Augmented and Revised Java Language](https://www.amazon.co.jp/%E5%A2%97%E8%A3%9C%E6%94%B9%E8%A8%82% E7% 89% 88Java% E8% A8% 80% E8% AA% 9E% E3% 81% A7% E5% AD% A6% E3% 81% B6% E3% 83% 87% E3% 82% B6% E3% 82% A4% E3% 83% B3% E3% 83% 91% E3% 82% BF% E3% 83% BC% E3% 83% B3% E5% 85% A5% E9% 96% 80-% E7% B5 % 90% E5% 9F% 8E-% E6% B5% A9 / dp / 4797327030) Speaking of design patterns, this is a book.

Recommended Posts

Why design patterns are needed
Why are class variables needed? [Java]
Various design patterns
Java Design Patterns
Introduction to design patterns (introduction)
Why preventDefault is needed
Study GoF design patterns
Introduction to Design Patterns (Builder)
Read design patterns in Ruby
Introduction to Design Patterns (Composite)
Introduction to design patterns (Flyweight)
Introduction to design patterns Prototype
Introduction to Design Patterns (Iterator)
[Java] Summary of design patterns
Introduction to Design Patterns (Strategy)
Introduction to Design Patterns (Factory Method)
Introduction to Design Patterns (Abstract Factory)
Important design patterns to improve maintainability