You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

11 KiB

濒危动物爬虫系统类图

整体架构类图

classDiagram
    %% MVC架构
    class CrawlerController {
        -CrawlerView view
        -CrawlerContext context
        -List~Animal~ animals
        -CommandManager commandManager
        +start()
        +processCommand(String input)
        +setDataSource(String sourceType)
        +crawlData()
        +listData()
        +saveData(String filePath)
        +visualizeData()
    }
    
    class CrawlerView {
        +showWelcome()
        +showHelp()
        +showDataSources()
        +showAnimals(List~Animal~ animals)
        +showError(String message)
        +showExit()
    }
    
    class Animal {
        -String name
        -String scientificName
        -String category
        -String status
        -String habitat
        -String description
        -int population
        +getName()
        +setName(String name)
        +getScientificName()
        +setScientificName(String scientificName)
        +getCategory()
        +setCategory(String category)
        +getStatus()
        +setStatus(String status)
        +getHabitat()
        +setHabitat(String habitat)
        +getDescription()
        +setDescription(String description)
        +getPopulation()
        +setPopulation(int population)
        +toString()
    }
    
    %% 策略模式
    class CrawlerStrategy {
        <<interface>>
        +getDataSourceName() String
        +getDataSourceUrl() String
        +crawl() List~Animal~
    }
    
    class CrawlerContext {
        -CrawlerStrategy strategy
        +setStrategy(CrawlerStrategy strategy)
        +getStrategy() CrawlerStrategy
        +getStrategyName() String
        +getStrategyUrl() String
        +executeStrategy() List~Animal~
    }
    
    class ChinaRedListStrategy {
        +getDataSourceName() String
        +getDataSourceUrl() String
        +crawl() List~Animal~
    }
    
    class WWFStrategy {
        +getDataSourceName() String
        +getDataSourceUrl() String
        +crawl() List~Animal~
    }
    
    class IUCNStrategy {
        +getDataSourceName() String
        +getDataSourceUrl() String
        +crawl() List~Animal~
    }
    
    %% Command模式
    class Command {
        <<interface>>
        +execute()
        +getName() String
        +getDescription() String
    }
    
    class CommandManager {
        -List~Command~ commands
        +addCommand(Command command)
        +executeAll()
        +clear()
    }
    
    class CrawlCommand {
        -CrawlerContext context
        -List~Animal~ result
        +execute()
        +getResult() List~Animal~
        +getName() String
        +getDescription() String
    }
    
    class SaveCommand {
        -List~Animal~ animals
        -String filePath
        +execute()
        +getName() String
        +getDescription() String
    }
    
    class ListCommand {
        -List~Animal~ animals
        +execute()
        +getName() String
        +getDescription() String
    }
    
    class SetStrategyCommand {
        -CrawlerContext context
        -String sourceType
        +execute()
        +getName() String
        +getDescription() String
    }
    
    class VisualizeCommand {
        -List~Animal~ animals
        +execute()
        +getName() String
        +getDescription() String
    }
    
    %% 异常体系
    class CrawlerException {
        +CrawlerException(String message)
        +CrawlerException(String message, Throwable cause)
    }
    
    class NetworkException {
        +NetworkException(String message)
        +NetworkException(String message, Throwable cause)
    }
    
    class ParseException {
        +ParseException(String message)
        +ParseException(String message, Throwable cause)
    }
    
    class StorageException {
        +StorageException(String message)
        +StorageException(String message, Throwable cause)
    }
    
    class InvalidCommandException {
        +InvalidCommandException(String message)
    }
    
    class DataSourceException {
        +DataSourceException(String message)
        +DataSourceException(String message, Throwable cause)
    }
    
    %% 可视化
    class AnimalVisualization {
        +createStatusDistributionChart(List~Animal~ animals)
        +createPopulationChart(List~Animal~ animals)
    }
    
    %% 主入口
    class Main {
        +main(String[] args)
    }
    
    %% 关系定义
    CrawlerController --> CrawlerView : uses
    CrawlerController --> CrawlerContext : uses
    CrawlerController --> CommandManager : uses
    CrawlerController --> Animal : manages
    
    CrawlerContext --> CrawlerStrategy : uses
    CrawlerStrategy <|.. ChinaRedListStrategy : implements
    CrawlerStrategy <|.. WWFStrategy : implements
    CrawlerStrategy <|.. IUCNStrategy : implements
    
    Command <|.. CrawlCommand : implements
    Command <|.. SaveCommand : implements
    Command <|.. ListCommand : implements
    Command <|.. SetStrategyCommand : implements
    Command <|.. VisualizeCommand : implements
    
    CommandManager --> Command : manages
    
    CrawlCommand --> CrawlerContext : uses
    CrawlCommand --> Animal : produces
    SaveCommand --> Animal : uses
    ListCommand --> Animal : uses
    SetStrategyCommand --> CrawlerContext : uses
    VisualizeCommand --> Animal : uses
    VisualizeCommand --> AnimalVisualization : uses
    
    CrawlerException <|-- NetworkException : extends
    CrawlerException <|-- ParseException : extends
    CrawlerException <|-- StorageException : extends
    CrawlerException <|-- InvalidCommandException : extends
    CrawlerException <|-- DataSourceException : extends
    
    Main --> CrawlerController : creates

设计模式详解

1. MVC架构

