使用复合模式来计算世界上的人数 [英] using Composite Pattern to count the number of people in the world

查看:110
本文介绍了使用复合模式来计算世界上的人数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一种综合模式,该模式可以计算世界,大陆,州的人数...对不起,没有州,但有国家/地区,我很抱歉



这是我遵循的模式:



我这样做:





但我不确定是否正确,因为我可以使用叶子->人口吗?



因为当我为PC制作Composite Pattern时,它的(PC = PC组件将是Cabinet = Composite,来自Cabinet将会是Composite HDD =叶子,而主板=复合,则从主板将是RAM =叶子,CPU =叶子)。我想说的每个叶子都是不同的,并且在我的图中是相同的。



您认为我至少有一些好东西。



谢谢您的回答:)

解决方案

首先,您应该先对您的分析问题。那么,您所拥有的是什么?因此,您有一个世界,其中大陆组成,其中每个大陆由国家组成>,每个国家/地区由地区组成。在进行此类OO分析时,名词(此处以粗体显示)是您班级的候补。关系词还提供了一些指示类之间如何相关的信息(标记为斜体)。



那么我们能从frmo看到什么?有些术语(称为实体)具有一些直接排序的层次包含关系。但是没有某种形式的关系可以将某事物视为另一种类型的元素。这是在考虑如何相互继承时的重要信息。因为可以将继承关系看成是专门化的,所以在这里没有任何意义:一个国家不是一个特定的世界,一个世界也不是一个特定的国家。此外,我们看到没有旁路,例如contry不是世界的直接部分。并且任何元素都不能包含其自身类型的元素,例如没有国家包含一个国家。





首先,只需调用您的wold实例的calculatePopulation。



如果您想以统一的方式处理任何类型的元素,则可以创建一个抽象类或接口并让所有元素从中继承/实现。此类/接口声明calculatePopulation操作。改进后的解决方案可以从层次结构中的任意元素开始计算种群,而无需考虑元素的种类。


  1. 解决方案:当您想要一个没有在类层次结构内部实现并且是递归的算法时。在那种情况下,您需要一个抽象,以便您可以以相同的方式处理所有元素,因为算法实现不必过多地关注细节。因此,也让所有元素都从一个abstact类继承,该类具有提供所包含元素的操作和提供填充的操作,该操作直接是元素的一部分(而不是像以前的情况那样是可传递的/间接的)。因此,现在您将遇到以下情况:您有两个明显不同的情况,第一,您有子元素,没有直接人口,第二,您没有子元素,有直接人口。但是您的遍历可以忽略这一点。



在这里,您可以通过调用实现(这是另一个类)并传递要填充的对象开始。



当然,还有其他解决方案。但是所有仅使用复合方法解决此问题的人都是非常人为的,因为从概念上讲它是递归的,而您的问题不是递归的(即没有元素可以包含相同类型的元素),因此复合方法是不合需要的或称其为超大尺寸。


I'm trying to make a Composite Pattern that counts the number of people in the world, the continent, the state... sorry no state but country my bad sorry

Here is the pattern that I follow:

I did this:

but I am not sure it's right, because can I use leaf -> population ?

Because when I did Composite Pattern for PC so its (PC= component from PC will be Cabinet = composite, from Cabinet will be HDD= leaf and Motherboard = composite, from Motherboard will be RAM = leaf and CPU = leaf). What I want to say every leafs are different and in my diagram are same.

and you think I have something good at least.

Thank you for your answers :)

解决方案

First of all, you should start with an analysis of your problem. So what is that you have and how is it related. So you have a World that consists of continents, where each continent consists of countries, where each country consists of regions. When doing such a OO Analysis, the nouns (here marked in bold) are candidates for your classes. And the relational terms give some indications howp the classes are related (marked italic).

So what can we see frmo this? There are some terms (called entities) that have some directly ordered hierarchical containment relations. But no relation in the kind that something can be seen as an element of the other type. This is an important information when thinking about how to inherit from each other; as the inherits relation can be seen a specialization, it doesnt make any sense here: a country is not a specific world and a world is not a specific country. Further, we see there are no bypasses, e.g. a contry is no direct part of the world. And no element can contain an element of its own type, e.g. no country contains a country.

So what can we learn from this brief analysis? We cannot formulate the problem as a composite without changing significant expressiveness (e.g. changing the intended hierarchy) or without introducing much additional complexity (e.g. by constraints).

And now in the second step, we develop a design out of the analysis results. How the design looks like at the end depends on what shall be done with the elements.

  1. Solution: In your example you can just add a calculatePopulation operation to any of the classes. As any class knows its parts and their types, this can directly be done by calling the calculatePopulation operation of the contained instances of the next element type. This would be the OO way in solving such problems, which is most likely the best way if there are no further constraints.

And to start it you just call calculatePopulation of your wold instance.

If you want to treat any kind of element in a unified way, you can create an abstract class or interface and let all the elements inherit from it/implement it. This class/interface declares the calculatePopulation operation. Whith that improved solution you can start with an arbitrary element of your hierarchy to calculate the population without taking care for what kind of element it is.

  1. Solution: When you want to have an algorithm that is not implemented inside of the class hierarchy and that is recursive. In that case you need an abstraction, such that you can handle all the elements in the same way, as the algorithm implementation shall not take care for too much of the details. Thus also let all the elements inherit from an abstact class that has an operation that provides the contained elements and an operation that provides the population, which is directly part of the element ( not transitive/indirectly as in the case before). So now you would have the situation that you have two clearly disticitve cases, first you have child element and no direct population and, second, you have no child elements and a direct population. But your traversal can ignore this.

Here you start it by calling your implementation (which is an other class) and passing the object of which you want to have to population.

There are further solutions, of course. But all of those using a composite just for this problem are very artificial, because it is by its concept recursive and your problem is not recursive (i.e. there is no element that can contain a element of the same type) and thus the composite is unsiateble or lets call it oversized.

这篇关于使用复合模式来计算世界上的人数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