Convert Figma logo to code with AI

alibaba logoCOLA

🥤 COLA: Clean Object-oriented & Layered Architecture

11,850
3,055
11,850
67

Top Related Projects

:fire: Seata is an easy-to-use, high-performance, open source distributed transaction solution.

28,911

🍬A set of tools that keep Java sweet.

40,335

The java implementation of Apache Dubbo. An RPC and microservice framework.

Spring Boot

🚀一个用来深入学习并实战 Spring Boot 的项目。

16,138

一个轻量级 Java 权限认证框架,让鉴权变得简单、优雅!—— 登录认证、权限认证、分布式Session会话、微服务网关鉴权、单点登录、OAuth2.0

Quick Overview

COLA (Clean Object-oriented and Layered Architecture) is an application architecture and development framework designed by Alibaba. It aims to help developers build enterprise-level applications with a clean, scalable, and maintainable structure. COLA provides a set of best practices and tools for implementing domain-driven design (DDD) principles in Java applications.

Pros

  • Promotes clean architecture and separation of concerns
  • Provides a standardized approach to building enterprise applications
  • Includes code generators and archetypes to speed up development
  • Integrates well with popular Java frameworks like Spring Boot

Cons

  • Steep learning curve for developers unfamiliar with DDD concepts
  • May be overkill for smaller projects or simple applications
  • Limited documentation and resources in English
  • Requires adherence to specific architectural patterns, which may not suit all project needs

Code Examples

  1. Defining a Command:
@Data
@EqualsAndHashCode(callSuper = false)
public class PlaceOrderCmd extends Command {
    private String customerName;
    private String productId;
    private int quantity;
}
  1. Implementing a CommandExecutor:
@Component
public class PlaceOrderCmdExe implements CommandExecutor<PlaceOrderCmd, OrderDTO> {
    @Resource
    private OrderRepository orderRepository;

    @Override
    public OrderDTO execute(PlaceOrderCmd cmd) {
        Order order = new Order(cmd.getCustomerName(), cmd.getProductId(), cmd.getQuantity());
        orderRepository.save(order);
        return OrderAssembler.toDTO(order);
    }
}
  1. Using the COLA EventBus:
@Component
public class OrderCreatedEventHandler implements EventHandler<OrderCreatedEvent> {
    @Resource
    private EventBus eventBus;

    @Override
    public void execute(OrderCreatedEvent event) {
        // Handle the event
        System.out.println("Order created: " + event.getOrderId());
        // Publish another event if needed
        eventBus.fire(new NotifyCustomerEvent(event.getOrderId()));
    }
}

Getting Started

To start using COLA in your project:

  1. Add the COLA dependencies to your pom.xml:
<dependency>
    <groupId>com.alibaba.cola</groupId>
    <artifactId>cola-component-domain-starter</artifactId>
    <version>4.3.1</version>
</dependency>
<dependency>
    <groupId>com.alibaba.cola</groupId>
    <artifactId>cola-component-exception</artifactId>
    <version>4.3.1</version>
</dependency>
  1. Use COLA archetypes to generate project structure:
mvn archetype:generate \
    -DgroupId=com.example \
    -DartifactId=my-cola-project \
    -Dversion=1.0.0-SNAPSHOT \
    -Dpackage=com.example \
    -DarchetypeGroupId=com.alibaba.cola \
    -DarchetypeArtifactId=cola-framework-archetype-web \
    -DarchetypeVersion=4.3.1
  1. Implement your domain logic following COLA's architectural guidelines.

Competitor Comparisons

:fire: Seata is an easy-to-use, high-performance, open source distributed transaction solution.

Pros of Seata

  • Focused on distributed transaction management, providing a comprehensive solution for microservices
  • Supports multiple transaction modes (AT, TCC, SAGA, XA) for different scenarios
  • Integrates well with popular frameworks and cloud-native environments

Cons of Seata

  • Steeper learning curve due to its specialized nature in distributed transactions
  • May introduce additional complexity and overhead in simpler application architectures

COLA Pros

  • Provides a complete application architecture framework for complex business systems
  • Emphasizes domain-driven design (DDD) principles and clean architecture
  • Offers a more holistic approach to application development beyond just transaction management

COLA Cons

  • Less specialized in distributed transaction management compared to Seata
  • May be overkill for smaller or less complex applications

Code Comparison

Seata (Transaction Management):

@GlobalTransactional
public void businessMethod() {
    // Business logic with distributed transaction
}

COLA (Application Architecture):

@Command
public class CreateOrderCmd extends Command {
    // Command object for creating an order
}

Both projects serve different primary purposes, with Seata focusing on distributed transactions and COLA providing a comprehensive application architecture framework. The choice between them depends on the specific needs of the project, with Seata being more suitable for complex distributed transaction scenarios and COLA for overall application structure and design.

28,911

🍬A set of tools that keep Java sweet.

Pros of Hutool

  • Lightweight and easy to use, with a focus on utility functions
  • Extensive collection of tools covering various Java development needs
  • Active community and frequent updates

Cons of Hutool

  • Less structured approach for large-scale application architecture
  • Limited support for complex business logic and domain-driven design
  • May require additional frameworks for comprehensive enterprise applications

Code Comparison

Hutool example:

String result = StrUtil.format("Hello, {}!", "World");
DateTime dateTime = DateUtil.parse("2023-05-15");
String md5 = SecureUtil.md5("password");

COLA example:

@Command
public class PlaceOrderCmd extends Command {
    private String customerId;
    private List<OrderItemDTO> orderItems;
}

public OrderDTO placeOrder(PlaceOrderCmd cmd) {
    // Business logic implementation
}

Summary

Hutool is a versatile utility library for Java developers, offering a wide range of tools for common tasks. It excels in simplifying day-to-day coding challenges but may not provide the architectural guidance needed for large-scale enterprise applications. COLA, on the other hand, focuses on providing a structured approach to building complex business applications, emphasizing clean architecture and domain-driven design principles. While Hutool is more suitable for quick solutions and smaller projects, COLA is better suited for enterprise-level applications with intricate business logic.

40,335

The java implementation of Apache Dubbo. An RPC and microservice framework.

Pros of Dubbo

  • Mature and widely adopted high-performance RPC framework
  • Extensive service governance features (load balancing, routing, clustering)
  • Strong community support and active development

Cons of Dubbo

  • Primarily focused on RPC, less comprehensive for full application architecture
  • Steeper learning curve for developers new to distributed systems
  • More complex setup and configuration compared to simpler frameworks

Code Comparison

COLA (Clean Object-Oriented and Layered Architecture):

@Component
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private CustomerMapper customerMapper;

    public CustomerDTO getCustomer(String customerId) {
        CustomerDO customerDO = customerMapper.getById(customerId);
        return CustomerAssembler.toDTO(customerDO);
    }
}

Dubbo:

@Service(version = "1.0.0")
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    public CustomerDTO getCustomer(String customerId) {
        CustomerDO customerDO = customerMapper.getById(customerId);
        return CustomerAssembler.toDTO(customerDO);
    }
}

While both examples show similar service implementations, Dubbo uses its own @Service annotation for RPC service exposure, whereas COLA follows a more standard Spring-based approach. Dubbo's focus on RPC is evident, while COLA provides a broader architectural framework.

Spring Boot

Pros of Spring Boot

  • Extensive ecosystem with a wide range of starter dependencies
  • Robust auto-configuration capabilities
  • Large and active community support

Cons of Spring Boot

  • Can be heavyweight for simple applications
  • Learning curve for advanced features and customizations

Code Comparison

Spring Boot:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

COLA:

@ColaApplication
public class Application {
    public static void main(String[] args) {
        ColaApplication.run(Application.class, args);
    }
}

Key Differences

  • COLA focuses on Clean Architecture and Domain-Driven Design principles
  • Spring Boot provides a more general-purpose framework for various application types
  • COLA offers a structured approach to organizing business logic and application layers
  • Spring Boot has more extensive documentation and third-party integrations