classDiagram
    class Model {
        <<package>>
        Animal
    }
    
    class View {
        <<package>>
        CrawlerView
    }
    
    class Controller {
        <<package>>
        CrawlerController
    }
    
    Model <-- Controller : manipulates
    View <-- Controller : updates
    Model <-- View : displays

说明:

  • Model (模型层)Animal 类,负责数据存储和管理
  • View (视图层)CrawlerView 类,负责用户界面显示
  • Controller (控制器层)CrawlerController 类,负责业务逻辑处理

2. 策略模式 (Strategy Pattern)

classDiagram
    class CrawlerStrategy {
        <<interface>>
        +getDataSourceName() String
        +getDataSourceUrl() String
        +crawl() List~Animal~
    }
    
    class CrawlerContext {
        -CrawlerStrategy strategy
        +setStrategy(CrawlerStrategy strategy)
        +executeStrategy() List~Animal~
    }
    
    class ChinaRedListStrategy {
        +crawl() List~Animal~
    }
    
    class WWFStrategy {
        +crawl() List~Animal~
    }
    
    class IUCNStrategy {
        +crawl() List~Animal~
    }
    
    CrawlerStrategy <|.. ChinaRedListStrategy
    CrawlerStrategy <|.. WWFStrategy
    CrawlerStrategy <|.. IUCNStrategy
    CrawlerContext --> CrawlerStrategy

说明:

  • 策略接口CrawlerStrategy 定义爬虫策略的标准行为
  • 上下文类CrawlerContext 持有策略对象,可以动态切换策略
  • 具体策略ChinaRedListStrategyWWFStrategyIUCNStrategy 实现不同网站的爬取策略

3. Command模式 (命令模式)

classDiagram
    class Command {
        <<interface>>
        +execute()
        +getName() String
        +getDescription() String
    }
    
    class CommandManager {
        -List~Command~ commands
        +addCommand(Command command)
        +executeAll()
    }
    
    class CrawlCommand {
        -CrawlerContext context
        +execute()
    }
    
    class SaveCommand {
        -List~Animal~ animals
        +execute()
    }
    
    class ListCommand {
        -List~Animal~ animals
        +execute()
    }
    
    class VisualizeCommand {
        -List~Animal~ animals
        +execute()
    }
    
    Command <|.. CrawlCommand
    Command <|.. SaveCommand
    Command <|.. ListCommand
    Command <|.. VisualizeCommand
    CommandManager --> Command

说明:

  • 命令接口Command 定义命令的标准行为
  • 命令管理器CommandManager 管理和执行命令
  • 具体命令CrawlCommandSaveCommandListCommandVisualizeCommand 实现具体操作

4. 异常体系

classDiagram
    class Exception {
        <<Java标准类>>
    }
    
    class CrawlerException {
        +CrawlerException(String message)
        +CrawlerException(String message, Throwable cause)
    }
    
    class NetworkException {
        +NetworkException(String message)
    }
    
    class ParseException {
        +ParseException(String message)
    }
    
    class StorageException {
        +StorageException(String message)
    }
    
    class InvalidCommandException {
        +InvalidCommandException(String message)
    }
    
    class DataSourceException {
        +DataSourceException(String message)
    }
    
    Exception <|-- CrawlerException
    CrawlerException <|-- NetworkException
    CrawlerException <|-- ParseException
    CrawlerException <|-- StorageException
    CrawlerException <|-- InvalidCommandException
    CrawlerException <|-- DataSourceException

说明:

  • 基类CrawlerException 继承自Java标准 Exception
  • 子类异常
    • NetworkException:网络相关异常
    • ParseException:数据解析异常
    • StorageException:文件存储异常
    • InvalidCommandException:无效命令异常
    • DataSourceException:数据源异常

包结构图

graph TB
    subgraph com.animal
        crawler[crawler包]
        controller[controller包]
        view[view包]
        model[model包]
        strategy[strategy包]
        command[command包]
        exception[exception包]
        visualization[visualization包]
    end
    
    crawler --> Main
    controller --> CrawlerController
    view --> CrawlerView
    model --> Animal
    strategy --> CrawlerStrategy
    strategy --> CrawlerContext
    strategy --> ChinaRedListStrategy
    strategy --> WWFStrategy
    strategy --> IUCNStrategy
    command --> Command
    command --> CommandManager
    command --> CrawlCommand
    command --> SaveCommand
    command --> ListCommand
    command --> VisualizeCommand
    exception --> CrawlerException
    exception --> NetworkException
    exception --> ParseException
    exception --> StorageException
    visualization --> AnimalVisualization

类之间的关系总结

关系类型 类A 类B 说明
依赖 CrawlerController CrawlerView 控制器使用视图显示信息
依赖 CrawlerController CrawlerContext 控制器使用上下文管理策略
依赖 CrawlerController CommandManager 控制器使用命令管理器
实现 ChinaRedListStrategy CrawlerStrategy 实现爬虫策略接口
实现 WWFStrategy CrawlerStrategy 实现爬虫策略接口
实现 IUCNStrategy CrawlerStrategy 实现爬虫策略接口
实现 CrawlCommand Command 实现命令接口
实现 SaveCommand Command 实现命令接口
继承 NetworkException CrawlerException 继承基础异常类
继承 ParseException CrawlerException 继承基础异常类
关联 CrawlerContext CrawlerStrategy 上下文持有策略对象
关联 CrawlCommand CrawlerContext 命令使用上下文对象