I am using an adapter pattern to implement a Doubly Linked list class that uses two adaptees classes; Stack and Queue for pop() and dequeue() respectively. The results are all good; all I want to check here is if my implementation makes sense as an adapter pattern.
The Driver method creates a doubly linked list and calls the removeLast() method in the DDL class which uses the adaptee's class(Stack's pop() ) to remove the node in the last position. Queue is similar to this.
I have passed the DDL list created to the removeLast() and passed it again while creating a Stack object and then called pop() so that these classes have access to that list. This implementation works and outputs correctly.
Yet I feel that passing the DDL list to two classes sequentially is not a good design choice. Can anyone please suggest me if this implementation looks good or any other ways to access the list without passing it to DDL and Stack class? Thank you.
For brevity, I have only placed the interfaces and two class: Adapter and Stack. The node class is as usual but with two pointers prev and next.
Here are the interfaces used:
//Interface that is to be implemented by the DDLAdapter concrete class
public interface DDLInterface {
public void insertLast(String value);
public void removeFirst(DDLAdapter ddl);
public boolean removeLast(DDLAdapter ddl);
}
public interface StackInterface {
public boolean pop();
}
public interface QueueInterface {
public void dequeue();
}
Here is the adapter class:
public class DDLAdapter implements DDLInterface {
DDLAdapter ddl;
Node head , tail;
int size = 1;
public DDLAdapter(){
head = null;
tail = null;
}
@Override
public void insertLast(String value) {
Node newNode = new Node(value);
Node temp = head;
if(head == null){
head = newNode; //Set newNode to head
newNode.prev = null; //newNode's prev points to null
return;
}
while(temp.next != null){ //If the node has a next reference
temp = temp.next; //Assign the node that was the reference
//as the new temp node
}
temp.next = newNode; //Add the newNode after the tail
//Update the references of the newNode
newNode.next = null; //next is null as newNode is the tail
newNode.prev = temp; //prev is pointing to the previous tail which
//is stored at temp
size++;
}
//Just the dummy print method
public void print(){
Node temp =head; //Get the head to the temp
System.out.println("Size = " + size);
for(int i = 0; i < size; i++){//Loop through the whole list
//Print the value of the temp
System.out.print(temp.value + " ");
//If temp has a next pointer, assign temp to that referenced node
if(temp.next != null){
temp = temp.next;
}
}
}
@Override
public boolean removeLast(DDLAdapter ddl) {
this.ddl = ddl; //Set the initialized ddl object to the passed ddl list
stack = new Stack(this.ddl); //IS THIS A GOOD WAY OF DESIGN?
//This calls the stack pop() method
if(stack.pop() == true) {
size--;
return true;
}else {
return false;
}
}
@Override
public void removeFirst() {
// TODO Auto-generated method stub
}
}
Here's the Stack class that implements the StackInterface:
public class Stack implements StackInterface{
DDLAdapter ddl; //Initialize a ddl object
String name;
//Is this a good way of design to access the ddl list created in the main method?
public Stack(DDLAdapter ddl) { //Here this ddl is the list we are working on
this.ddl = ddl;
}
@Override
public boolean pop() {
Node temp = ddl.head;
while(temp.next != null){ //If the node has a next reference
temp = temp.next; //Assign the node that was the reference
//as the new temp node
}
Node prev = temp.prev; //Grab the prev node of tail
prev.next = null; //now the prev points to null instead of the tail
return true;
}
}
Aucun commentaire:
Enregistrer un commentaire