-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updating Singleton with enum example, Template Method with list example
- Loading branch information
1 parent
52a7152
commit b4b218a
Showing
6 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions
12
src/headfirst/designpatterns/singleton/enumS/Singleton.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/headfirst/designpatterns/singleton/enumS/SingletonClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/headfirst/designpatterns/templatemethod/list/MyListTestDrive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
src/headfirst/designpatterns/templatemethod/list/MyStringList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |