«前の日記(2017-08-19) 最新 次の日記(2017-08-21)» 編集

ToDo:

  • 99 深呼吸して、おちけつ (~12/31 あと38日)
  • 98 毎日目標をたてる (~12/31 あと38日)
  • 97 買物は行動を変えるもの・ことに限る(~12/31 あと38日)

ɥozɐʞıɥ


2017-08-20 嵐の翌日 [長年日記]

_ [モフ太] 朝散歩

嵐の翌日です。

モフ太さんぽ
モフ太さんぽ posted by (C)ひかぞぉ

_ [Rust] Command Pattern

Command pattern - WikipediaをRustで実験。本当はもっtGenericsを使ったほうが良さそうなのだが。

trait Command {

fn execute(&self);

}

struct Switch<'a>{

closed_command: &'a Command,

opened_command: &'a Command

}

impl <'a>Switch<'a>{

fn new(cls: &'a Command, opn: &'a Command) -> Self{

Switch{

closed_command : cls,

opened_command : opn,

}

}

fn close(&self){

self.closed_command.execute();

}

fn open(&self){

self.opened_command.execute();

}

}

trait Switchable{

fn power_on(&self);

fn power_off(&self);

}

struct Light;

impl Switchable for Light{

fn power_on(&self){

println!("The light is on");

}

fn power_off(&self){

println!("The light is off");

}

}

struct CloseSwitchCommand<'a>{

switchable: &'a Switchable

}

impl<'a> CloseSwitchCommand<'a>{

fn new(cmd: &'a Switchable) -> Self{

CloseSwitchCommand{

switchable : cmd,

}

}

}

impl<'a> Command for CloseSwitchCommand<'a>{

fn execute(&self){

self.switchable.power_on();

}

}

impl<'a> OpenSwitchCommand<'a>{

fn new(cmd: &'a Switchable) -> Self{

OpenSwitchCommand{

switchable : cmd

}

}

}

impl<'a> Command for OpenSwitchCommand<'a>{

fn execute(&self) {

self.switchable.power_off();

}

}

use std::env;

use std::str;

fn main(){

let args: Vec<String> = env::args().collect();

let lamp = Light;

let switch_close = CloseSwitchCommand::new(&lamp);

let switch_open = OpenSwitchCommand::new(&lamp);

let switch = Switch::new(&switch_close,&switch_open);

let s: &str = &args[1];

match s{

"ON" => switch.open(),

"OFF" => switch.close(),

_ => println!("Argument ON or OFF is required!"),

}

}

_ [モフ太] 夕方散歩

モフ太さんぽ
モフ太さんぽ posted by (C)ひかぞぉ



2002|09|10|11|12|
2003|01|02|03|04|05|06|07|08|09|10|11|12|
2004|01|02|03|04|05|06|07|08|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|11|12|
2006|01|02|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|08|09|10|11|12|
2009|01|02|03|04|05|06|07|08|09|10|11|12|
2010|01|02|03|04|05|06|07|08|09|10|11|12|
2011|01|02|03|04|05|06|07|08|09|10|11|12|
2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|
«前の日記(2017-08-19) 最新 次の日記(2017-08-21)» 編集