Introducing the design patterns of [GoF](https://ja.wikipedia.org/wiki/Gang of for _ (Information Engineering)) ["Introduction to Design Patterns Learned in the Augmented and Revised Java Language"]( https://www.amazon.co.jp/ Augmented and revised edition Introduction to design patterns learned in Java language-Hiroshi Yuki / dp / 4797327030 / ref = pd_lpo_sbs_14_t_0? _ Encoding = UTF8 & psc = 1 & refRID = 2ZE4GPYNN55JGDR5QMHP) I will summarize about.
Translated into Japanese, it means "decorator". ** A pattern that expands functionality by steadily decorating the original object ** is called the ** Decorator pattern **. By applying this pattern, you can flexibly extend the functionality without changing the class from which it is extended.
The Decorator pattern is used by the classes that appear in the class diagram below.
Component
Represents the abstract class from which it is decorated (extended).
Decorator
An abstract class that inherits from the Component
class and is the base class for the decorating class. Of particular note is that the field has the Component
class to be decorated **.
ConcreteComponent
Implementation class of the Component
class. Implement all the abstract methods of Component class.
ConcreteDecorator
An implementation class for the Decorator
class. The Decorator class inherits from the Component class, so you need to implement the abstract methods that the Component
class has.
As a concrete example, it will be explained based on the following class.
-** Sponge Cake class **
SpongeCake.java
public abstract class SpongeCake {
public abstract String getName(); //Get a name
public abstract int getPrice(); //Get the price
public void show() { //Show name and price
System.out.println(getName() + ":" + getPrice() + "Circle");
}
}
In the SpongeCake
class, a method to get the name and price is defined as an abstract method, and a method to display them is defined as a concrete method.
It's just a sponge cake with no decoration.
The actual processing of the abstract class will be implemented in the subclass described later.
There is nothing particularly difficult.
-** Decorator class **
Decorator.java
public abstract class Decorator extends SpongeCake {
protected SpongeCake spongeCake; //Refers to the "contents" that this decorative frame is wrapped in
protected Decorator(SpongeCake spongeCake) { //Specify "contents" as an argument when creating an instance
this.spongeCake = spongeCake;
}
}
The Decorator class
inherits from the SpongeCake class above, but is an abstract class because it does not override the abstract method.
** The point is that the SpongeCake object to be decorated is held in the field and specified as an argument when the object is created by the constructor **.
-** ShortCake class **
ShortCake.java
public class ShortCake extends SpongeCake {
@Override
public String getName() { //Override the method to get the name of the parent class
return "Shortcake";
}
@Override
public int getPrice() { //Override the method to get the price of the parent class
return 500;
}
}
-** ChocolateCake class **
ChocolateCake.java
public class ChocolateCake extends SpongeCake {
@Override
public String getName() { //Override the method to get the name of the parent class
return "Chocolate cake";
}
@Override
public int getPrice() { //Override the method to get the price of the parent class
return 700;
}
}
The ShortCake
class and the ChocolateCake
class are implementation classes of the SpongeCake
class.
It is an image of a more concrete cake with a little decoration on the cake with only sponge.
It overrides the abstract methods getName ()
and getPrice ()
, and returns the name and price respectively.
There is nothing particularly difficult here either.
-** Strawberry Decorator class **
StrawberryDecorator.java
public class StrawberryDecorator extends Decorator {
public StrawberryDecorator(SpongeCake spongeCake) { //Pass the decoration target in the constructor
super(spongeCake);
}
@Override
public String getName() { //Strawberries added to the beginning of the name of the contents
return "Strawberry" + spongeCake.getName();
}
@Override
public int getPrice() { //The price of the contents plus 100
return spongeCake.getPrice() + 100;
}
}
-** Banana Decorator class **
BananaDecorator.java
public class BananaDecorator extends Decorator {
public BananaDecorator(SpongeCake spongeCake) { //Pass the decoration target in the constructor
super(spongeCake);
}
@Override
public String getName() { //Banana added to the beginning of the name of the contents
return "banana" + spongeCake.getName();
}
@Override
public int getPrice() { //The price of the contents plus 300
return spongeCake.getPrice() + 300;
}
}
The StrawberryDecorator
class and the BananaDecorator
class are implementation classes of the Decorator class.
Add strawberries and bananas to the sponge cake (or its subclasses) specified in the constructor as decorations, respectively.
Also, add the price in the same way.
The point is that ** the constructor argument is of type SpongeCake **.
From this, we can see that the arguments that can be passed to the constructor are the SpongeCake class, or its subclasses ShortCake class and ChocolateCake class.
Also, keep in mind that the Decorator class is also a subclass of the SpongeCake class.
In other words, ** the StrawberryDecorator
class and the BananaDecorator
class that inherit from the Decorator class can be specified as arguments to the constructor of the StrawberryDecorator
class that performs decoration **.
This makes it possible to add more decoration to the decorated object.
-** Main class **
Main.java
public class Main {
public static void main(String[] args) {
//Generation of shortcake
SpongeCake s1 = new ShortCake();
SpongeCake s2 = new StrawberryDecorator(s1);
SpongeCake s3 = new BananaDecorator(s1);
SpongeCake s4 = new StrawberryDecorator(new ShortCake());
SpongeCake s5 = new StrawberryDecorator(new BananaDecorator(new ShortCake()));
s1.show();
s2.show();
s3.show();
s4.show();
s5.show();
System.out.println("--------------------------------------------");
//Chocolate cake generation
SpongeCake c1 = new ChocolateCake();
SpongeCake c2 = new StrawberryDecorator(c1);
SpongeCake c3 = new BananaDecorator(c1);
SpongeCake c4 = new StrawberryDecorator(new ChocolateCake());
SpongeCake c5 = new StrawberryDecorator(new BananaDecorator(new ChocolateCake()));
c1.show();
c2.show();
c3.show();
c4.show();
c5.show();
}
}
Instances of the ShortCake
class and the ChocolateCake
class are created and decorated respectively.
The point is, as described in the description of the StrawberryDecorator
class and the BananaDecorator
class, ** Since the class to be decorated can be specified in the argument of the constructor, it is possible to perform multiple decorations. That is **.
Also,
4,5th line
SpongeCake s1 = new ShortCake();
SpongeCake s2 = new StrawberryDecorator(s1);
When
7th line
SpongeCake s4 = new StrawberryDecorator(new ShortCake());
Is just specifying the decoration target ShortCake
in the constructor, and although the writing method is different, it is actually the same process.
The result of executing Main.java
is as follows.
You can see that the original Short Cake and Chocolate Cake are being decorated more and more.
Execution result
Shortcake:500 yen
Strawberry shortcake:600 yen
Banana shortcake:800 yen
Strawberry shortcake:600 yen
Strawberry banana shortcake:900 yen
--------------------------------------------
Chocolate cake:700 yen
Strawberry chocolate cake:800 yen
Banana chocolate cake:1000 yen
Strawberry chocolate cake:800 yen
Strawberry banana chocolate cake:1100 yen
The advantages of the Decorator pattern are: ** 1. ** Functions can be added without changing the inherited class. ** 2. ** Various functions can be added by combining the necessary parts.
I learned about the Decorator pattern, which adds decoration to objects one after another. The sample code is uploaded below, so please refer to it if you like.
In addition, other design patterns are summarized below, so please refer to them as well.
-[Updated from time to time] Summary of design patterns in Java
-[Introduction to Design Patterns Learned in the Augmented and Revised Java Language](https://www.amazon.co.jp/ Introduction to Design Patterns Learned in the Augmented and Revised Java Language-Hiroshi Yuki / dp / 4797327030 / ref = pd_lpo_sbs_14_t_0? _Encoding = UTF8 & psc = 1 & refRID = 2ZE4GPYNN55JGDR5QMHP)
Recommended Posts