Uml 关于我项目中的关联和聚合
Uml about the association and the aggregation in my project
我正在为考试做这个小项目。我的老师关注 uml,所以我的问题是关于我项目中的聚合和关联(我知道这是一个被问了很多次的问题)。
该项目是一个全局观察者,它有一个组件集合,每次组件(叶或复合)改变其状态时,它都会通知观察者。我的问题是关于 ConcreteObserver 和 Component 之间聚合的正确性:ConcreteObserver 有一个 Component 的集合,但是每个 Component 都有一个对观察者的引用(在 Leaf 和 Composite 中有一个观察者)。也许我应该添加一个关联 Component->ConcreteObserver?
也许我应该将聚合 ConcreteObserver 1->* Component 更改为关联 ConcreteObserver1->*Com
这是uml
这是我项目的全部代码我知道它很长但是你必须注意具体观察者和具体组件(复合)
(我没有放叶子,因为它与 Composite 非常相似,但它没有实现子相关的方法)
这是组件
//Component
public interface Component {
public void op();
public void add(Component c);
public void remove(Component c);
public List <Component> getChild();
public void setState(int x);
public int getState();
public String getName();
}
//Composite
public class Composite implements Component {
private int state;
private String name;
private Observer observer;
private List <Component> comps = new ArrayList <Component> ();
public Composite(String name, Observer observer){
this.name = name;
this.observer = observer;
this.observer.add(this);
}
@Override
public void setState(int x){
this.state = x;
observer.update(name);
}
@Override
public int getState(){
return state;
}
@Override
public String getName(){
return name;
}
@Override
public void op() {
System.out.println("sono una foglia");
}
@Override
public void add(Component c) {
comps.add(c);
}
@Override
public void remove(Component c) {
comps.remove(c);
}
@Override
public List<Component> getChild() {
List <Component> cc = new ArrayList <Component> ();
for (Component zen : comps){
cc.add(zen);
}
return cc;
}
}
//Observer
public abstract class Observer {
public abstract void update(String name);
public abstract void add(Component c);
}
//ConcreteObserver
public class ConcreteObserver extends Observer {
private List <Component> components = new ArrayList <Component> ();
@Override
public void update(String name){
for(Component zen : components){
if(zen.getName()==name){
System.out.println("the state of " + name + " is changed, now it's " + zen.getState());
}
}
}
@Override
public void add(Component c){
components.add(c);
}
public void printAllState(){
for(Component zen : components){
System.out.println("the state of " + zen.getName() + " is " + zen.getState());
}
}
}
//main
public class Compito24Febbraio2017 {
public static void main(String[] args) {
ConcreteObserver o = new ConcreteObserver();
Composite c1 = new Composite ("c1",o);
Leaf l1 = new Leaf("l1",o);
Leaf l2 = new Leaf("l2",o);
c1.add(l1);
c1.add(l2);
c1.setState(5);
l1.setState(4);
o.printAllState();
}
}
只是不关心那些共享聚合。让他们远离,或者如果他们在那里,就忽略他们。 UML 规范说(第 110 页):
Indicates that the Property has shared aggregation semantics. Precise semantics of shared aggregation varies by application area and modeler.
这实际上意味着:它没有特定的含义。
编辑 关于您的评论:表示关系的首选方式是这样的:
您在关联的另一端使用角色名称,而不是键入属性。
我正在为考试做这个小项目。我的老师关注 uml,所以我的问题是关于我项目中的聚合和关联(我知道这是一个被问了很多次的问题)。 该项目是一个全局观察者,它有一个组件集合,每次组件(叶或复合)改变其状态时,它都会通知观察者。我的问题是关于 ConcreteObserver 和 Component 之间聚合的正确性:ConcreteObserver 有一个 Component 的集合,但是每个 Component 都有一个对观察者的引用(在 Leaf 和 Composite 中有一个观察者)。也许我应该添加一个关联 Component->ConcreteObserver? 也许我应该将聚合 ConcreteObserver 1->* Component 更改为关联 ConcreteObserver1->*Com
这是uml
这是我项目的全部代码我知道它很长但是你必须注意具体观察者和具体组件(复合) (我没有放叶子,因为它与 Composite 非常相似,但它没有实现子相关的方法) 这是组件
//Component
public interface Component {
public void op();
public void add(Component c);
public void remove(Component c);
public List <Component> getChild();
public void setState(int x);
public int getState();
public String getName();
}
//Composite
public class Composite implements Component {
private int state;
private String name;
private Observer observer;
private List <Component> comps = new ArrayList <Component> ();
public Composite(String name, Observer observer){
this.name = name;
this.observer = observer;
this.observer.add(this);
}
@Override
public void setState(int x){
this.state = x;
observer.update(name);
}
@Override
public int getState(){
return state;
}
@Override
public String getName(){
return name;
}
@Override
public void op() {
System.out.println("sono una foglia");
}
@Override
public void add(Component c) {
comps.add(c);
}
@Override
public void remove(Component c) {
comps.remove(c);
}
@Override
public List<Component> getChild() {
List <Component> cc = new ArrayList <Component> ();
for (Component zen : comps){
cc.add(zen);
}
return cc;
}
}
//Observer
public abstract class Observer {
public abstract void update(String name);
public abstract void add(Component c);
}
//ConcreteObserver
public class ConcreteObserver extends Observer {
private List <Component> components = new ArrayList <Component> ();
@Override
public void update(String name){
for(Component zen : components){
if(zen.getName()==name){
System.out.println("the state of " + name + " is changed, now it's " + zen.getState());
}
}
}
@Override
public void add(Component c){
components.add(c);
}
public void printAllState(){
for(Component zen : components){
System.out.println("the state of " + zen.getName() + " is " + zen.getState());
}
}
}
//main
public class Compito24Febbraio2017 {
public static void main(String[] args) {
ConcreteObserver o = new ConcreteObserver();
Composite c1 = new Composite ("c1",o);
Leaf l1 = new Leaf("l1",o);
Leaf l2 = new Leaf("l2",o);
c1.add(l1);
c1.add(l2);
c1.setState(5);
l1.setState(4);
o.printAllState();
}
}
只是不关心那些共享聚合。让他们远离,或者如果他们在那里,就忽略他们。 UML 规范说(第 110 页):
Indicates that the Property has shared aggregation semantics. Precise semantics of shared aggregation varies by application area and modeler.
这实际上意味着:它没有特定的含义。
编辑 关于您的评论:表示关系的首选方式是这样的:
您在关联的另一端使用角色名称,而不是键入属性。