当前位置: 首页 > news >正文

组合模式介绍和经典实现

组合模式(Composite Pattern)是一种结构型设计模式,它允许你将对象组合成树形结构来表示部分-整体层次结构。通过组合模式,客户端可以一致地处理单个对象和组合对象,无需关心对象的具体类型。这使得代码更加简洁、灵活和可扩展。

组合模式的说明

角色
  1. Component(组件)

    • 定义了树中对象的公共接口。
    • 可以是抽象类或接口,声明了所有子类共有的方法。
  2. Leaf(叶子节点)

    • 表示叶子节点,即没有子节点的对象。
    • 实现了 Component 接口,但不包含子节点。
  3. Composite(组合节点)

    • 表示组合节点,即包含子节点的对象。
    • 实现了 Component 接口,并且包含和管理子节点。
  4. Client(客户端)

    • 通过 Component 接口操作对象,无需关心对象的具体类型。

在 OAuth2 中 grantType 授权中的应用案例

在 OAuth2 中,grantType 表示授权类型,常见的授权类型包括 authorization_codepasswordclient_credentialsrefresh_token。我们可以使用组合模式来管理这些授权类型,使得客户端可以一致地处理不同类型的授权请求。

经典实现

假设我们有一个 OAuth2 授权服务器,需要支持多种授权类型。我们可以使用组合模式来管理这些授权类型。

import java.util.ArrayList;
import java.util.List;

// Component(组件)
public interface GrantType {
    void process();
    void add(GrantType grantType); // 只有组合节点需要实现
    void remove(GrantType grantType); // 只有组合节点需要实现
    GrantType getGrantType(int index); // 只有组合节点需要实现
}

// Leaf(叶子节点)
public class AuthorizationCodeGrant implements GrantType {
    @Override
    public void process() {
        System.out.println("Processing Authorization Code Grant");
    }

    @Override
    public void add(GrantType grantType) {
        throw new UnsupportedOperationException("Not supported in AuthorizationCodeGrant");
    }

    @Override
    public void remove(GrantType grantType) {
        throw new UnsupportedOperationException("Not supported in AuthorizationCodeGrant");
    }

    @Override
    public GrantType getGrantType(int index) {
        throw new UnsupportedOperationException("Not supported in AuthorizationCodeGrant");
    }
}

public class PasswordGrant implements GrantType {
    @Override
    public void process() {
        System.out.println("Processing Password Grant");
    }

    @Override
    public void add(GrantType grantType) {
        throw new UnsupportedOperationException("Not supported in PasswordGrant");
    }

    @Override
    public void remove(GrantType grantType) {
        throw new UnsupportedOperationException("Not supported in PasswordGrant");
    }

    @Override
    public GrantType getGrantType(int index) {
        throw new UnsupportedOperationException("Not supported in PasswordGrant");
    }
}

public class ClientCredentialsGrant implements GrantType {
    @Override
    public void process() {
        System.out.println("Processing Client Credentials Grant");
    }

    @Override
    public void add(GrantType grantType) {
        throw new UnsupportedOperationException("Not supported in ClientCredentialsGrant");
    }

    @Override
    public void remove(GrantType grantType) {
        throw new UnsupportedOperationException("Not supported in ClientCredentialsGrant");
    }

    @Override
    public GrantType getGrantType(int index) {
        throw new UnsupportedOperationException("Not supported in ClientCredentialsGrant");
    }
}

public class RefreshTokenGrant implements GrantType {
    @Override
    public void process() {
        System.out.println("Processing Refresh Token Grant");
    }

    @Override
    public void add(GrantType grantType) {
        throw new UnsupportedOperationException("Not supported in RefreshTokenGrant");
    }

    @Override
    public void remove(GrantType grantType) {
        throw new UnsupportedOperationException("Not supported in RefreshTokenGrant");
    }

    @Override
    public GrantType getGrantType(int index) {
        throw new UnsupportedOperationException("Not supported in RefreshTokenGrant");
    }
}

// Composite(组合节点)
public class CompositeGrantType implements GrantType {
    private List<GrantType> grantTypes = new ArrayList<>();

    @Override
    public void process() {
        for (GrantType grantType : grantTypes) {
            grantType.process();
        }
    }

