在水果盘中有一些水果,如苹果、香蕉、梨子,当然大水果盘中也还可以有小水果盘,先需要对水果盘中的水果进行遍历,当然如果对一个水果盘执行“吃”方法时,实际上就是吃其中的水果,使用组合模式模拟场景。
代码:
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();
}
}
///=========================