Use Cases

  • Spring Boot: Ideal for rapid prototyping, microservices, and enterprise applications
  • COLA: Well-suited for complex domain logic and large-scale enterprise applications

Community and Support

  • Spring Boot has a larger community and more resources available
  • COLA is backed by Alibaba and gaining traction in enterprise environments

Learning Curve

  • Spring Boot: Easier to get started, but can become complex with advanced features
  • COLA: Requires understanding of Clean Architecture and DDD concepts

🚀一个用来深入学习并实战 Spring Boot 的项目。

Pros of spring-boot-demo

  • More comprehensive coverage of Spring Boot features and integrations
  • Easier to understand for beginners due to its modular structure
  • Regularly updated with new Spring Boot versions and features

Cons of spring-boot-demo

  • Lacks a cohesive architectural framework compared to COLA
  • May not be as suitable for large-scale enterprise applications
  • Does not provide as much guidance on application layering and design principles

Code Comparison

COLA example (Domain-Driven Design approach):

@Data
@Entity
public class Customer extends BaseEntity {
    private String name;
    private String email;
    // ...
}

spring-boot-demo example (Simple POJO approach):

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Long id;
    private String name;
    private Integer age;
    // ...
}

Summary

COLA provides a more structured approach for building enterprise applications using Domain-Driven Design principles, while spring-boot-demo offers a wider range of Spring Boot examples and integrations. COLA is better suited for large-scale projects requiring a robust architecture, whereas spring-boot-demo is more accessible for developers learning Spring Boot or implementing smaller applications.

16,138

一个轻量级 Java 权限认证框架,让鉴权变得简单、优雅!—— 登录认证、权限认证、分布式Session会话、微服务网关鉴权、单点登录、OAuth2.0

Pros of Sa-Token

  • Lightweight and easy to integrate, focusing specifically on authentication and authorization
  • Extensive documentation and examples for various use cases
  • Supports multiple login methods and session management

Cons of Sa-Token

  • Limited scope compared to COLA's comprehensive architecture
  • May require additional libraries for full application development
  • Less emphasis on domain-driven design principles

Code Comparison

Sa-Token authentication example:

// Login
StpUtil.login(10001);

// Get login ID
String loginId = StpUtil.getLoginId();

// Check login status
boolean isLogin = StpUtil.isLogin();

COLA architecture example:

@Component
public class CustomerServiceImpl implements CustomerServiceI {
    @Resource
    private CustomerMapper customerMapper;

    public CustomerDTO getCustomer(String customerId) {
        Customer customer = customerMapper.getById(customerId);
        return CustomerAssembler.toDTO(customer);
    }
}

Sa-Token focuses on authentication and authorization, providing a simple API for managing user sessions. COLA, on the other hand, offers a more comprehensive architecture for building enterprise applications, emphasizing domain-driven design and clean architecture principles.

While Sa-Token excels in its specific domain, COLA provides a broader framework for structuring entire applications. The choice between the two depends on the project's scope and requirements.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

🥤 COLA v5

Fast CI Multiply Java versions CI License Java support Maven Central GitHub Releases GitHub Stars GitHub Forks user repos GitHub issues GitHub Contributors gitpod: Ready to Code

COLA 是 Clean Object-Oriented and Layered Architecture的缩写,代表“整洁面向对象分层架构”。 目前COLA已经发展到COLA v5。

COLA分为两个部分,COLA架构和COLA组件。

一、COLA架构

COLA 概述

架构的意义 就是 要素结构:

  • 要素 是 组成架构的重要元素;
  • 结构 是 要素之间的关系。

而 应用架构的意义 就在于

  • 定义一套良好的结构;
  • 治理应用复杂度,降低系统熵值;
  • 从随心所欲的混乱状态,走向井井有条的有序状态。

arch why

