Skip to content

Commit

Permalink
Merge pull request #16 from lpxxn/fix_fmt_code
Browse files Browse the repository at this point in the history
fix: fmt code
  • Loading branch information
lpxxn authored Apr 1, 2021
2 parents 33acfdc + 18de250 commit d020c46
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions behavioral/strategy.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,66 @@
trait FlyBehaviour{
trait FlyBehaviour {
fn fly(&self);
}

struct FlyWithWings;

impl FlyBehaviour for FlyWithWings {
fn fly(&self){
fn fly(&self) {
println!("i can fly!")
}
}

struct FlyNoWay;

impl FlyBehaviour for FlyNoWay{
fn fly(&self){
impl FlyBehaviour for FlyNoWay {
fn fly(&self) {
println!("i can't fly!~~")
}
}

trait Duck {
fn get_fly_behaviour(&self) -> & dyn FlyBehaviour;
fn fly(&self){
trait Duck {
fn get_fly_behaviour(&self) -> &dyn FlyBehaviour;
fn fly(&self) {
let s = self.get_fly_behaviour();
s.fly();
}
}

struct MallardDuck{
struct MallardDuck {
fly_behaviour: Box<dyn FlyBehaviour>,
}

impl Duck for MallardDuck {
fn get_fly_behaviour (&self) -> & dyn FlyBehaviour{
impl Duck for MallardDuck {
fn get_fly_behaviour(&self) -> &dyn FlyBehaviour {
return &(*self.fly_behaviour);
}
}
}

impl MallardDuck{
fn new(fly_behaviour: Box< dyn FlyBehaviour>) -> Self{
MallardDuck{ fly_behaviour }
impl MallardDuck {
fn new(fly_behaviour: Box<dyn FlyBehaviour>) -> Self {
MallardDuck { fly_behaviour }
}
fn set_fly_behaviour(& mut self, fly_behaviour: Box< dyn FlyBehaviour>){
fn set_fly_behaviour(&mut self, fly_behaviour: Box<dyn FlyBehaviour>) {
self.fly_behaviour = fly_behaviour;
}
}

struct ModelDuck{
struct ModelDuck {
fly_behaviour: Box<FlyNoWay>,
}

impl Duck for ModelDuck {
fn get_fly_behaviour (&self) -> & dyn FlyBehaviour{
impl Duck for ModelDuck {
fn get_fly_behaviour(&self) -> &dyn FlyBehaviour {
return &(*self.fly_behaviour);
}
}
}

impl ModelDuck{
fn new(fly_behaviour: Box<FlyNoWay>)-> Self{
ModelDuck{ fly_behaviour }
impl ModelDuck {
fn new(fly_behaviour: Box<FlyNoWay>) -> Self {
ModelDuck { fly_behaviour }
}
}


pub fn main() {
let mut mallard_duck = MallardDuck::new(Box::new(FlyWithWings));
mallard_duck.fly();
Expand All @@ -71,6 +70,3 @@ pub fn main() {
let model_duck = ModelDuck::new(Box::new(FlyNoWay));
model_duck.fly();
}



0 comments on commit d020c46

Please sign in to comment.