• 组合模式 rust和java的实现


    组合模式

    组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。

    这种模式创建了一个包含自己对象组的类。该类提供了修改相同对象组的方式。

    介绍

    • 意图:将对象组合成树形结构以表示"部分-整体"的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

    • 主要解决:它在我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以像处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。

    • 何时使用:、
      1、您想表示对象的部分-整体层次结构(树形结构)。 2、您希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

    如何解决:树枝和叶子实现统一接口,树枝内部组合该接口。

    应用实例

    1. 算术表达式包括操作数、操作符和另一个操作数,其中,另一个操作数也可以是操作数、操作符和另一个操作数。

    优点: 1、高层模块调用简单。 2、节点自由增加。

    缺点:在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。

    使用场景:部分、整体场景,如树形菜单,文件、文件夹的管理。

    实现

    我们有一个类 Employee,该类被当作组合模型类。CompositePatternDemo 类使用 Employee 类来添加部门层次结构,并打印所有员工,这样我们就可以实现不同的的部门进行自由组合,实现不同部门之间的即插即用。

    组合模式的 UML 图

    java

    步骤 1
    创建 Employee 类,该类带有 Employee 对象的列表。
    在这里插入图片描述

    Employee.java

    import java.util.ArrayList;
    import java.util.List;
     
    public class Employee {
       private String name;
       private String dept;
       private int salary;
       private List<Employee> subordinates;
     
       //构造函数
       public Employee(String name,String dept, int sal) {
          this.name = name;
          this.dept = dept;
          this.salary = sal;
          subordinates = new ArrayList<Employee>();
       }
     
       public void add(Employee e) {
          subordinates.add(e);
       }
     
       public void remove(Employee e) {
          subordinates.remove(e);
       }
     
       public List<Employee> getSubordinates(){
         return subordinates;
       }
     
       public String toString(){
          return ("Employee :[ Name : "+ name 
          +", dept : "+ dept + ", salary :"
          + salary+" ]");
       }   
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    步骤 2
    使用 Employee 类来创建和打印员工的层次结构。

    CompositePatternDemo.java

    public class CompositePatternDemo {
       public static void main(String[] args) {
          Employee CEO = new Employee("John","CEO", 30000);
     
          Employee headSales = new Employee("Robert","Head Sales", 20000);
     
          Employee headMarketing = new Employee("Michel","Head Marketing", 20000);
     
          Employee clerk1 = new Employee("Laura","Marketing", 10000);
          Employee clerk2 = new Employee("Bob","Marketing", 10000);
     
          Employee salesExecutive1 = new Employee("Richard","Sales", 10000);
          Employee salesExecutive2 = new Employee("Rob","Sales", 10000);
     
          CEO.add(headSales);
          CEO.add(headMarketing);
     
          headSales.add(salesExecutive1);
          headSales.add(salesExecutive2);
     
          headMarketing.add(clerk1);
          headMarketing.add(clerk2);
     
          //打印该组织的所有员工
          System.out.println(CEO); 
          for (Employee headEmployee : CEO.getSubordinates()) {
             System.out.println(headEmployee);
             for (Employee employee : headEmployee.getSubordinates()) {
                System.out.println(employee);
             }
          }        
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    步骤 3
    执行程序,输出结果为:

    Employee :[ Name : John, dept : CEO, salary :30000 ]
    Employee :[ Name : Robert, dept : Head Sales, salary :20000 ]
    Employee :[ Name : Richard, dept : Sales, salary :10000 ]
    Employee :[ Name : Rob, dept : Sales, salary :10000 ]
    Employee :[ Name : Michel, dept : Head Marketing, salary :20000 ]
    Employee :[ Name : Laura, dept : Marketing, salary :10000 ]
    Employee :[ Name : Bob, dept : Marketing, salary :10000 ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    rsut

    在rust中由于所有权机制,组合模式中如果不使用引用的方法在组合顺序上便有所限制,只能从低级的开始组合,否则进行组合时便会出现所有权报错问题,由于本人代码水平有限没能实现用引用实现的组合模式,只能用转移所有权的方法实现。

    use std::fmt;
    
    // 定义雇员
    struct  Employee{
        name:String,
        dept:String,
        sal:i32,
        subordinates:Vec
    }
    // 自定义格式化
    impl fmt::Display for Employee {
        fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
            write!(f,"Employee : Name{}, dept : {} ,salary : {}", self.name,self.dept,self.sal)
        }
    }
    impl Employee {
        fn add(&mut self,e:Employee) {
            self.subordinates.push(e);
        }
        fn remove(&mut self,e:Employee) {
            self.subordinates.retain(|x| {
                if x.name!=e.name||x.dept==e.dept||x.sal==e.sal{
                    return true;
                }
                return false;
            });
        }
        fn get_subordinates(&self) {
            self.subordinates.as_ptr();
        }
        fn new(name:String,dept:String, sal:i32)->Employee{
            Employee { name, dept,sal,subordinates:Vec::new() }
        }
        
    }
    fn pe(e:&Employee) {
        println!("{}",e);
        if !e.subordinates.is_empty(){
           e.subordinates.iter().for_each(|x| pe(x));
        }
        
    }
    fn main(){
        let mut ceo=Employee::new(String::from("John"), String::from("CEO"),30000);
        let mut head_sales=Employee::new(String::from("Robert"), String::from("Head Sales"),20000);
        let mut head_market=Employee::new(String::from("Michel"), String::from("Head Marketing"),10000);
        let mut clerk1=Employee::new(String::from("Laura"), String::from("Marketing"),10000);
        head_sales.add(head_market);
        head_sales.add(clerk1);
        ceo.add(head_sales);
        
        pe(&ceo)
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
  • 相关阅读:
    ajax异步访问及跨域处理
    模拟实现memcpy memmove,字符类库函数的介绍,strerror,strtok的使用讲解。
    QQ怎么上传大于1G的视频啊?视频压缩这样做
    宁波效实中学国际课程与交流中心IB数学课程介绍
    通讯协议学习之路:IIC协议理论
    css第二课:外部样式link和import的运用及行内样式的介绍
    JavaScript面试经,offer拿到手软
    qt 展示TF卡(USB、SD)内容时,中文乱码问题
    【前段基础入门之】=>初识 HTML
    【亲测】阿里云ECS搭建《幻兽帕鲁》联机服务器
  • 原文地址:https://blog.csdn.net/studycodeday/article/details/134426003