This article we still continue our design pattern series, today we bring a brand-new design pattern in the actual development of everyone must have met, maybe we just do not know it is called template method mode , today we will be in detail about what is the template method mode, How this pattern is used.
As for what is the template method pattern, we still have the same ritual, first a definition, and then a more intuitive look at the class diagram.
Defined
The template method pattern defines the steps of an algorithm and allows a subcategory to provide its practice in one or more steps. Let the subcategory redefine some of the steps in the algorithm without changing the algorithm schema.
This definition is very good, at least read 2 times can understand what the meaning of, and we can imagine how to design such a structure. What kind of use scene does it have? In fact, in the definition has been said very clearly, there is roughly a two-point application scenario:
- In some categories of algorithms, the same method is actually done, resulting in duplication of code.
- Some things you must follow to control sub-categories
Above two points summed up said, can think:
The components of the system are executed according to certain processes, and different components are implemented in different ways, requiring us to defer to subclasses to implement them.
The key point of the template method pattern is to defer the specific operation to the subclass implementation . Next, we look at the class diagram, from the class diagram we can see more intuitive experience of the template method pattern.
Code Show
According to the Convention, we should use the go language to implement the template method pattern in this link, but because of the particularity of Go language, it does not have the actual meaning inheritance, therefore realizes in go, still is a little not so same as what we said above, So I decided here we first use Java to implement the template method pattern, after understanding, we will consider how to use go to implement. In today's example, we are going to simulate the different behavior of male and female students when they go out, the boy may be sleeping before going out, waking up and go out; And girls are not the same, girls are to dress up to go out.
Template method mode for Java edition
With the actual examples above, let's start with a Java simulation. First we need to define an interface to regulate this behavior.
interface IPerson { void beforeOut(); void out();}
With the interface, it must be implemented, no matter male or female, he is human, and in our situation, go out this behavior is the same, not the same is just the behavior before going out. Corresponding to the code is the out
method is the same as male and female, different implementation is the beforeOut
method, so we design the human this class when the implementation of the care out
method.
abstract class Person implements IPerson { private String name; publicPerson(String name) { this.name = name; } publicvoidout() { beforeOut(); System.out.println("go out...") }}
Here we implement the out
method, and out
before printing to call the beforeOut
method, as to how this method is implemented, here to see whether it is a boy or a girl, very simple, directly to see the code.
class Boy extends Person { publicBoy(String name) { super(name); } publicvoidbeforeOut() { System.out.println("get up...") }}class Girl extends Person { publicBoy(String name) { super(name); } publicvoidbeforeOut() { System.out.println("dress up...") }}
OK, whether a boy or a girl, inherited from Person
this abstract class, and implemented beforeOut
this method, except that the boy's method implementation is a get up, and the girl is a dress up.
Now, a simple template method of the application of the pattern we have done, from the code we can also find that the pattern is very simple, in the previous blog I said that the design pattern itself is very simple, not too easy is the design patterns of application and practice.
OK, after the Java code to understand the template method pattern, the following began to enter the focus of today, how to use go to achieve it?
Version Golang Template method mode
To Golang know the students know, in fact, there is no strict Golang inheritance mechanism, it is only the use of the characteristics of the combination of inheritance, in this method for 组合优于继承
the embodiment is still very good, but this also brought some problems, such as can not be implemented abstract method, the above method delay implementation and so on. Here we do not worry, although Golang does not have this feature, but we can completely change a way to complete it, after all, the pattern is dead, the code is alive, and how to achieve it? The answer is binding! As for how to use, I do not say, we directly look at the code on the line!
According to the above Java code, we come to one by one implementation of it, the first is the interface, in the Golang we also have interfaces, so we can design a following interface.
typeinterface { string) BeforeOut() Out()}
This interface and the above Java version of the same, the main method is in the inside, and then we have to design a Person
structure. This is Person
not the same as the above Person
, so let's look at the code first.
typestruct { Specific IPerson name string}
is a bit different, there IPerson
is a type of field here, what is this field for? We mentioned above, here we are going to use binding way to implement the template method pattern, here this particular field is actually we want to bind the instance, in our case is a boy or a girl, There may be a lot of people to confuse, it's OK, finally we look at the code used to understand, here we should first look at this how to Person
do it.
funcstring) { this.name = name}func (this *Person) Out() { this.BeforeOut() " go out...")}func (this *Person) BeforeOut() { ifnil { return } this.Specific.BeforeOut()}
The first method we ignore it directly, the implementation of the second method and Java version is actually the same, to see the third method, this method is Person
not to implement the Java version, according to the reason this BeforeOut
is to delay to the sub-class to achieve, this does not conform to international practice AH? Let's take a look at the content of this method, in fact, the key point is a word, this.Specific.BeforeOut()
this sentence directly called the example of the binding we said the BeforeOut
method, carefully taste, or conform to the Convention, here we just use a compromise way to achieve the delay of the implementation of the method.
Well, the most difficult to understand the place we said in detail, the following is the boys and girls of their respective implementations, very simple, directly on the code.
typestruct { Person}func (_ *Boy) BeforeOut() { fmt.Println("get up..")}typestruct { Person}func (_ *Girl) BeforeOut() { fmt.Println("dress up..")}
In the code, whether Boy
or not the structure is combined anonymously, the implementation of Girl
Person
their method is basically the same as BeforeOut
the Java version above, here we will not say more, and finally we look at such a design, we how to use.
func main() { var p *Person = &Person{} p.Specific = &Boy{} p.SetName("qibin") p.Out() p.Specific = &Girl{} p.SetName("loader") p.Out()}
The main body of the code is still Person
, but we distinguish between male and female students, give him a Specific
field, and then recall the Person
structure of the implementation of the BeforeOut
method, is not an instant to understand what is going on. Let's take a look at the results of the operation.
Finally, it's about the case code of the article, code I put on GitHub, welcome all kinds of Star, Https://github.com/qibin0506/go-designpattern
Design mode-template method mode (Go language description)