RustMagazine / rust_magazine_2021

RustMagazine 2021 期刊 (已完结)
https://rustmagazine.github.io/rust_magazine_2021/
MIT License
1.2k stars 113 forks source link

「系列」Rust设计模式 | 工厂模式 - Rust精选 #178

Closed utterances-bot closed 1 year ago

utterances-bot commented 1 year ago

「系列」Rust设计模式 | 工厂模式 - Rust精选

The roots aren't deep but the seeds are planted!

https://rustmagazine.github.io/rust_magazine_2021/chapter_3/rust-design-pattern-factory.html

xylo987 commented 1 year ago

写的什么烂狗屎代码,误人子弟

ZhangHanDong commented 1 year ago

@xylo987 你留点口德吧

xylo987 commented 1 year ago
use std::vec::Vec;
use rusqlite::{Connection, Result};
use rusqlite::NO_PARAMS;
use std::panic;

mod rdbms {
    pub trait DBConnectorTrait {
        fn print(&self);
    }

    pub trait DBConnectorFactoryTrait<T> {
        fn make_db_connector(&self, params: T) -> Box<dyn DBConnectorTrait>;
    }

    pub struct SqliteDBConnector {
        path: String
    }
    impl DBConnectorTrait 
    for SqliteDBConnector {
        fn print(&self) {
            println!("sqlite3 attributes {}", self.path);
        }
    }

    pub struct MySQLDBConnector {
        host: String,
        port: u16,
        username: String,
        password: String,
        charset: String,
        db: String
    }
    impl DBConnectorTrait 
    for MySQLDBConnector {
        fn print(&self) {
            println!("mysql attributes {}, {}, {}, {}, {}, {}", self.host, self.port, self.username, self.password, self.charset, self.db);
        }
    }

    pub struct DBConnectorFactory;
    impl DBConnectorFactoryTrait<String> 
    for DBConnectorFactory  {
        fn make_db_connector(&self, path: String) -> Box<dyn DBConnectorTrait> {
            Box::new(SqliteDBConnector{
                path: path.to_string()
            })
        }
    }
    impl DBConnectorFactoryTrait<(String, u16, String, String, String, String)> 
    for DBConnectorFactory  {
        fn make_db_connector(&self, mysql_connect_tuple: (String, u16, String, String, String, String)) -> Box<dyn DBConnectorTrait> {
            Box::new(MySQLDBConnector{
                host: mysql_connect_tuple.0,
                port: mysql_connect_tuple.1,
                username: mysql_connect_tuple.2,
                password: mysql_connect_tuple.3,
                charset: mysql_connect_tuple.4,
                db: mysql_connect_tuple.5
            })
        }   }
}

use std::io;

fn main() -> io::Result<()> {
    use rdbms::DBConnectorFactoryTrait;
    use rdbms::DBConnectorFactory;

    let factory = DBConnectorFactory{};
    let sqlite_connector = factory.make_db_connector(".".to_string());
    let mysql_connector = factory.make_db_connector(("localhost".to_string(), 
                                                     1433, 
                                                     "root".to_string(), 
                                                     "123".to_string(), 
                                                     "utf8".to_string(), 
                                                     "mysql".to_string()));

    sqlite_connector.print();
    mysql_connector.print();

    println!("按任意键退出...");
    let mut buffer = String::new();
    let stdin = io::stdin();
    stdin.read_line(&mut buffer)?;

    Ok(())
}
xylo987 commented 1 year ago

你也不看看我那是几点发的,只差把写那个文章的人打一顿,还口德,没动手就是好事了,君子动手不动口。我已经是宽宏大量到极限了。

ZhangHanDong commented 1 year ago

@xylo987 文章有好有坏很正常,遇到不适合的不看就行,你又骂又想打,至于吗?你是不是有啥心理疾病呢?