//适配器
public class Adapter implements Target{
private MySQLConnect mySQLConnect;//适配者对象
public Adapter(MySQLConnect mySQLConnect){
this.mySQLConnect=mySQLConnect;
}
@Override
public void connect() {
mySQLConnect.mysqlConnect();
}
}
public interface Target {
public void connect();
}
//适配者
public class MySQLConnect {
public void mysqlConnect(){
System.out.println("数据库链接配置");
}
}
//测试
public class Main {
public static void main(String[] args) {
Target target=new Adapter(new MySQLConnect());
target.connect();
}
}
//适配器类,其他代码和上一样
public class Adapter extends MySQLConnect implements Target{
@Override
public void connect() {
mysqlConnect();
}
}
public interface ServiceInterface {
public void print();
public void eat();
public void smell();
}
public class AbstractService implements ServiceInterface{//缺省适配器
@Override
public void print() { }
@Override
public void eat() { }
@Override
public void smell() { }
}
public class ConcreteService extends AbstractService{
@Override
public void print() {//重写打印接口
System.out.println("打印");
}
}
优点:
缺点:
使用场景:
public interface Factory {
public Product create();
}
public class MySQLFactory implements Factory{
@Override
public Product create() {
return new MySQLProduct();
}
}
public abstract class Factory {//抽象工厂
public void connect(){//对客户端隐藏调用具体产品的方法
Product product = this.create();
product.connect();
}
public abstract Product create();
}
public class MySQLFactory extends Factory{//具体工厂
@Override
public Product create() {
return new MySQLProduct();
}
}
public class Main {
public static void main(String[] args) {
Factory factory=new MySQLFactory();//可以通过配置文件配置,进行工厂方法切换
factory.connect();
}
}
优点
缺点:
使用场景:
public interface AbstractFactory {//抽象工厂
public AbstractProductA createProductA();
public AbstractProductB createProductB();
}
public class ConcreteProduct1 implements AbstractFactory{//具体工厂1
@Override
public AbstractProductA createProductA() {
return new ProductA1();
}
@Override
public AbstractProductB createProductB() {
return new ProductB1();
}
}
public class ConcreteProduct2 implements AbstractFactory{//具体工厂2
@Override
public AbstractProductA createProductA() {
return new ProductA2();
}
@Override
public AbstractProductB createProductB() {
return new ProductB2();
}
}
public interface AbstractProductA {//抽象产品A
public void printA();
}
public class ProductA1 implements AbstractProductA{//具体产品A--1
@Override
public void printA() {
System.out.println("产品A类--型号1类型");
}
}
public class ProductA2 implements AbstractProductA{//具体产品A---2
@Override
public void printA() {
System.out.println("产品A类--型号2类型");
}
}
public interface AbstractProductB {//抽象产品B
public void printB();
}
public class ProductB1 implements AbstractProductB{//具体产品B--1
@Override
public void printB() {
System.out.println("产品B类--型号1类型");
}
}
public class ProductB2 implements AbstractProductB{//具体产品B--2
@Override
public void printB() {
System.out.println("产品B类--型号2类型");
}
}
public class Main {//测试类
public static void main(String[] args) {
System.out.println("工厂1生成对应1号类型的产品族");
AbstractFactory factory1=new ConcreteProduct1();
factory1.createProductA().printA();
factory1.createProductB().printB();
System.out.println("工厂2生成对应的2号类型的产品族");
AbstractFactory factory2=new ConcreteProduct2();
factory2.createProductA().printA();
factory2.createProductB().printB();
}
}
优点:
缺点:
使用场景:
public interface Component {//抽象组件
public void operation();
}
public class ConcreteComponent implements Component{//具体组件
@Override
public void operation() {
System.out.println("组件操作");
}
}
public class Decorator implements Component{//抽象装饰器
private Component component;
public Decorator(Component component){
this.component=component;
}
@Override
public void operation() {
component.operation();
}
}
public class ConcreteDecoratorA extends Decorator{//具体装饰类
public ConcreteDecoratorA(Component component) {
super(component);
}
@Override
public void operation() {
super.operation();
addState();
}
public void addState(){
System.out.println("添加机器状态");
}
}
public class ConcreteDecoratorB extends Decorator {//具体装饰类
public ConcreteDecoratorB(Component component) {
super(component);
}
@Override
public void operation() {
super.operation();
addMoney();
}
public void addMoney(){
System.out.println("加钱,买更多机器投入操作");
}
}
public class Main {//main方法
public static void main(String[] args) {
Component component=new ConcreteComponent();
System.out.println("装饰器A-----------");
Decorator decorator=new ConcreteDecoratorA(component);
decorator.operation();
System.out.println("装饰器B---------------");
decorator=new ConcreteDecoratorB(component);
decorator.operation();
}
}
优点:
缺点:
使用场景:
@Data
public class Product {//产品类
private String A;
private String B;
}
public abstract class Builder {//建造者抽象类
protected Product product=new Product();
public abstract void buildPathA(String value);
public abstract void buildPathB(String value);
public Product getProduct(){
return this.product;
}
}
public class ConcreteBuilder extends Builder{//具体建造类
@Override
public void buildPathA(String value) {
product.setA(value);
}
@Override
public void buildPathB(String value) {
product.setB(value);
}
}
public class Director {//指挥者类
private Builder builder;
public Director(Builder builder){
this.builder=builder;
}
public Product construct(String A,String B){
builder.buildPathA(A);
builder.buildPathB(B);
return builder.getProduct();
}
}
public class Main {
public static void main(String[] args) {
Builder builder=new ConcreteBuilder();
Product product=new Director(builder).construct("属性A","属性B");
System.out.println(product.getA());
System.out.println(product.getB());
}
}
优点:
缺点:
使用场景:
public abstract class Component {//抽象组件
public abstract void add(Component component);//添加构建
public abstract void remove(Component component);//删除构建
public abstract List getChild();//获取子类
public abstract void print();//输出文件
}
public class OssLeaf extends Component{//叶子组件
private String name;
public OssLeaf(String name){
this.name=name;
}
@Override
public void add(Component component) {
System.out.println("异常处理");
}
@Override
public void remove(Component component) {
System.out.println("异常处理");
}
@Override
public List getChild() {
return null;
}
@Override
public void print() {
System.out.println("输出"+name+"文件");
}
}
public class TxtLeaf extends Component{//叶子组件
private String name;
public TxtLeaf(String name){
this.name=name;
}
@Override
public void add(Component component) {
System.out.println("异常处理");
}
@Override
public void remove(Component component) {
System.out.println("异常处理");
}
@Override
public List getChild() {
return null;
}
@Override
public void print() {
System.out.println("输出"+name+"文件类型");
}
}
public class Composite extends Component{//容器组件
//记录该组件的子组件(叶子组件、容器组件)
private List componentList=new ArrayList<>();
private String name;
public Composite(String name){
this.name=name;
}
@Override
public void add(Component component) {
componentList.add(component);
}
@Override
public void remove(Component component) {
componentList.remove(component);
}
@Override
public List getChild() {
return componentList;
}
@Override
public void print() {
System.out.println("输出"+name+"类型的文件,输出如下:");
//调用子类的方法
for (Component component : componentList) {
component.print();
}
}
}
public class Main {//测试方法
public static void main(String[] args) {
OssLeaf ossLeaf1=new OssLeaf("图片");
OssLeaf ossLeaf2=new OssLeaf("视频");
TxtLeaf txtLeaf=new TxtLeaf("txt");
TxtLeaf txtLeaf1=new TxtLeaf("docx");
TxtLeaf txtLeaf2=new TxtLeaf("pptx");
Composite composite1=new Composite("oss类型文件");
composite1.add(ossLeaf1);
composite1.add(ossLeaf2);
composite1.print();
Composite composite2=new Composite("文本类型文件");
composite2.add(txtLeaf);
composite2.add(txtLeaf1);
composite2.add(txtLeaf2);
composite2.print();
}
}
优点:
缺点:新增新的组件时,难以对组件的类型进行控制;
使用场景:
public abstract class Subject {//抽象目标
public List observerList=new ArrayList<>();//观察者集合
public void add(Observer observer){//添加观察者
observerList.add(observer);
}
public void delete(Observer observer){//删除观察者
observerList.remove(observer);
}
public abstract void notifyObserver(String name);
}
public class ConcreteSubject extends Subject{//具体目标类
@Override
public void notifyObserver(String name) {
//通知每一个观察者处理error
for (Observer observer : observerList) {
if (!((ConcreteObserver) observer).getName().equals(name)){
observer.dealError();
}
}
}
}
public interface Observer {//抽象观察者
public void dealError();//在发生error时触发
public void error(Subject subject);
}
public class ConcreteObserver implements Observer{//具体观察者
private String name;
public ConcreteObserver(String name){
this.name=name;
}
public String getName(){
return this.name;
}
//处理错误
@Override
public void dealError() {
System.out.println(name+"来处理error了");
}
//发生错误
@Override
public void error(Subject subject) {
System.out.println(name+"发生error,都来处理error");
subject.notifyObserver(name);
}
}
public class Main {
public static void main(String[] args) {
Subject subject=new ConcreteSubject();
Observer observer1=new ConcreteObserver("A");
Observer observer2=new ConcreteObserver("B");
Observer observer3=new ConcreteObserver("C");
subject.add(observer1);
subject.add(observer2);
subject.add(observer3);
observer1.error(subject);
}
}
优点:
缺点:
使用场景:
避免请求发送者和接收者耦合在一起,让多个对象都可以接收请求,将这些对象合成一条链,并且沿着这条链传递请求,直到有对象处理该请求为止;
@Data
public abstract class Handler {
protected Handler handler;//下一个处理者
protected String name;//姓名
public Handler(String name){
this.name=name;
}
//处理请求逻辑方法
public abstract void processorHandler(int money);
}
public class ConcreteHandlerA extends Handler{
public ConcreteHandlerA(String name){
super(name);
}
@Override//可以审批5000元以下
public void processorHandler(int money) {
if (money<=5000){
System.out.println(name+"进行审批,审批通过");
}else {
System.out.println(name+"无法审批金额,流转下一个审批者");
handler.processorHandler(money);
}
}
}
public class ConcreteHandlerB extends Handler{
public ConcreteHandlerB(String name){
super(name);
}
@Override//可以审批5000-20000
public void processorHandler(int money) {
if (money>5000&&money<=20000){
System.out.println(name+"进行审批,审批通过");
}else {
System.out.println(name+"无法审批金额,流转下一个审批者");
handler.processorHandler(money);
}
}
}
public class ConcreteHandlerC extends Handler{
public ConcreteHandlerC(String name){
super(name);
}
@Override//可以审批20000以上
public void processorHandler(int money) {
if (money>20000){
System.out.println(name+"进行审批,审批通过");
}else {
System.out.println(name+"无法审批金额,流转下一个审批者");
handler.processorHandler(money);
}
}
}
public class Main {
public static void main(String[] args) {
Handler handlerA=new ConcreteHandlerA("主管");
Handler handlerB=new ConcreteHandlerB("经理");
Handler handlerC=new ConcreteHandlerC("董事长");
//设置下一个处理者
handlerA.setHandler(handlerB);
handlerB.setHandler(handlerC);
handlerA.processorHandler(30000);
}
}
优点:
缺点:
使用场景: