Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
队列java实现问题,为什么要这样设计?_Java - Fatal编程技术网

队列java实现问题,为什么要这样设计?

队列java实现问题,为什么要这样设计?,java,Java,大家好,我正在学习使用java的队列数据结构,但我已经看到一个抽象类定义了枚举,这里定义了枚举,为什么还要做好事,或者做这样设计的事情呢 package com.jwetherell.algorithms.data_structures; import java.util.Arrays; /** * Queue. A queue is a particular kind of abstract data type or collection in * which the entitie

大家好,我正在学习使用java的队列数据结构,但我已经看到一个抽象类定义了枚举,这里定义了枚举,为什么还要做好事,或者做这样设计的事情呢

package com.jwetherell.algorithms.data_structures;

import java.util.Arrays;


/**
 * Queue. A queue is a particular kind of abstract data type or collection in
 * which the entities in the collection are kept in order and the principal (or
 * only) operations on the collection are the addition of entities to the rear
 * terminal position and removal of entities from the front terminal position.
 * This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO
 * data structure, the first element added to the queue will be the first one to
 * be removed.
 * 
 * http://en.wikipedia.org/wiki/Queue_(abstract_data_type)
 * 
 * @author Justin Wetherell <phishman3579@gmail.com>
 */

public abstract class Queue<T> {

    public enum QueueType {
        LinkedQueue, ArrayQueue
    };

    /**
     * Enqueue the value in the queue.
     * 
     * @param value
     *            to enqueue.
     */
    public abstract void enqueue(T value);

    /**
     * Dequeue the head of the queue.
     * 
     * @return value that was dequeued.
     */
    public abstract T dequeue();

    /**
     * Does the queue contain the value. Warning this is an O(n) operation.
     * 
     * @param value
     *            to locate in the queue.
     * @return True if queue contains value.
     */
    public abstract boolean contains(T value);

    /**
     * Number of items in the queue.
     * 
     * @return number of items.
     */
    public abstract int size();

    /**
     * Create queue from QueueType.
     * 
     * @param type
     *            of queue to create.
     * @return Queue that was created.
     */
    public static <T> Queue<T> createQueue(QueueType type) {
        switch (type) {
            case ArrayQueue:
                return new ArrayQueue<T>();
            default:
                return new LinkedQueue<T>();
        }
    }
包com.jwetherell.algorithms.data\u结构;
导入java.util.array;
/**
*排队。队列是一种特殊的抽象数据类型或集合
*集合中的实体保持有序,主体(或
*仅)集合上的操作是在后面添加实体
*端子位置和从前端子位置删除实体。
*这使队列成为先进先出(FIFO)数据结构。先进先出
*数据结构中,添加到队列的第一个元素将是
*被移除。
* 
* http://en.wikipedia.org/wiki/Queue_(抽象数据类型)
* 
*@作者Justin Wetherell
*/
公共抽象类队列{
公共枚举队列类型{
链接队列
};
/**
*将值放入队列中。
* 
*@param值
*排队。
*/
公开摘要无效排队(T值);
/**
*把队列的头排出来。
* 
*@已退出队列的返回值。
*/
公开摘要T出列();
/**
*队列是否包含该值。警告:这是一个O(n)操作。
* 
*@param值
*在队列中查找。
*@如果队列包含值,则返回True。
*/
公共抽象布尔包含(T值);
/**
*队列中的项目数。
* 
*@返回项目数。
*/
公共抽象int size();
/**
*从QueueType创建队列。
* 
*@param类型
*要创建的队列的数量。
*@已创建的返回队列。
*/
公共静态队列createQueue(QueueType类型){
开关(类型){
案例排列队列:
返回新的ArrayQueue();
违约:
返回新的LinkedQueue();
}
}

很抱歉,在出现问题之前,我没有说清楚,这是源代码,我相信问题在于,为什么我们要为静态工厂方法
createQueue
定义枚举(
ArrayQueue
LinkedQueue
),以及这是否是一个好的设计

依我的拙见,这不是一个好的设计。我不会对像
队列
这样的一般事物使用工厂方法。用户可能有自己的
队列
实现,然后你会被这个工厂方法卡住,不能与新的实现一起工作。相反,只需在此处实例化你需要的实现你需要


只有当这是应用程序的业务规则时(即队列类型是可配置的),我才会使用队列工厂;但即使这样,我也不会在抽象的
队列
类中实现它。

问题是什么?你能澄清一下你的问题吗?我不确定你在问什么。没有更多的上下文,这是强加的我无法说明为什么在您正在查看的类中会这样实现,以及这是否是一个好的解决方案。枚举在静态
createQueue
中的底部使用,并且执行它看起来的操作;它只是创建队列的一个子类型而不是另一个子类型的一种方法,在本例中,它是一个
数组Queue
,或
LinkedQueue
。我肯定还有其他方法可以做到这一点,但这是他们选择的方法。除此之外,我无法告诉你“为什么”。