|
*************
// Chain of Responsibility
// Similar to the implementation form of Decorator,
// Decorator adds the function on the original method, while
// Chain indicates that if the signal is not currently processed, it is transferred to the next node for processing.
// I can use the if branch to achieve the same effect, but it is not flexible enough. Each node on the chain can be replaced and increased.
// More flexible. We can design interfaces to add and delete nodes to achieve more convenient results.
// This is a chain structure. Have you ever thought of using a ring structure?
Interface Handler {
Void handRequest (int signal );
}
Class CHandler1 implements Handler {
Private Handler handler;
Public CHandler1 (Handler handler ){
This. handler = handler;
}
Public void handRequest (int signal ){
If (signal = 1 ){
System. out. println ("handle signal 1 ");
}
Else {
Handler. handRequest (signal );
}
}
}
Class CHandler2 implements Handler {
Private Handler handler;
Public CHandler2 (Handler handler ){
This. handler = handler;
}
Public void handRequest (int signal ){
If (signal = 2 ){
System. out. println ("handle signal 2 ");
}
Else {
Handler. handRequest (signal );
}
}
}
Class CHandler3 implements Handler {
Public void handRequest (int signal ){
If (signal = 3 ){
System. out. println ("handle signal 3 ");
}
Else {
Throw new Error ("can't handle signal ");
}
}
}
Class ChainClient {
Public static void main (String [] args ){
Handler h3 = new CHandler3 ();
Handler h2 = new CHandler2 (h3 );
Handler h1 = new CHandler1 (h2 );
H1.handRequest (2 );
}
}
// Interpreter
// Similar to Composite, except for the final and non-final characters
// Template Method
Abstract class TemplateMethod {
Abstract void amd1 ();
Abstract void amd2 ();
// This Method is a Template Method.
Public void tmd (){
Amd1 ();
Amd2 ();
}
}
// State
// Standard type
// The status and operation should not be coupled.
Class Contexta {
Private State st;
Public Contexta (int nst ){
ChangeStfromNum (nst );
}
Public void changeStfromNum (int nst ){
If (nst = 1 ){
St = new CStatea1 ();
}
Else if (nst = 2 ){
St = new CStatea2 ();
}
Throw new Error ("bad state ");
}
Void request (){
St. handle (this );
}
}
Interface State {
Void handle (Contexta context );
}
Class CStatea1 implements State {
Public void handle (Contexta context ){
System. out. println ("state 1 ");
// You may need to change the status during the processing of a State. For example, you can disable this effect immediately after it is enabled.
// Context. changeStfromNum (2 );
}
}
Class CStatea2 implements State {
Public void handle (Contexta context ){
System. out. println ("state 2 ");
}
}
// Factory type
// Generate different states based on the state failure
// Class StateFactory {
// Public static State getStateInstance (int num ){
// State st = null;
//
// If (num = 1 ){
// St = new CStatea1 ();
//}
// Else if (num = 2 ){
// St = new CStatea2 ();
//}
//
// Return st;
//}
//}
// Strategy
// Similar to Bridge, it is a multi-state representation.
// Visitor
// Bidirectional reference. Use another class to call your own method and access your data structure.
Interface Visitor {
Void visitElement (Elementd element );
}
Class CVisitor implements Visitor {
Public void visitElement (Elementd element ){
Element. operation ();
}
}
Interface Elementd {
Void accept (Visitor visitor );
Void operation ();
}
Class CElementd implements Elementd {
Public void accept (Visitor visitor ){
Visitor. visitElement (this );
}
Public void operation (){
// The actual operation is here
}
}
Class Clientd {
Public static void main (){
Elementd elm = new CElementd ();
Visitor vis = new CVisitor ();
Vis. visitElement (elm );
}
}
// Iteraotr
// Use the iterator to perform sequential iteration on the data structure of a class
Interface Structure {
Interface Iteratora {
Void first ();
Boolean hasElement ();
Object next ();
}
}
Class Structure1 implements Structure {
Object [] objs = new Object [1, 100];
// Use an internal class to have full access to the Struture1 Data Structure
Class Iteratora1 implements Iteratora {
Int index = 0;
Public void first (){
Index = 0;
}
Public boolean hasElement (){
Return index <100;
}
Public Object next (){
Object obj = null;
If (hasElement ()){
Obj = objs [index];
Index ++;
}
Return obj;
}
}
}
// Meditor
Class A1 {
Public void operation1 (){}
Public void operation2 (){}
}
Class A2 {
Public void operation1 (){}
Public void operation2 (){}
}
Class Mediator {
A1 a1;
A2 a2;
Public Mediator (A1 a1, A2 a2 ){
This. a1 = a1;
This. a2 = a2;
}
// If I want to implement this function, I may put it in A1.
// But this coupling is large, so I don't want to reference A2 objects in A1,
// So I used Mediator as the intermediary
Public void mmed1 (){
A1.operation1 ();
A2.operation2 ();
}
Public void mmed2 (){
A2.operation1 ();
A1.operation2 ();
}
}
// Command
// I think the method is converted into a class
Class extends er {
Public void action1 (){}
Public void action2 (){}
}
Interface Command {
Void Execute ();
}
Class CCommand1 implements Command {
Private aggreger extends er;
Public CCommand1 (extends er extends ER ){
This. Cycler = Cycler;
}
Public void Execute (){
Cycler. action1 ();
}
}
Class CCommand2 implements Command {
Private aggreger extends er;
Public CCommand2 (extends er extends ER ){
This. Cycler = Cycler;
}
Public void Execute (){
Explorer. action2 ();
}
}
// Observer
// It seems that this mode is useless.
// However, if I have a thread that monitors the Subject status
// If the status of the Observer changes, the status of the Observer is changed and some operations are performed.
// The Observer and the Visitor are similar in that they both have two-way references.
// Subject can register many observers
Interface Subjectb {
Void attach (Observer observer );
Void detach (Observer observer );
Void mypolicy ();
Int getstate ();
Void setstate (INT State );
}
Class subjectb1 implements subjectb {
List Observers = new arraylist ();
Int state;
Public void attach (Observer observer ){
Observers. Add (observer );
}
Public void detach (Observer observer ){
Observers. Remove (observer );
}
Public void mypolicy (){
Observer observer = NULL;
Iterator it = observers. iterator ();
While (it. hasnext ()){
Observer = (observer) it. Next ();
Observer. Update ();
}
}
Public int getstate (){
Return state;
}
Public void setState (int state ){
This. state = state;
}
}
Interface Observer {
Void Update ();
}
Class Observer1 implements Observer {
Subjectb subject;
Int state;
Public Observer1 (Subjectb subject ){
This. subject = subject;
}
Public void Update (){
This. state = subject. getState ();
}
Public void operation (){
// Some state-based operations
}
}
// Memento
// It seems that this mode is useless.
Class Memento {
Int state;
Public int getState (){
Return state;
}
Public void setState (int state ){
This. state = state;
}
}
Class Originator {
Int state;
Public void setMemento (Memento memento ){
State = memento. getState ();
}
Public Memento createMemento (){
Memento memento = new Memento ();
Memento. setState (1 );
Return memento;
}
Public int getState (){
Return state;
}
Public void setState (int state ){
This. state = state;
}
}
Class careTaker {
Memento memento;
Public void saverMemento (Memento memento ){
This. memento = memento;
}
Public Memento retrieveMemento (){
Return memento;
}
}
// The program is finally executed in sequence, and is spliced by the disconnected operations.
// Concatenate codes of different classes through reference.
// It is equivalent to having the ability to access data structures and methods.
// Similar. For example, I want to extract a method from a class because this method depends on the data and other methods of this class.
// It is not feasible to directly remove the code, but if we have references to such objects
// Internal, so we can remove this method with reference
Public class tt1 {
Public static void main (String [] args ){
}
} |