COLA架构就是为此而生,其核心职责就是定义良好的应用结构,提供最佳应用架构的最佳实践。通过不断探索,我们发现良好的分层结构,良好的包结构定义,可以帮助我们治理混乱不堪的业务应用系统。

cure

经过多次迭代,我们定义出了相对稳定、可靠的应用架构:

cola arch

COLA Archetypes

好的应用架构,都遵循一些共同模式,不管是六边形架构、洋葱圈架构、整洁架构、还是COLA架构,都提倡以业务为核心,解耦外部依赖,分离业务复杂度和技术复杂度等。

COLA架构区别于这些架构的地方,在于除了思想之外,我们还提供了可落地的工具和实践指导。

为了能够快速创建满足COLA架构的应用,我们提供了两个archetype,位于cola-archetypes目录下:

  1. cola-archetype-service:用来创建纯后端服务的archetype。
  2. cola-archetype-web:用来创建adapter和后端服务一体的web应用archetype。

二、COLA组件

此外,我们还提供了一些非常有用的通用组件,这些组件可以帮助我们提升研发效率。

这些功能组件被收拢在cola-components目录下面。到目前为止,我们已经沉淀了以下组件:

组件名称功能依赖
cola-component-dto定义了DTO格式,包括分页无
cola-component-exception定义了异常格式,
主要有BizException和SysException
无
cola-component-statemachine状态机组件无
cola-component-domain-starterSpring托管的领域实体组件无
cola-component-catchlog-starter异常处理和日志组件exception、dto组件
cola-component-extension-starter扩展点组件无
cola-component-test-container测试容器组件无

三、如何使用COLA

1. 创建应用

执行以下命令:

mvn archetype:generate \
    -DgroupId=com.alibaba.cola.demo.web \
    -DartifactId=demo-web \
    -Dversion=1.0.0-SNAPSHOT \
    -Dpackage=com.alibaba.demo \
    -DarchetypeArtifactId=cola-framework-archetype-web \
    -DarchetypeGroupId=com.alibaba.cola \
    -DarchetypeVersion=5.0.0

命令执行成功的话,会看到如下的应用代码结构:

demo struture

2. 运行应用

  • 在项目目录下运行mvn install(如果不想运行测试,可以加上-DskipTests参数)。
  • 进入start目录,执行mvn spring-boot:run。
    运行成功的话,可以看到SpringBoot启动成功的界面。
  • 生成的应用中,已经实现了一个简单的Rest请求,可以在浏览器中输入 http://localhost:8080/helloworld 进行测试。

如果要生成不是web工程而是service工程也类似,执行的是下面的命令:

mvn archetype:generate \
    -DgroupId=com.alibaba.cola.demo.service \
    -DartifactId=demo-service \
    -Dversion=1.0.0-SNAPSHOT \
    -Dpackage=com.alibaba.demo \
    -DarchetypeArtifactId=cola-framework-archetype-service \
    -DarchetypeGroupId=com.alibaba.cola \
    -DarchetypeVersion=5.0.0

版本迭代

5.0.0 版本

  1. 支持jdk17和SpringBoot 3.x
  2. 增加cola-archetype-light,支持新的基于package轻量级分层架构
  3. 增加cola-component-unittest组件,支持新的单元测试
  4. 增强cola-component-test-container组件,支持Junit5的Extension

4.0.0 版本

https://blog.csdn.net/significantfrank/article/details/110934799

3.1.0 版本

https://blog.csdn.net/significantfrank/article/details/109529311

  1. 进一步简化了cola-core,只保留了扩展能力。
  2. 将exception从cola-core移入到cola-common。
  3. 对archetype中的分包逻辑进行重构,改成按照domain做划分。
  4. 将cola-archetype-web中的controller改名为adapter,为了呼应六边形架构的命名。

3.0.0 版本

https://blog.csdn.net/significantfrank/article/details/106976804

2.0.0 版本

https://blog.csdn.net/significantfrank/article/details/100074716

1.0.0 版本

https://blog.csdn.net/significantfrank/article/details/85785565