组合模式 水果盘

   在水果盘中有一些水果,如苹果、香蕉、梨子,当然大水果盘中也还可以有小水果盘,先需要对水果盘中的水果进行遍历,当然如果对一个水果盘执行“吃”方法时,实际上就是吃其中的水果,使用组合模式模拟场景。

代码:

import java.util.ArrayList;
import java.util.Arrays;
interface MyElement{
      public void add(MyElement element);
      public void remove(MyElement element);
      public void eat();
      public boolean Leaf();
}
class Plate implements MyElement{
      ArrayList<MyElement> list = new ArrayList<MyElement>();

      @Override
      public void add(MyElement element) {
      // TODO Auto-generated method stub
            list.add(element);
            System.out.println("添加成功");
      }
       @Override
      public void remove(MyElement element) {
            // TODO Auto-generated method stub
             list.remove(element);
            System.out.println("移除成功");
      }

      @Override
      public void eat() {
            // TODO Auto-generated method stub
            if(Leaf()==false){//有错误但能实现
            for(MyElement e:list){
                  e.eat();
            }
            }else{
                  for(MyElement e:list){
                  e.eat();
                  }
            }
      }
      public boolean Leaf() {
            // TODO Auto-generated method stub

      return false;
}

}
class Apple implements MyElement{

      @Override
      public void add(MyElement element) {}

      @Override
      public void remove(MyElement element) {}



      @Override
      public void eat() {
      // TODO Auto-generated method stub
      System.out.println("eat apple");
      }

      @Override
      public boolean Leaf() {
            // TODO Auto-generated method stub
            Apple a = new Apple();
            a.eat();
            return true;
      }

}
class Pear implements MyElement{


      @Override
      public void add(MyElement element) {
            // TODO Auto-generated method stub

      }
      @Override
      public void remove(MyElement element) {
            // TODO Auto-generated method stub

      }

       @Override
      public void eat() {
            // TODO Auto-generated method stub
            System.out.println("eat pear");
      }

      @Override
      public boolean Leaf() {
            // TODO Auto-generated method stub
            return true;
      }
}
class Banana implements MyElement{


      @Override
      public void add(MyElement element) {
            // TODO Auto-generated method stub

      }

      @Override
      public void remove(MyElement element) {
      // TODO Auto-generated method stub

      }

      @Override
      public void eat() {
      // TODO Auto-generated method stub
            System.out.println("eat banana");
      }
      @Override
      public boolean Leaf() {
      // TODO Auto-generated method stub
            return true;
      }

}
public class Main {
    public static void main(String[] args) {
    Plate e1 = new Plate();
    Plate e2 = new Plate();
    MyElement a = new Apple();
    MyElement p = new Pear();
    MyElement b = new Banana();
    e2.add(a);
    e2.add(b);
    e2.add(p);
    e1.add(b);
    e1.add(e2);
    e1.eat(); 
    }
}


///=========================

import java.util.ArrayList;
//抽象组件
interface MyComponent {
      public void add(MyComponent a1);
      public void remove(Plate mc);
      public MyComponent getChild(int i);
      public void eat();
      public boolean isLeaf();
}
class Apple implements MyComponent {

      @Override
      public void eat() {
            System.out.println("Eat Apple");
      }
      @Override
      public void add(MyComponent mc) {
      // TODO Auto-generated method stub
      }

      @Override
      public void remove(Plate mc) {
      // TODO Auto-generated method stub
      }
      @Override
      public MyComponent getChild(int i) {
      // TODO Auto-generated method stub
      return null;
      }
      @Override
      public boolean isLeaf() {
      // TODO Auto-generated method stub
            return true;
      }

}
class Pear implements MyComponent {
      @Override
      public void eat() {
            System.out.println("Eat Pear");
      }
      @Override
      public void add(MyComponent mc) {
            // TODO Auto-generated method stub
      }
      @Override
      public void remove(Plate mc) {
      // TODO Auto-generated method stub
      }
      @Override
      public MyComponent getChild(int i) {
      // TODO Auto-generated method stub
            return null;
      }
      @Override
      public boolean isLeaf() {
      // TODO Auto-generated method stub
            return true;
      }
}
class Banana implements MyComponent {
      @Override
      public void eat() {
            System.out.println("Eat Banana");
      }
      @Override
      public void add(MyComponent mc) {}
      @Override
      public void remove(Plate mc){}
      @Override
      public MyComponent getChild(int i) {
            return null;
      }
      @Override
      public boolean isLeaf() {
            return true;
      }

}

class Plate implements MyComponent {

      private ArrayList<MyComponent>list;

      public Plate() {
            list = new ArrayList<MyComponent>();
      }
      @Override
      public void eat() {
            for(int i=0 ;i<list.size(); i++){
                  if((list.get(i).isLeaf())){
                       (list.get(i)).eat();
                  }else{
                        Plate p = (Plate) list.get(i);
                        p.eat();
                  }
            }      
      }
      public void add(MyComponent mc) {
            list.add(mc);
            System.out.println("添加成功");
      }
      public void remove(Plate mc) {
            if (list.contains(mc)) {
                  list.remove(mc);
                  System.out.println("移除成功");
            }
      }
      @Override
      public MyComponent getChild(int i) {
      // TODO Auto-generated method stub
            return list.get(i);
      }
      @Override
      public boolean isLeaf() {
            // TODO Auto-generated method stub
                  return false;
      }
      public ArrayList<MyComponent> getList(){
            return list;
      }
}
public class Main {
      public static void main(String[] args) {
            MyComponent plate1 = new Plate();
            MyComponent plate2 = new Plate();
            MyComponent a1 = new Apple();
            MyComponent p1 = new Pear();
            MyComponent b1 = new Banana();
            plate2.add(a1);
            plate2.add(p1);
            plate2.add(b1);
            System.out.println();
            plate1.add(a1);
            plate1.add(p1);
            plate1.add(plate2);  
            plate1.eat();
      }
}



///=========================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值