0

我正在尝试在我的开发中应用 MVC(没有任何框架),但我遇到了很多麻烦。第一个麻烦:M!您如何处理具有深层数据结构和多个“根元素”的复杂模型?

这是一个非常简单的例子。假设我们的模型只有两个“点”。哪个是最好的模型实现?

想法 n°1:使用一个入口点和吸气剂

import java.awt.Point;
public class TwoPointsA {
    private final Point A = new Point();
    private final Point B = new Point();
    public Point getA() {
        return this.A;
    }
    public Point getB() {
        return this.B;
    }
}

想法 2:使用一个入口点并委托方法

import java.awt.Point;
public class TwoPointsB {
    private final Point A = new Point();
    private final Point B = new Point();
    public void translateA(final int dx, final int dy) {
        this.A.translate(dx, dy);
    }
    public void translateB(final int dx, final int dy) {
        this.B.translate(dx, dy);
    }
}

想法 3:使用多个入口点和吸气剂

import java.awt.Point;
public class TwoPointsC {
    public enum Points {
        A, B;
        private final Point value = new Point();
        public Point get() {
            return this.value;
        }
    }
}

想法 4:使用多个入口点和委托方法

import java.awt.Point;
public class TwoPointsD {
    public enum Points {
        A, B;
        private final Point value = new Point();
        public void translate(final int dx, final int dy) {
            this.value.translate(dx, dy);
        }
    }
}
4

1 回答 1

0

MVC 是关于分离关注点的。

MODEL 和 VIEW 是对象的集合。

您的 VIEW 中的对象可能具有 BUTTON、WINDOW、TEXTAREA 等类型。

您的模型中的对象具有与您的域相关的类型,例如地址、邮政编码等

CONTROLLER 对象为您的模型提供接口。VIEW 对象与 CONTROLLER 对象对话以与您的系统进行交互。

传统上 VIEW 对象可以直接与 MODEL 对象对话,但不推荐这样做。

所有对象都符合OO原则,都是完整的对象。

于 2012-12-05T14:23:45.057 回答