dimanche 29 avril 2018

Does my program utilize any design patterns, and if so then which one?

So does my program utilize any design patterns and if so then which one? And also if it doesn't utilize any design patterns then how would I edit this to utilize a design pattern? I've just been trying to look at design patterns and got kind of confused by some of them. Here is an interface, an implement class and a main class.

public interface StackADTInterface<T>
{
    public void push(T element);

    public T pop() throws EmptyCollectionException;

    public T peek();

    public int size();

    public boolean isEmpty();

    public String toString();
}

public class StackArraySec02Imp<T> implements StackADTInterface<T>
{
    public final int maxLength = 100;
    public int top = 0;

    public T[] stackArray;

    public StackArraySec02Imp()
    {
        stackArray = (T[])(new Object[maxLength]);
    }

    public StackArraySec02Imp(int initialCapacity)
    {
        stackArray = (T[])(new Object[initialCapacity]);
    }

    @Override
    public void push(T element) 
    {
        if(size() == stackArray.length)
        {
            System.out.println("Stack is Full");
        }
        else
        {
        stackArray[top] = element;
        top++;
        }
    }

    @Override
    public T pop() throws EmptyCollectionException 
    {

        if(!isEmpty())
        {
            top--;
            T retObj;
            retObj = stackArray[top];
            stackArray[top] = null;
            return retObj;
        }
        else
        {
            throw new EmptyCollectionException("Stack is Empty");
        }
    }

    @Override
    public T peek() 
    {
        T retObj = stackArray[top-1];
        return retObj;
    }

    @Override
    public int size() 
    {
        return top;
    }

    @Override
    public boolean isEmpty() 
    {
        boolean isEmptyFlag = false;
        if(top <= 0 && stackArray[top] == null)
        {
            isEmptyFlag = true;
        }

        return isEmptyFlag;
    }

    @Override
    public String toString()
    {
        String result = "";

        for(int i = stackArray.length - 1; i >= 0; i--)
        {
            result += stackArray[i] + "\n";
        }

        return result;
    }

}


public class mainArraymain 
{
    public static void main(String[] args)
    {
        StackArrayImplemList<Integer> newStack = new 
        StackArrayImplemList<Integer>();


        newStack.push(12);
        newStack.push(5);
        newStack.push(6);
        newStack.push(7);
        newStack.push(8);
        newStack.push(9);

        System.out.println(newStack.toString());
    }

}

Aucun commentaire:

Enregistrer un commentaire