Skip to content

Commit

Permalink
updating Singleton with enum example, Template Method with list example
Browse files Browse the repository at this point in the history
  • Loading branch information
bethrobson committed Mar 10, 2021
1 parent 52a7152 commit b4b218a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/headfirst/designpatterns/singleton/enumS/Singleton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package headfirst.designpatterns.singleton.enumS;

public enum Singleton {
UNIQUE_INSTANCE;

// other useful fields here

// other useful methods here
public String getDescription() {
return "I'm a thread safe Singleton!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package headfirst.designpatterns.singleton.enumS;

public class SingletonClient {
public static void main(String[] args) {
Singleton singleton = Singleton.UNIQUE_INSTANCE;
System.out.println(singleton.getDescription());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package headfirst.designpatterns.templatemethod.list;

import java.util.List;

public class MyListTestDrive {

public static void main(String[] args) {
String[] ducks = { "Mallard Duck", "Redhead Duck", "Rubber Duck", "Decoy Duck"};
MyStringList ducksList = new MyStringList(ducks);
for (int i = 0; i < ducksList.size(); i++) {
System.out.println(ducksList.get(i));
}
String oldDuck = ducksList.set(3, "Donald Duck");
System.out.println("Replacing " + oldDuck);
System.out.println("New List:");
for (int i = 0; i < ducksList.size(); i++) {
System.out.println(ducksList.get(i));
}
// Now the real test... subList()
List ducksSubList = ducksList.subList(2, 3);
System.out.println("Created a sub list of ducks, with size: " + ducksSubList.size());
for (int i = 0; i < ducksSubList.size(); i++) {
System.out.println(ducksSubList.get(i));
}
}
}
35 changes: 35 additions & 0 deletions src/headfirst/designpatterns/templatemethod/list/MyStringList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package headfirst.designpatterns.templatemethod.list;

import java.util.AbstractList;

// AbstractList provides a skeletal implementation of the List interface
// to minimize the effort required to implement this interface backed by
// a "random access" data store (such as an array).

// To implement an unmodifiable list, the programmer needs only to extend
// this class and provide implementations for the get(int) and size() methods.
// get(int index) is an abstract method in AbstractList
// size() is an abstract method in AbstractCollection
// subList(int fromIndex, int toIndex) returns a view of the portion of this list
// between the specified fromIndex, inclusive, and toIndex, exclusive.

public class MyStringList extends AbstractList<String> {
private String[] myList;
MyStringList(String[] strings) {
myList = strings;
}
@Override
public String get(int index) {
return myList[index];
}
@Override
public String set(int index, String item) {
String oldString = myList[index];
myList[index] = item;
return oldString;
}
@Override
public int size() {
return myList.length;
}
}

0 comments on commit b4b218a

Please sign in to comment.