    @Override
    public void add(GrantType grantType) {
        grantTypes.add(grantType);
    }

    @Override
    public void remove(GrantType grantType) {
        grantTypes.remove(grantType);
    }

    @Override
    public GrantType getGrantType(int index) {
        return grantTypes.get(index);
    }
}

// Client(客户端)
public class OAuth2CompositeExample {

    public static void main(String[] args) {
        // 创建授权类型对象
        GrantType authorizationCodeGrant = new AuthorizationCodeGrant();
        GrantType passwordGrant = new PasswordGrant();
        GrantType clientCredentialsGrant = new ClientCredentialsGrant();
        GrantType refreshTokenGrant = new RefreshTokenGrant();

        // 创建组合授权类型
        CompositeGrantType compositeGrantType = new CompositeGrantType();
        compositeGrantType.add(authorizationCodeGrant);
        compositeGrantType.add(passwordGrant);
        compositeGrantType.add(clientCredentialsGrant);
        compositeGrantType.add(refreshTokenGrant);

        // 处理授权请求
        compositeGrantType.process();
    }
}

运行结果

Processing Authorization Code Grant
Processing Password Grant
Processing Client Credentials Grant
Processing Refresh Token Grant

解释

  1. Component(组件)

    • GrantType 接口定义了所有授权类型的公共方法,包括 processaddremovegetGrantType
  2. Leaf(叶子节点)

    • AuthorizationCodeGrantPasswordGrantClientCredentialsGrantRefreshTokenGrant 实现了 GrantType 接口,表示具体的授权类型。对于不支持的操作,抛出 UnsupportedOperationException
  3. Composite(组合节点)

    • CompositeGrantType 实现了 GrantType 接口,并且包含和管理子授权类型。process 方法会递归地调用子授权类型的 process 方法。
  4. Client(客户端)

    • 客户端代码通过 compositeGrantType 处理授权请求,而不需要关心具体授权类型的类型。这使得客户端代码更加简洁和一致。

应用场景

在 OAuth2 授权服务器中,组合模式可以用于以下场景:

  1. 多授权类型支持

    • 通过组合模式,可以轻松地支持多种授权类型,并且可以在运行时动态地添加或删除授权类型。
  2. 权限管理

    • 可以将不同的授权类型组合成一个复杂的权限结构,使得权限管理更加灵活和可扩展。
  3. 配置管理

    • 可以通过组合模式来管理授权类型的配置,使得配置更加灵活和可维护。

通过这些经典实现,可以看到组合模式在 OAuth2 授权服务器中的强大之处,它不仅简化了客户端代码,还提高了系统的可维护性和扩展性。

相关文章:

  • K8S学习之基础五十七:部署代码扫描工具sonarqube
  • 【React】基础版React + Redux实现教程,自定义redux库,Redux Toolkit教程
  • 【Linux】调试器——gdb使用
  • 信而泰PFC/ECN流量测试方案:打造智能无损网络的关键利器
  • TCP的长连接和短连接,以及它们分别适用于什么场合
  • 深入理解椭圆曲线密码学(ECC)与区块链加密
  • DeFi监管进退两难,技术破局的希望与挑战
  • Centos7 安装 Nginx
  • Postman 版本信息速查:快速定位版本号
  • WPS宏开发手册——JSA语法练习
  • Appium中元素定位之一个元素定位API
  • 基于EFISH-SBC-RK3576的无人机智能飞控与数据存储方案
  • Python网络编程实战:多线程素数服务与简易爬虫开发
  • linux input子系统深度剖析
  • vue3的v-model
  • html5 有哪些新特性
  • Stereolabs ZED Box Mini:机器人与自动化领域的人工智能视觉新选择
  • 【力扣hot100题】(008)找到字符串中所有字母异位词
  • IP报文格式
  • 国科云:浅谈DNS在IPv6改造过程中的重要性
  • 公司网站建设价格多少/南京seo网站优化
  • 专业的网站建设费用/全专业优化公司
  • 资料库网站源码/免费网上销售平台
  • 网站建设多少钱/百度关键词排名靠前
  • 怎么查看wordpress版本号/在线seo短视频
  • 律师网站建设建议/郑州seo培训