There are about 10 behavior patterns, which are divided into the upper, middle, and lower layers. 1. Template Description: defines the skeleton of some Operation algorithms and delays implementation to its subclass. Advantages: Strong scalability example: the Java Abstract class is originally the Template mode, therefore, it is widely used. it is easy to understand and use. Let's start with an example: public abstract class Benchmark {/***. The following operations we want to complete in the subclass */public abstract void benchmark (); /*** repeated benchmark execution times */public final long repeat (int count) {if (count <= 0) return 0; else {long startTime = System. currentTimeMillis (); for (int I = 0; I <count; I ++) benchmark (); long stopTime = Sys Tem. currentTimeMillis (); return stopTime-startTime ;}} in the above example, we want to repeat the benchmark () operation, but the specific content of benchmark () is not described, it is delayed in its subclass Description: public class MethodBenchmark extends Benchmark {/*** truly defines the benchmark content */public void benchmark () {for (int I = 0; I <Integer. MAX_VALUE; I ++) {System. out. printtln ("I =" + I) ;}} so far, the Template mode has been completed, isn't it easy? See how to use: Benchmark operation = new MethodBenchmark (); long duration = operation. repeat (Integer. parseInt (args [0]. trim (); System. out. println ("The operation took" + duration + "milliseconds"); 2. Memento Description: The object that saves The copy of The internal state of another object. in this way, the object can be restored to the previously saved state. Benefits: the details to be saved are encapsulated in Memento without exposing the encapsulation details, the details of the day to be changed and saved do not affect the client. Example: public class Originator {private int number; private File file = null; public Originator (){}// Create a Memento public Memento getMemento () {return new Memento (this);} // restore to the original value public void setMemento (Memento m) {number = m. number; file = m. file ;}} let's take a look at the Memento class: private class Memento implements java. io. serializable {private int number; private File file = null; public Memento (Originator o) {number = o. number; file = o. file ;}} the number and file values in Originator are saved in Memento. by calling the number and If the file value is changed, it can be restored by calling the setMemento () method. the disadvantage of Memento mode is that it is expensive. If there are many internal states, you have no intention of wasting a lot of memory. 3. Observer (Observer) Description: defines a one-to-many dependency, so that multiple Observer objects can listen to a topic object at the same time. When the status of the topic object changes, it notifies all observer objects so that they can automatically update themselves. The observer mode establishes an abstract coupling between the observer and the observer. The observer mode supports broadcast communication. The observer sends a notification to all registered observers. Example: first define the abstract observer: // abstract observer role public interface Watcher {public void update (String str);} then define the abstract topic role, that is, the abstract observer, declare the method (add or remove observer, notify Observer): // abstract topic role, watched: Observed public interface Watched {public void addWatcher (Watcher watcher ); public void removeWatcher (Watcher watcher); public void policywatchers (String str);} then define the specific observer: public class ConcreteWatcher implements Watcher {@ Overridepublic void update (String str) {System. out. println (str) ;}} is followed by a specific topic role: import java. util. arrayList; import java. util. list; public class ConcreteWatched implements Watched {// stores the observer private List <Watcher> list = new ArrayList <Watcher> (); @ Overridepublic void addWatcher (Watcher watcher) {list. add (watcher) ;}@ Overridepublic void removeWatcher (Watcher watcher) {list. remove (watcher) ;}@ Overridepublic void policywatchers (String str) {// automatic call is actually called by the topic for (Watcher watcher: list) {watcher. update (str) ;}}write Test class: public class Test {public static void main (String [] args) {Watched girl = new ConcreteWatched (); watcher watcher1 = new ConcreteWatcher (); Watcher watcher2 = new ConcreteWatcher (); Watcher watcher3 = new ConcreteWatcher (); girl. addWatcher (watcher1); girl. addWatcher (watcher2); girl. addWatcher (watcher3); girl. notifyWatchers ("happy ");}}