diff --git a/src/headfirst/designpatterns/builder/pizza/MeatLoversPizzaBuilder.java b/src/headfirst/designpatterns/builder/pizza/MeatPizzaBuilder.java similarity index 100% rename from src/headfirst/designpatterns/builder/pizza/MeatLoversPizzaBuilder.java rename to src/headfirst/designpatterns/builder/pizza/MeatPizzaBuilder.java diff --git a/src/headfirst/designpatterns/builder/pizza/VeggieLoversPizzaBuilder.java b/src/headfirst/designpatterns/builder/pizza/VeggiePizzaBuilder.java similarity index 100% rename from src/headfirst/designpatterns/builder/pizza/VeggieLoversPizzaBuilder.java rename to src/headfirst/designpatterns/builder/pizza/VeggiePizzaBuilder.java diff --git a/src/headfirst/designpatterns/singleton/enumS/Singleton.java b/src/headfirst/designpatterns/singleton/enumS/Singleton.java new file mode 100755 index 00000000..c416fd3f --- /dev/null +++ b/src/headfirst/designpatterns/singleton/enumS/Singleton.java @@ -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!"; + } +} diff --git a/src/headfirst/designpatterns/singleton/enumS/SingletonClient.java b/src/headfirst/designpatterns/singleton/enumS/SingletonClient.java new file mode 100755 index 00000000..ba364fca --- /dev/null +++ b/src/headfirst/designpatterns/singleton/enumS/SingletonClient.java @@ -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()); + } +} diff --git a/src/headfirst/designpatterns/templatemethod/list/MyListTestDrive.java b/src/headfirst/designpatterns/templatemethod/list/MyListTestDrive.java new file mode 100644 index 00000000..4af85553 --- /dev/null +++ b/src/headfirst/designpatterns/templatemethod/list/MyListTestDrive.java @@ -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)); + } + } +} \ No newline at end of file diff --git a/src/headfirst/designpatterns/templatemethod/list/MyStringList.java b/src/headfirst/designpatterns/templatemethod/list/MyStringList.java new file mode 100644 index 00000000..769ae24c --- /dev/null +++ b/src/headfirst/designpatterns/templatemethod/list/MyStringList.java @@ -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 { + 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; + } +} \ No newline at end of file