23 Design Patterns

 Classification:

There are five types of creational patterns: factory method pattern, abstract factory pattern, singleton pattern, builder pattern, and prototype pattern.

There are seven structural patterns: adapter pattern, decorator pattern, proxy pattern, appearance pattern, bridge pattern, composition pattern, and flyweight pattern.

There are eleven behavioral patterns: strategy pattern, template method pattern, observer pattern, iteration sub pattern, responsibility chain pattern, command pattern, memorandum pattern, state pattern, visitor pattern, mediator pattern, interpreter pattern

1. Simple factory mode: It is to establish a factory class to create instances for subclasses that implement the same interface or subclasses that inherit the same parent class.

 

public class Test {
    public static void main(String[] args) {
        Operation oper;
        oper = OperationFactory.createOperation(2);
        oper.numberA = 1;
        oper.numberB = 2;
        System.out.println(oper.getResult());
    }
}
// Operation class 
class Operation
{
    public double numberA = 0;
    public double numberB = 0;
    
    public double getNumberA() {
        return numberA;
    }
    public void setNumberA(double numberA) {
        this.numberA = numberA;
    }
    public double getNumberB() {
        return numberB;
    }
    public void setNumberB(double numberB) {
        this.numberB = numberB;
    }
    
    public double getResult() {
        return 0;
    }
}
// The addition class inherits the operation class 
class OperationAdd extends Operation
{
    public double getResult() {
        return numberA + numberB;
    }
    
}
// Subtraction class inherits operation class 
class OperationSub extends Operation
{
    public double getResult() {
        return numberA - numberB;
    }

}
// Factory class 
class OperationFactory
{
    public static Operation createOperation(int operate) {
        Operation oper = null;
        switch (operate) {
        case 1:
            oper = new OperationAdd();
            break;
        case 2:
            oper = new OperationSub();
            break;
        }
        return oper;
    }
}

2. Strategy mode: Define a series of algorithms and encapsulate each one so that they can be replaced with each other, and changes in the algorithm will not affect the customers who use the algorithm. An interface needs to be designed to provide a unified method for a series of implementation classes, and multiple implementation classes implement the interface.

The function of cash register system and settlement interface. There are activities during settlement: 1. Discounts can be 20% off, 10% off, 50% off, and an algorithm can be abstracted for discounts. 2. Full reduction, 100 off when full 300, 400 off when full 1000, etc., abstract one. The key point is to encapsulate its change point

package strategy;

public class Context {
	Strategy strategy;
	
	public Context(Strategy strategy) {
		this.strategy = strategy;
	}
	//Context interface
	public void ContextInterface() {
		/ / Call the method according to the specific policy object
		strategy.AlgorithmInterface();
	}
}

  

package strategy;

public class ConcreteStrategyA extends Strategy {

	@Override
	public void AlgorithmInterface() {
		System.out.println("Algorithm A implementation");
	}

}
package strategy;

public class ConcreteStrategyB extends Strategy {

	@Override
	public void AlgorithmInterface() {
		System.out.println("Algorithm B implementation");
	}

}
package strategy;

public class ConcreteStrategyC extends Strategy {

	@Override
	public void AlgorithmInterface() {
		System.out.println("Algorithm C implementation");
	}

}

  

package strategy;

public class Context {
	Strategy strategy;
	
	public Context(Strategy strategy) {
		this.strategy = strategy;
	}
	//Context interface
	public void ContextInterface() {
		/ / Call the method according to the specific policy object
		strategy.AlgorithmInterface();
	}
}

  

package strategy;

public class test {
	
	public static void main(String[] args) {
		Context context;
		context = new Context(new ConcreteStrategyA());
		context.ContextInterface();
		
		context = new Context(new ConcreteStrategyB());
		context.ContextInterface();
		
		context = new Context(new ConcreteStrategyB());
		context.ContextInterface();
	}
                              
}

3. Decorator mode: The decoration mode is to add some new functions to an object, and it is dynamic. It requires the decorated object and the decorated object to implement the same interface, and the decorated object holds an instance of the decorated object.

package decorator;
//Define an object interface that can dynamically add responsibilities to these objects
public abstract class Component {
	public abstract void Operation();
}

  

package decorator;

public class ConcreteComponent extends Component {

	@Override
	public void Operation() {
		System.out.println("Decorated object!!!");
	}

}

  

package decorator;

public abstract class Decorator extends Component {
	public Component component;
	
	public void setComponent(Component component) {
		this.component = component;
	}

	@Override
	public void Operation() {
		if(component != null){
			component.Operation();
		}
	}

}

  

package decorator;

public class ConcreteDecoratorA extends Decorator {

	@Override
	public void Operation() {
		super.Operation();
		System.out.println("The operation of the specific decoration object A!!");
	}
	
	public void setComponent(Component component) {
		this.component = component;
	}

}

  

package decorator;

public class ConcreteDecoratorB extends Decorator {

	@Override
	public void Operation() {
		super.Operation();
		AddedBehavior ();
		System.out.println("The operation of the specific decoration object B!!");
	}
	
	private void AddedBehavior() {
		
	}

}

  

package decorator;

public class test {
	
	public static void main(String[] args) {
		ConcreteComponent c = new ConcreteComponent();
		ConcreteDecoratorA d1 = new ConcreteDecoratorA();
		ConcreteDecoratorB d2 = new ConcreteDecoratorB();
		
		d1.setComponent(c);
		d2.setComponent(d1);
		d2.Operation();
	}			
}

  result:

    Decorated objects! ! !
    The operation of the specific decoration object A! !
    The operation of the specific decoration object B! !

4. Proxy mode (Proxy): The proxy mode is to use one more proxy class to perform some operations on the original object.

package proxy;

public interface Subject {
	public abstract void request();

}

  

package proxy;

public class RealSubject implements Subject{

	@Override
	public void request() {
		System.out.println("Real request!!!");
	}

}

  

package proxy;

public class Proxy implements Subject{
	RealSubject realSubject;

	@Override
	public void request() {
		if(realSubject == null){
			realSubject = new RealSubject();
		}
		realSubject.request();
	}
}

  

package proxy;

public class test {
	public static void main(String[] args) {
		Proxy p = new Proxy();
		p.request();
	}
}

5. Factory method pattern: 

6. Prototype mode: 

7. Template method mode:

8. Appearance mode:

9. Builder mode: It is mainly used to create some complex objects. The construction order of these objects is usually stable, but the internal construction of objects usually faces complex changes.

10. Observer mode:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324675317&siteId=291194637