├── cover.jpg ├── lib ├── junit-4.12.jar └── hamcrest-core-1.3.jar ├── ReplaceTypeCodeWithStateStrategy ├── enum1 │ ├── State.java │ └── Main.java ├── after │ ├── State.java │ ├── StateLogging.java │ ├── StateStopped.java │ └── Main.java ├── step1 │ ├── State.java │ ├── StateLogging.java │ ├── StateStopped.java │ └── Main.java ├── step2 │ ├── State.java │ ├── StateLogging.java │ ├── StateStopped.java │ └── Main.java ├── AnsSmell │ ├── Main.java │ └── MainTest.java ├── ExSmell │ ├── Main.java │ └── MainTest.java ├── before │ └── Main.java ├── enum2 │ ├── Main.java │ └── Logger.java └── enum3 │ └── Main.java ├── DuplicateObservedData ├── after │ ├── ValueListener.java │ ├── Main.java │ ├── ValueChangeEvent.java │ ├── MainTest.java │ └── Value.java ├── graph │ ├── ValueListener.java │ ├── Main.java │ ├── ValueChangeEvent.java │ ├── MainTest.java │ └── Value.java ├── step2 │ ├── ValueListener.java │ ├── Main.java │ ├── ValueChangeEvent.java │ ├── Value.java │ └── MainTest.java ├── step3 │ ├── ValueListener.java │ ├── Main.java │ ├── ValueChangeEvent.java │ ├── MainTest.java │ └── Value.java ├── before │ ├── Main.java │ └── MainTest.java └── step1 │ ├── Main.java │ ├── Value.java │ └── MainTest.java ├── ReplaceConstructorWithFactoryMethod ├── AnsSmell2 │ ├── Printer.java │ ├── DigitPrinter.java │ ├── Main.java │ ├── GraphPrinter.java │ └── Client.java ├── ExSmell │ ├── Printer.java │ ├── DigitPrinter.java │ ├── Main.java │ ├── GraphPrinter.java │ └── Client.java ├── AnsSmell │ ├── DigitPrinter.java │ ├── Main.java │ ├── GraphPrinter.java │ ├── Printer.java │ └── Client.java ├── subclass │ ├── ShapeLine.java │ ├── ShapeOval.java │ ├── ShapeRectangle.java │ ├── Main.java │ └── MainTest.java ├── before │ ├── Main.java │ └── MainTest.java ├── after │ ├── Main.java │ └── MainTest.java ├── step1 │ ├── Main.java │ └── MainTest.java └── step2 │ ├── Main.java │ └── MainTest.java ├── RemoveControlFlag ├── SimpleDatabaseAfter1 │ ├── dbfile.txt │ ├── testfile.txt │ └── Main.java ├── SimpleDatabaseAfter2 │ ├── dbfile.txt │ ├── testfile.txt │ └── Main.java ├── SimpleDatabaseBefore │ ├── dbfile.txt │ └── Main.java ├── FindIntAfter2 │ ├── FindInt.java │ ├── Main.java │ └── MainTest.java ├── FindIntBefore │ ├── FindInt.java │ ├── Main.java │ └── MainTest.java └── FindIntAfter1 │ ├── Main.java │ ├── FindInt.java │ └── MainTest.java ├── ExtractClass ├── immutable │ ├── ImmutableAuthor.java │ ├── Main.java │ ├── Author.java │ └── MainTest.java ├── mutable │ ├── ImmutableAuthor.java │ ├── Main.java │ ├── Author.java │ └── MainTest.java ├── after │ └── Main.java ├── before │ └── Main.java └── step12 │ └── Main.java ├── ReplaceMagicNumberWithSymbolicConstant ├── typecheck │ ├── RobotCommand.java │ ├── Main.java │ ├── MainTest.java │ └── Robot.java ├── before │ ├── Main.java │ ├── Robot.java │ └── MainTest.java ├── after │ ├── Main.java │ ├── Robot.java │ └── MainTest.java ├── enum │ ├── Main.java │ ├── Robot.java │ └── MainTest.java ├── typecode │ ├── Main.java │ ├── RobotCommand.java │ ├── MainTest.java │ └── Robot.java ├── step1 │ ├── Main.java │ ├── Robot.java │ └── MainTest.java └── step2 │ ├── Main.java │ ├── Robot.java │ └── MainTest.java ├── HideDelegate ├── after │ ├── address.txt │ ├── AddressFile.java │ ├── MainTest.java │ ├── Main.java │ └── Database.java ├── again │ ├── address.txt │ ├── AddressFile.java │ ├── MainTest.java │ ├── Main.java │ └── Database.java ├── before │ ├── address.txt │ ├── AddressFile.java │ ├── MainTest.java │ ├── Database.java │ └── Main.java ├── nested │ ├── address.txt │ ├── MainTest.java │ └── Main.java ├── step1 │ ├── address.txt │ ├── AddressFile.java │ ├── MainTest.java │ ├── Main.java │ └── Database.java └── step2 │ ├── address.txt │ ├── AddressFile.java │ ├── MainTest.java │ ├── Main.java │ └── Database.java ├── ExtractMethod ├── after │ ├── Main.java │ └── Banner.java ├── step1 │ └── Main.java ├── step2 │ ├── Main.java │ └── Banner.java └── before │ ├── Main.java │ └── Banner.java ├── ReplaceErrorCodeWithException ├── AnsSmell │ ├── SomethingException.java │ ├── Main.java │ ├── Something.java │ └── MainTest.java ├── after │ ├── InvalidCommandException.java │ ├── Direction.java │ ├── Position.java │ └── Main.java ├── inner │ ├── InvalidCommandException.java │ ├── Direction.java │ ├── Position.java │ └── Main.java ├── state │ ├── InvalidCommandException.java │ ├── Direction.java │ ├── Position.java │ └── Main.java ├── step1 │ ├── InvalidCommandException.java │ ├── Direction.java │ ├── Position.java │ └── Main.java ├── step2 │ ├── InvalidCommandException.java │ ├── Direction.java │ ├── Position.java │ └── Main.java ├── before │ ├── Direction.java │ ├── Position.java │ └── Main.java └── ExSmell │ ├── Something.java │ ├── Main.java │ └── MainTest.java ├── IntroduceNullObject ├── step1 │ ├── NullLabel.java │ ├── Label.java │ └── Main.java ├── step2 │ ├── NullLabel.java │ ├── Label.java │ └── Main.java ├── after │ ├── NullLabel.java │ ├── Label.java │ ├── Main.java │ └── Person.java ├── step3 │ ├── NullLabel.java │ ├── Label.java │ ├── Main.java │ └── Person.java ├── before │ ├── Label.java │ └── Main.java ├── field │ ├── Main.java │ ├── Person.java │ └── Label.java └── nested │ ├── Main.java │ └── Person.java ├── TeaseApartInheritance ├── after │ ├── CSVStringReader.java │ ├── CSVFileReader.java │ ├── CSVPrinter.java │ ├── Main.java │ ├── file.csv │ └── CSVReader.java ├── change │ ├── CSVStringReader.java │ ├── CSVFileReader.java │ ├── CSVPrinter.java │ ├── Main.java │ ├── file.csv │ └── CSVReader.java ├── step4 │ ├── CSVStringReader.java │ ├── CSVFileReader.java │ ├── CSVPrinter.java │ ├── Main.java │ ├── file.csv │ ├── CSVReader.java │ └── CSVStringTablePrinter.java ├── before │ ├── CSVReader.java │ ├── Main.java │ ├── file.csv │ ├── CSVStringReader.java │ ├── CSVFileReader.java │ └── CSVStringTablePrinter.java ├── rename │ ├── CSVReader.java │ ├── CSVPrinter.java │ ├── Main.java │ ├── file.csv │ ├── CSVStringReader.java │ └── CSVFileReader.java ├── step2 │ ├── CSVReader.java │ ├── CSVPrinter.java │ ├── Main.java │ ├── file.csv │ ├── CSVStringReader.java │ ├── CSVFileReader.java │ └── CSVStringTablePrinter.java └── step3 │ ├── CSVReader.java │ ├── CSVPrinter.java │ ├── Main.java │ ├── file.csv │ ├── CSVStringReader.java │ └── CSVFileReader.java ├── IntroduceAssertion ├── delete │ └── Main.java ├── notdelete │ └── Main.java ├── after1 │ └── Main.java ├── after │ └── Main.java ├── after2 │ └── Main.java └── before │ └── Main.java ├── ReplaceTypeCodeWithClass ├── enum │ ├── ItemType.java │ ├── Main.java │ ├── Item.java │ └── MainTest.java ├── AnsSmellEnum │ ├── Main.java │ ├── LunchSet.java │ └── MainTest.java ├── AnsSmellClass │ ├── Main.java │ └── MainTest.java ├── ExSmell │ ├── Main.java │ └── MainTest.java ├── after │ ├── ItemType.java │ ├── Item.java │ ├── Main.java │ └── MainTest.java ├── step2 │ ├── ItemType.java │ ├── Main.java │ ├── Item.java │ └── MainTest.java ├── step1 │ ├── Main.java │ ├── ItemType.java │ └── MainTest.java └── before │ ├── Main.java │ ├── MainTest.java │ └── Item.java ├── ReplaceTypeCodeWithSubclasses ├── ExSmell │ ├── Main.java │ └── MainTest.java ├── AnsSmell │ ├── Main.java │ ├── MusicPlayer.java │ ├── VideoPlayer.java │ ├── Player.java │ └── MainTest.java ├── factory │ ├── ShapeLine.java │ ├── ShapeOval.java │ ├── ShapeRectangle.java │ ├── Main.java │ ├── MainTest.java │ └── Shape.java ├── manyfactory │ ├── ShapeLine.java │ ├── ShapeOval.java │ ├── Main.java │ ├── ShapeRectangle.java │ └── MainTest.java ├── before │ ├── Main.java │ └── MainTest.java ├── after │ ├── Main.java │ ├── ShapeLine.java │ ├── ShapeOval.java │ ├── ShapeRectangle.java │ └── MainTest.java ├── step1 │ ├── Main.java │ ├── ShapeLine.java │ ├── ShapeOval.java │ ├── ShapeRectangle.java │ └── MainTest.java └── step2 │ ├── Main.java │ ├── ShapeLine.java │ ├── ShapeOval.java │ ├── ShapeRectangle.java │ └── MainTest.java ├── ReplaceInheritanceWithDelegation ├── this │ ├── Dice.java │ ├── Main.java │ └── MainTest.java ├── after │ ├── Dice.java │ ├── Main.java │ └── MainTest.java ├── step2 │ ├── Dice.java │ ├── Main.java │ └── MainTest.java ├── step1 │ ├── Main.java │ └── MainTest.java └── before │ ├── Main.java │ └── MainTest.java └── UnitTest ├── failure └── Person.java └── sample └── Person.java /cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gilbutITbook/006921/HEAD/cover.jpg -------------------------------------------------------------------------------- /lib/junit-4.12.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gilbutITbook/006921/HEAD/lib/junit-4.12.jar -------------------------------------------------------------------------------- /lib/hamcrest-core-1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gilbutITbook/006921/HEAD/lib/hamcrest-core-1.3.jar -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/enum1/State.java: -------------------------------------------------------------------------------- 1 | public enum State { 2 | STATE_STOPPED, 3 | STATE_LOGGING; 4 | } 5 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/after/State.java: -------------------------------------------------------------------------------- 1 | public abstract class State { 2 | public abstract int getTypeCode(); 3 | } 4 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/step1/State.java: -------------------------------------------------------------------------------- 1 | public abstract class State { 2 | public abstract int getTypeCode(); 3 | } 4 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/step2/State.java: -------------------------------------------------------------------------------- 1 | public abstract class State { 2 | public abstract int getTypeCode(); 3 | } 4 | -------------------------------------------------------------------------------- /DuplicateObservedData/after/ValueListener.java: -------------------------------------------------------------------------------- 1 | public interface ValueListener { 2 | public void valueChanged(ValueChangeEvent e); 3 | } 4 | -------------------------------------------------------------------------------- /DuplicateObservedData/graph/ValueListener.java: -------------------------------------------------------------------------------- 1 | public interface ValueListener { 2 | public void valueChanged(ValueChangeEvent e); 3 | } 4 | -------------------------------------------------------------------------------- /DuplicateObservedData/step2/ValueListener.java: -------------------------------------------------------------------------------- 1 | public interface ValueListener { 2 | public void valueChanged(ValueChangeEvent e); 3 | } 4 | -------------------------------------------------------------------------------- /DuplicateObservedData/step3/ValueListener.java: -------------------------------------------------------------------------------- 1 | public interface ValueListener { 2 | public void valueChanged(ValueChangeEvent e); 3 | } 4 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/AnsSmell2/Printer.java: -------------------------------------------------------------------------------- 1 | public abstract class Printer { 2 | public abstract void println(int n); 3 | } 4 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/ExSmell/Printer.java: -------------------------------------------------------------------------------- 1 | public abstract class Printer { 2 | public abstract void println(int n); 3 | } 4 | -------------------------------------------------------------------------------- /RemoveControlFlag/SimpleDatabaseAfter1/dbfile.txt: -------------------------------------------------------------------------------- 1 | hyuki@example.com=Hiroshi Yuki 2 | sato@example.com=Sato Hanako 3 | tomura@example.com=Tomura 4 | -------------------------------------------------------------------------------- /RemoveControlFlag/SimpleDatabaseAfter1/testfile.txt: -------------------------------------------------------------------------------- 1 | hyuki@example.com=Hiroshi Yuki 2 | sato@example.com=Sato Hanako 3 | tomura@example.com=Tomura 4 | -------------------------------------------------------------------------------- /RemoveControlFlag/SimpleDatabaseAfter2/dbfile.txt: -------------------------------------------------------------------------------- 1 | hyuki@example.com=Hiroshi Yuki 2 | sato@example.com=Sato Hanako 3 | tomura@example.com=Tomura 4 | -------------------------------------------------------------------------------- /RemoveControlFlag/SimpleDatabaseAfter2/testfile.txt: -------------------------------------------------------------------------------- 1 | hyuki@example.com=Hiroshi Yuki 2 | sato@example.com=Sato Hanako 3 | tomura@example.com=Tomura 4 | -------------------------------------------------------------------------------- /RemoveControlFlag/SimpleDatabaseBefore/dbfile.txt: -------------------------------------------------------------------------------- 1 | hyuki@example.com=Hiroshi Yuki 2 | sato@example.com=Sato Hanako 3 | tomura@example.com=Tomura 4 | -------------------------------------------------------------------------------- /ExtractClass/immutable/ImmutableAuthor.java: -------------------------------------------------------------------------------- 1 | public interface ImmutableAuthor { 2 | public String getName(); 3 | public String getMail(); 4 | } 5 | -------------------------------------------------------------------------------- /ExtractClass/mutable/ImmutableAuthor.java: -------------------------------------------------------------------------------- 1 | public interface ImmutableAuthor { 2 | public String getName(); 3 | public String getMail(); 4 | } 5 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/typecheck/RobotCommand.java: -------------------------------------------------------------------------------- 1 | public interface RobotCommand { 2 | public abstract void apply(Robot robot); 3 | } 4 | -------------------------------------------------------------------------------- /DuplicateObservedData/after/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | new IntegerDisplay(); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /DuplicateObservedData/before/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | new IntegerDisplay(); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /DuplicateObservedData/graph/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | new IntegerDisplay(); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /DuplicateObservedData/step1/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | new IntegerDisplay(); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /DuplicateObservedData/step2/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | new IntegerDisplay(); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /DuplicateObservedData/step3/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | new IntegerDisplay(); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /HideDelegate/after/address.txt: -------------------------------------------------------------------------------- 1 | # 2 | #Sun Aug 20 23:16:58 KST 2017 3 | Hiroshi\ Yuki=hyuki@example.com 4 | Tomura=tomura@example.com 5 | Hanako\ Sato=hanako@example.com 6 | -------------------------------------------------------------------------------- /HideDelegate/again/address.txt: -------------------------------------------------------------------------------- 1 | # 2 | #Sun Aug 20 23:18:54 KST 2017 3 | Hiroshi\ Yuki=hyuki@example.com 4 | Tomura=tomura@example.com 5 | Hanako\ Sato=hanako@example.com 6 | -------------------------------------------------------------------------------- /HideDelegate/before/address.txt: -------------------------------------------------------------------------------- 1 | # 2 | #Sun Aug 20 23:07:58 KST 2017 3 | Hiroshi\ Yuki=hyuki@example.com 4 | Tomura=tomura@example.com 5 | Hanako\ Sato=hanako@example.com 6 | -------------------------------------------------------------------------------- /HideDelegate/nested/address.txt: -------------------------------------------------------------------------------- 1 | # 2 | #Sun Aug 20 23:20:44 KST 2017 3 | Hiroshi\ Yuki=hyuki@example.com 4 | Tomura=tomura@example.com 5 | Hanako\ Sato=hanako@example.com 6 | -------------------------------------------------------------------------------- /HideDelegate/step1/address.txt: -------------------------------------------------------------------------------- 1 | # 2 | #Sun Aug 20 23:13:29 KST 2017 3 | Hiroshi\ Yuki=hyuki@example.com 4 | Tomura=tomura@example.com 5 | Hanako\ Sato=hanako@example.com 6 | -------------------------------------------------------------------------------- /HideDelegate/step2/address.txt: -------------------------------------------------------------------------------- 1 | # 2 | #Sun Aug 20 23:14:15 KST 2017 3 | Hiroshi\ Yuki=hyuki@example.com 4 | Tomura=tomura@example.com 5 | Hanako\ Sato=hanako@example.com 6 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/AnsSmell/DigitPrinter.java: -------------------------------------------------------------------------------- 1 | public class DigitPrinter extends Printer { 2 | public void println(int n) { 3 | System.out.println(n); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/ExSmell/DigitPrinter.java: -------------------------------------------------------------------------------- 1 | public class DigitPrinter extends Printer { 2 | public void println(int n) { 3 | System.out.println(n); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /ExtractMethod/after/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Banner hello = new Banner("Hello, World!"); 4 | hello.print(3); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /ExtractMethod/step1/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Banner hello = new Banner("Hello, World!"); 4 | hello.print(3); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /ExtractMethod/step2/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Banner hello = new Banner("Hello, World!"); 4 | hello.print(3); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/AnsSmell2/DigitPrinter.java: -------------------------------------------------------------------------------- 1 | public class DigitPrinter extends Printer { 2 | public void println(int n) { 3 | System.out.println(n); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /ExtractMethod/before/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Banner hello = new Banner("Hello, World!"); 4 | hello.print(3); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/after/StateLogging.java: -------------------------------------------------------------------------------- 1 | public class StateLogging extends State { 2 | @Override public int getTypeCode() { 3 | return Logger.STATE_LOGGING; 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/after/StateStopped.java: -------------------------------------------------------------------------------- 1 | public class StateStopped extends State { 2 | @Override public int getTypeCode() { 3 | return Logger.STATE_STOPPED; 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/step1/StateLogging.java: -------------------------------------------------------------------------------- 1 | public class StateLogging extends State { 2 | @Override public int getTypeCode() { 3 | return Logger.STATE_LOGGING; 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/step1/StateStopped.java: -------------------------------------------------------------------------------- 1 | public class StateStopped extends State { 2 | @Override public int getTypeCode() { 3 | return Logger.STATE_STOPPED; 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/step2/StateLogging.java: -------------------------------------------------------------------------------- 1 | public class StateLogging extends State { 2 | @Override public int getTypeCode() { 3 | return Logger.STATE_LOGGING; 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/step2/StateStopped.java: -------------------------------------------------------------------------------- 1 | public class StateStopped extends State { 2 | @Override public int getTypeCode() { 3 | return Logger.STATE_STOPPED; 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/AnsSmell/SomethingException.java: -------------------------------------------------------------------------------- 1 | public class SomethingException extends Exception { 2 | public SomethingException(String message) { 3 | super(message); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/AnsSmell/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | new Client(false).execute(); 4 | new Client(true).execute(); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/ExSmell/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | new Client(false).execute(); 4 | new Client(true).execute(); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /IntroduceNullObject/step1/NullLabel.java: -------------------------------------------------------------------------------- 1 | public class NullLabel extends Label { 2 | public NullLabel() { 3 | super("(none)"); 4 | } 5 | @Override public boolean isNull() { 6 | return true; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /IntroduceNullObject/step2/NullLabel.java: -------------------------------------------------------------------------------- 1 | public class NullLabel extends Label { 2 | public NullLabel() { 3 | super("(none)"); 4 | } 5 | @Override public boolean isNull() { 6 | return true; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/AnsSmell2/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | new Client(new DigitPrinter()).execute(); 4 | new Client(new GraphPrinter()).execute(); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /TeaseApartInheritance/after/CSVStringReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVStringReader extends CSVReader { 4 | public CSVStringReader(String string) { 5 | super(new BufferedReader(new StringReader(string))); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /TeaseApartInheritance/change/CSVStringReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVStringReader extends CSVReader { 4 | public CSVStringReader(String string) { 5 | super(new BufferedReader(new StringReader(string))); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step4/CSVStringReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVStringReader extends CSVReader { 4 | public CSVStringReader(String string) { 5 | super(new BufferedReader(new StringReader(string))); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/after/InvalidCommandException.java: -------------------------------------------------------------------------------- 1 | public class InvalidCommandException extends Exception { 2 | public InvalidCommandException(String name) { 3 | super(name); 4 | } 5 | public InvalidCommandException() { 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/inner/InvalidCommandException.java: -------------------------------------------------------------------------------- 1 | public class InvalidCommandException extends Exception { 2 | public InvalidCommandException(String name) { 3 | super(name); 4 | } 5 | public InvalidCommandException() { 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/state/InvalidCommandException.java: -------------------------------------------------------------------------------- 1 | public class InvalidCommandException extends Exception { 2 | public InvalidCommandException(String name) { 3 | super(name); 4 | } 5 | public InvalidCommandException() { 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/step1/InvalidCommandException.java: -------------------------------------------------------------------------------- 1 | public class InvalidCommandException extends Exception { 2 | public InvalidCommandException(String name) { 3 | super(name); 4 | } 5 | public InvalidCommandException() { 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/step2/InvalidCommandException.java: -------------------------------------------------------------------------------- 1 | public class InvalidCommandException extends Exception { 2 | public InvalidCommandException(String name) { 3 | super(name); 4 | } 5 | public InvalidCommandException() { 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /TeaseApartInheritance/after/CSVFileReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVFileReader extends CSVReader { 4 | public CSVFileReader(String filename) throws IOException { 5 | super(new BufferedReader(new FileReader(filename))); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /TeaseApartInheritance/change/CSVFileReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVFileReader extends CSVReader { 4 | public CSVFileReader(String filename) throws IOException { 5 | super(new BufferedReader(new FileReader(filename))); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step4/CSVFileReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVFileReader extends CSVReader { 4 | public CSVFileReader(String filename) throws IOException { 5 | super(new BufferedReader(new FileReader(filename))); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /DuplicateObservedData/after/ValueChangeEvent.java: -------------------------------------------------------------------------------- 1 | public class ValueChangeEvent { 2 | private final Value _source; 3 | public ValueChangeEvent(Value source) { 4 | _source = source; 5 | } 6 | public Value getSource() { 7 | return _source; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /DuplicateObservedData/graph/ValueChangeEvent.java: -------------------------------------------------------------------------------- 1 | public class ValueChangeEvent { 2 | private final Value _source; 3 | public ValueChangeEvent(Value source) { 4 | _source = source; 5 | } 6 | public Value getSource() { 7 | return _source; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /DuplicateObservedData/step2/ValueChangeEvent.java: -------------------------------------------------------------------------------- 1 | public class ValueChangeEvent { 2 | private final Value _source; 3 | public ValueChangeEvent(Value source) { 4 | _source = source; 5 | } 6 | public Value getSource() { 7 | return _source; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /DuplicateObservedData/step3/ValueChangeEvent.java: -------------------------------------------------------------------------------- 1 | public class ValueChangeEvent { 2 | private final Value _source; 3 | public ValueChangeEvent(Value source) { 4 | _source = source; 5 | } 6 | public Value getSource() { 7 | return _source; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/before/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Robot robot = new Robot("Andrew"); 4 | robot.order(0); // walk 5 | robot.order(1); // stop 6 | robot.order(2); // jump 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /IntroduceNullObject/after/NullLabel.java: -------------------------------------------------------------------------------- 1 | public class NullLabel extends Label { 2 | public NullLabel() { 3 | super("(none)"); 4 | } 5 | @Override public void display() { 6 | } 7 | @Override public boolean isNull() { 8 | return true; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /IntroduceNullObject/step3/NullLabel.java: -------------------------------------------------------------------------------- 1 | public class NullLabel extends Label { 2 | public NullLabel() { 3 | super("(none)"); 4 | } 5 | @Override public void display() { 6 | } 7 | @Override public boolean isNull() { 8 | return true; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /IntroduceAssertion/delete/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | private static final boolean ASSERT = false; // 어서션을 삭제하려면 false 3 | 4 | public static void main(String[] args) { 5 | int x = -123; 6 | if (ASSERT) { 7 | assert x > 0; 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /IntroduceAssertion/notdelete/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | private static final boolean ASSERT = true; // 어서션을 삭제하려면 false 3 | 4 | public static void main(String[] args) { 5 | int x = -123; 6 | if (ASSERT) { 7 | assert x > 0; 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/after/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Robot robot = new Robot("Andrew"); 4 | robot.order(Robot.COMMAND_WALK); 5 | robot.order(Robot.COMMAND_STOP); 6 | robot.order(Robot.COMMAND_JUMP); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/enum/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Robot robot = new Robot("Andrew"); 4 | robot.order(Robot.Command.WALK); 5 | robot.order(Robot.Command.STOP); 6 | robot.order(Robot.Command.JUMP); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/typecode/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Robot robot = new Robot("Andrew"); 4 | robot.order(Robot.COMMAND_WALK); 5 | robot.order(Robot.COMMAND_STOP); 6 | robot.order(Robot.COMMAND_JUMP); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/typecode/RobotCommand.java: -------------------------------------------------------------------------------- 1 | public class RobotCommand { 2 | private final String _name; 3 | public RobotCommand(String name) { 4 | _name = name; 5 | } 6 | public String toString() { 7 | return "[ RobotCommand: " + _name + " ]"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /TeaseApartInheritance/after/CSVPrinter.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public abstract class CSVPrinter { 4 | protected final CSVReader _csvReader; 5 | protected CSVPrinter(CSVReader csvReader) { 6 | _csvReader = csvReader; 7 | } 8 | public abstract void print() throws IOException; 9 | } 10 | -------------------------------------------------------------------------------- /TeaseApartInheritance/change/CSVPrinter.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public abstract class CSVPrinter { 4 | protected final CSVReader _csvReader; 5 | protected CSVPrinter(CSVReader csvReader) { 6 | _csvReader = csvReader; 7 | } 8 | public abstract void print() throws IOException; 9 | } 10 | -------------------------------------------------------------------------------- /DuplicateObservedData/step1/Value.java: -------------------------------------------------------------------------------- 1 | public class Value { 2 | private int _value = 0; 3 | public Value(int value) { 4 | _value = value; 5 | } 6 | public void setValue(int value) { 7 | _value = value; 8 | } 9 | public int getValue() { 10 | return _value; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /DuplicateObservedData/step2/Value.java: -------------------------------------------------------------------------------- 1 | public class Value { 2 | private int _value = 0; 3 | public Value(int value) { 4 | _value = value; 5 | } 6 | public void setValue(int value) { 7 | _value = value; 8 | } 9 | public int getValue() { 10 | return _value; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/after/Direction.java: -------------------------------------------------------------------------------- 1 | public class Direction { 2 | public int _x; 3 | public int _y; 4 | public Direction(int x, int y) { 5 | _x = x; 6 | _y = y; 7 | } 8 | public void setDirection(int x, int y) { 9 | _x = x; 10 | _y = y; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/before/Direction.java: -------------------------------------------------------------------------------- 1 | public class Direction { 2 | public int _x; 3 | public int _y; 4 | public Direction(int x, int y) { 5 | _x = x; 6 | _y = y; 7 | } 8 | public void setDirection(int x, int y) { 9 | _x = x; 10 | _y = y; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/inner/Direction.java: -------------------------------------------------------------------------------- 1 | public class Direction { 2 | public int _x; 3 | public int _y; 4 | public Direction(int x, int y) { 5 | _x = x; 6 | _y = y; 7 | } 8 | public void setDirection(int x, int y) { 9 | _x = x; 10 | _y = y; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/state/Direction.java: -------------------------------------------------------------------------------- 1 | public class Direction { 2 | public int _x; 3 | public int _y; 4 | public Direction(int x, int y) { 5 | _x = x; 6 | _y = y; 7 | } 8 | public void setDirection(int x, int y) { 9 | _x = x; 10 | _y = y; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/step1/Direction.java: -------------------------------------------------------------------------------- 1 | public class Direction { 2 | public int _x; 3 | public int _y; 4 | public Direction(int x, int y) { 5 | _x = x; 6 | _y = y; 7 | } 8 | public void setDirection(int x, int y) { 9 | _x = x; 10 | _y = y; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/step2/Direction.java: -------------------------------------------------------------------------------- 1 | public class Direction { 2 | public int _x; 3 | public int _y; 4 | public Direction(int x, int y) { 5 | _x = x; 6 | _y = y; 7 | } 8 | public void setDirection(int x, int y) { 9 | _x = x; 10 | _y = y; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /RemoveControlFlag/FindIntAfter2/FindInt.java: -------------------------------------------------------------------------------- 1 | public class FindInt { 2 | public static boolean find(int[] data, int target) { 3 | for (int i = 0; i < data.length; i++) { 4 | if (data[i] == target) { 5 | return true; 6 | } 7 | } 8 | return false; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/ExSmell/GraphPrinter.java: -------------------------------------------------------------------------------- 1 | public class GraphPrinter extends Printer { 2 | public void println(int n) { 3 | for (int i = 0; i < n; i++) { 4 | System.out.print("*"); 5 | } 6 | System.out.printf(" (%d)", n); 7 | System.out.println(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/after/Position.java: -------------------------------------------------------------------------------- 1 | public class Position { 2 | public int _x; 3 | public int _y; 4 | public Position(int x, int y) { 5 | _x = x; 6 | _y = y; 7 | } 8 | public void relativeMove(int dx, int dy) { 9 | _x += dx; 10 | _y += dy; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/before/Position.java: -------------------------------------------------------------------------------- 1 | public class Position { 2 | public int _x; 3 | public int _y; 4 | public Position(int x, int y) { 5 | _x = x; 6 | _y = y; 7 | } 8 | public void relativeMove(int dx, int dy) { 9 | _x += dx; 10 | _y += dy; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/inner/Position.java: -------------------------------------------------------------------------------- 1 | public class Position { 2 | public int _x; 3 | public int _y; 4 | public Position(int x, int y) { 5 | _x = x; 6 | _y = y; 7 | } 8 | public void relativeMove(int dx, int dy) { 9 | _x += dx; 10 | _y += dy; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/state/Position.java: -------------------------------------------------------------------------------- 1 | public class Position { 2 | public int _x; 3 | public int _y; 4 | public Position(int x, int y) { 5 | _x = x; 6 | _y = y; 7 | } 8 | public void relativeMove(int dx, int dy) { 9 | _x += dx; 10 | _y += dy; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/step1/Position.java: -------------------------------------------------------------------------------- 1 | public class Position { 2 | public int _x; 3 | public int _y; 4 | public Position(int x, int y) { 5 | _x = x; 6 | _y = y; 7 | } 8 | public void relativeMove(int dx, int dy) { 9 | _x += dx; 10 | _y += dy; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/step2/Position.java: -------------------------------------------------------------------------------- 1 | public class Position { 2 | public int _x; 3 | public int _y; 4 | public Position(int x, int y) { 5 | _x = x; 6 | _y = y; 7 | } 8 | public void relativeMove(int dx, int dy) { 9 | _x += dx; 10 | _y += dy; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/enum/ItemType.java: -------------------------------------------------------------------------------- 1 | public enum ItemType { 2 | BOOK(0), 3 | DVD(1), 4 | SOFTWARE(2); 5 | 6 | private final int _typecode; 7 | 8 | private ItemType(int typecode) { 9 | _typecode = typecode; 10 | } 11 | 12 | public int getTypecode() { return _typecode; } 13 | } 14 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/AnsSmell/GraphPrinter.java: -------------------------------------------------------------------------------- 1 | public class GraphPrinter extends Printer { 2 | public void println(int n) { 3 | for (int i = 0; i < n; i++) { 4 | System.out.print("*"); 5 | } 6 | System.out.printf(" (%d)", n); 7 | System.out.println(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/AnsSmell2/GraphPrinter.java: -------------------------------------------------------------------------------- 1 | public class GraphPrinter extends Printer { 2 | public void println(int n) { 3 | for (int i = 0; i < n; i++) { 4 | System.out.print("*"); 5 | } 6 | System.out.printf(" (%d)", n); 7 | System.out.println(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/step1/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Robot robot = new Robot("Andrew"); 4 | robot.order(Robot.COMMAND_WALK); // walk 5 | robot.order(Robot.COMMAND_STOP); // stop 6 | robot.order(Robot.COMMAND_JUMP); // jump 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/step2/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Robot robot = new Robot("Andrew"); 4 | robot.order(Robot.COMMAND_WALK); // walk 5 | robot.order(Robot.COMMAND_STOP); // stop 6 | robot.order(Robot.COMMAND_JUMP); // jump 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/typecheck/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Robot robot = new Robot("Andrew"); 4 | robot.order(0); // Compile error! 5 | robot.order(Robot.COMMAND_STOP); // stop 6 | robot.order(Robot.COMMAND_JUMP); // jump 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /TeaseApartInheritance/before/CSVReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.regex.*; 3 | 4 | public abstract class CSVReader { 5 | protected static final Pattern CSV_PATTERN = Pattern.compile("\\s*,\\s*"); 6 | public abstract String[] readCSV() throws IOException; 7 | public abstract void close() throws IOException; 8 | } 9 | -------------------------------------------------------------------------------- /TeaseApartInheritance/rename/CSVReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.regex.*; 3 | 4 | public abstract class CSVReader { 5 | protected static final Pattern CSV_PATTERN = Pattern.compile("\\s*,\\s*"); 6 | public abstract String[] readCSV() throws IOException; 7 | public abstract void close() throws IOException; 8 | } 9 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step2/CSVReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.regex.*; 3 | 4 | public abstract class CSVReader { 5 | protected static final Pattern CSV_PATTERN = Pattern.compile("\\s*,\\s*"); 6 | public abstract String[] readCSV() throws IOException; 7 | public abstract void close() throws IOException; 8 | } 9 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step3/CSVReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.regex.*; 3 | 4 | public abstract class CSVReader { 5 | protected static final Pattern CSV_PATTERN = Pattern.compile("\\s*,\\s*"); 6 | public abstract String[] readCSV() throws IOException; 7 | public abstract void close() throws IOException; 8 | } 9 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/AnsSmell/Printer.java: -------------------------------------------------------------------------------- 1 | public abstract class Printer { 2 | public abstract void println(int n); 3 | public static Printer create(boolean graphical) { 4 | if (graphical) { 5 | return new GraphPrinter(); 6 | } else { 7 | return new DigitPrinter(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /IntroduceNullObject/before/Label.java: -------------------------------------------------------------------------------- 1 | public class Label { 2 | private final String _label; 3 | public Label(String label) { 4 | _label = label; 5 | } 6 | public void display() { 7 | System.out.println("display: " + _label); 8 | } 9 | public String toString() { 10 | return "\"" + _label + "\""; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /RemoveControlFlag/FindIntBefore/FindInt.java: -------------------------------------------------------------------------------- 1 | public class FindInt { 2 | public static boolean find(int[] data, int target) { 3 | boolean flag = false; 4 | for (int i = 0; i < data.length && !flag; i++) { 5 | if (data[i] == target) { 6 | flag = true; 7 | } 8 | } 9 | return flag; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/AnsSmellEnum/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | LunchSet lunch_a_juice = new LunchSet(LunchType.A, DrinkType.JUICE); 4 | LunchSet lunch_b_coffee = new LunchSet(LunchType.B, DrinkType.COFFEE); 5 | System.out.println(lunch_a_juice); 6 | System.out.println(lunch_b_coffee); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /TeaseApartInheritance/rename/CSVPrinter.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public abstract class CSVPrinter { 4 | protected final CSVReader _csvReader; 5 | protected CSVPrinter(CSVReader csvReader) { 6 | _csvReader = csvReader; 7 | } 8 | public abstract String[] readCSV() throws IOException; 9 | public abstract void print() throws IOException; 10 | } 11 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step2/CSVPrinter.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public abstract class CSVPrinter { 4 | protected final CSVReader _csvReader; 5 | protected CSVPrinter(CSVReader csvReader) { 6 | _csvReader = csvReader; 7 | } 8 | public abstract String[] readCSV() throws IOException; 9 | public abstract void print() throws IOException; 10 | } 11 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step3/CSVPrinter.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public abstract class CSVPrinter { 4 | protected final CSVReader _csvReader; 5 | protected CSVPrinter(CSVReader csvReader) { 6 | _csvReader = csvReader; 7 | } 8 | public abstract String[] readCSV() throws IOException; 9 | public abstract void print() throws IOException; 10 | } 11 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step4/CSVPrinter.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public abstract class CSVPrinter { 4 | protected final CSVReader _csvReader; 5 | protected CSVPrinter(CSVReader csvReader) { 6 | _csvReader = csvReader; 7 | } 8 | public abstract String[] readCSV() throws IOException; 9 | public abstract void print() throws IOException; 10 | } 11 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/AnsSmellClass/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | LunchSet lunch_a_juice = new LunchSet(LunchType.A, DrinkType.JUICE); 4 | LunchSet lunch_b_coffee = new LunchSet(LunchType.B, DrinkType.COFFEE); 5 | System.out.println(lunch_a_juice); 6 | System.out.println(lunch_b_coffee); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/AnsSmell2/Client.java: -------------------------------------------------------------------------------- 1 | public class Client { 2 | private final Printer _printer; 3 | public Client(Printer printer) { 4 | _printer = printer; 5 | } 6 | public void execute() { 7 | int[] table = { 3, 1, 4, 1, 5, 9 }; 8 | for (int n : table) { 9 | _printer.println(n); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /RemoveControlFlag/FindIntAfter1/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | int[] data = { 4 | 1, 9, 0, 2, 8, 5, 6, 3, 4, 7, 5 | }; 6 | if (FindInt.find(data, 5)) { 7 | System.out.println("Found!"); 8 | } else { 9 | System.out.println("Not found..."); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /RemoveControlFlag/FindIntAfter2/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | int[] data = { 4 | 1, 9, 0, 2, 8, 5, 6, 3, 4, 7, 5 | }; 6 | if (FindInt.find(data, 5)) { 7 | System.out.println("Found!"); 8 | } else { 9 | System.out.println("Not found..."); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /RemoveControlFlag/FindIntBefore/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | int[] data = { 4 | 1, 9, 0, 2, 8, 5, 6, 3, 4, 7, 5 | }; 6 | if (FindInt.find(data, 5)) { 7 | System.out.println("Found!"); 8 | } else { 9 | System.out.println("Not found..."); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /RemoveControlFlag/FindIntAfter1/FindInt.java: -------------------------------------------------------------------------------- 1 | public class FindInt { 2 | public static boolean find(int[] data, int target) { 3 | boolean found = false; 4 | for (int i = 0; i < data.length; i++) { 5 | if (data[i] == target) { 6 | found = true; 7 | break; 8 | } 9 | } 10 | return found; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/ExSmell/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Player[] players = { 4 | new Player(Player.MUSIC), 5 | new Player(Player.VIDEO), 6 | }; 7 | 8 | for (Player player : players) { 9 | player.play(); 10 | player.stop(); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/AnsSmell/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Player[] players = { 4 | Player.create(Player.MUSIC), 5 | Player.create(Player.VIDEO), 6 | }; 7 | 8 | for (Player player : players) { 9 | player.play(); 10 | player.stop(); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/AnsSmell/Client.java: -------------------------------------------------------------------------------- 1 | public class Client { 2 | private final Printer _printer; 3 | public Client(boolean graphical) { 4 | _printer = Printer.create(graphical); 5 | } 6 | public void execute() { 7 | int[] table = { 3, 1, 4, 1, 5, 9 }; 8 | for (int n : table) { 9 | _printer.println(n); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/ExSmell/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | LunchSet lunch_a_juice = new LunchSet(LunchSet.LUNCH_TYPE_A, LunchSet.DRINK_TYPE_JUICE); 4 | LunchSet lunch_b_coffee = new LunchSet(LunchSet.LUNCH_TYPE_B, LunchSet.DRINK_TYPE_COFFEE); 5 | System.out.println(lunch_a_juice); 6 | System.out.println(lunch_b_coffee); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /ExtractClass/immutable/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Author mutable = new Author("Mr.Mutable", "mutable@example.com"); 4 | ImmutableAuthor immutable = new Author("Mr.Immutable", "immutable@example.com"); 5 | 6 | // mutable.setName("Mr.Mutable Jr."); 7 | immutable.setName("Mr.Immutable Jr."); // 컴파일 에러! 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /ExtractClass/mutable/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Author mutable = new Author("Mr.Mutable", "mutable@example.com"); 4 | ImmutableAuthor immutable = new Author("Mr.Immutable", "immutable@example.com"); 5 | 6 | mutable.setName("Mr.Mutable Jr."); 7 | // immutable.setName("Mr.Immutable Jr."); // 컴파일 에러! 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/AnsSmell/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Something something = new Something(); 4 | try { 5 | something.method1(); 6 | something.method2(); 7 | something.method3(); 8 | } catch (SomethingException e) { 9 | System.out.println(e.getMessage()); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /IntroduceNullObject/after/Label.java: -------------------------------------------------------------------------------- 1 | public class Label { 2 | private final String _label; 3 | public Label(String label) { 4 | _label = label; 5 | } 6 | public void display() { 7 | System.out.println("display: " + _label); 8 | } 9 | public String toString() { 10 | return "\"" + _label + "\""; 11 | } 12 | public boolean isNull() { 13 | return false; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /IntroduceNullObject/step1/Label.java: -------------------------------------------------------------------------------- 1 | public class Label { 2 | private final String _label; 3 | public Label(String label) { 4 | _label = label; 5 | } 6 | public void display() { 7 | System.out.println("display: " + _label); 8 | } 9 | public String toString() { 10 | return "\"" + _label + "\""; 11 | } 12 | public boolean isNull() { 13 | return false; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /IntroduceNullObject/step2/Label.java: -------------------------------------------------------------------------------- 1 | public class Label { 2 | private final String _label; 3 | public Label(String label) { 4 | _label = label; 5 | } 6 | public void display() { 7 | System.out.println("display: " + _label); 8 | } 9 | public String toString() { 10 | return "\"" + _label + "\""; 11 | } 12 | public boolean isNull() { 13 | return false; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /IntroduceNullObject/step3/Label.java: -------------------------------------------------------------------------------- 1 | public class Label { 2 | private final String _label; 3 | public Label(String label) { 4 | _label = label; 5 | } 6 | public void display() { 7 | System.out.println("display: " + _label); 8 | } 9 | public String toString() { 10 | return "\"" + _label + "\""; 11 | } 12 | public boolean isNull() { 13 | return false; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/AnsSmell/Something.java: -------------------------------------------------------------------------------- 1 | public class Something { 2 | public void method1() throws SomethingException { 3 | // ... 4 | } 5 | public void method2() throws SomethingException { 6 | // ... 7 | } 8 | public void method3() throws SomethingException { 9 | // ... 10 | throw new SomethingException("method3: file open error."); 11 | } 12 | // ... 13 | } 14 | -------------------------------------------------------------------------------- /HideDelegate/before/AddressFile.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class AddressFile { 4 | private final Database _database; 5 | public AddressFile(String filename) { 6 | _database = new Database(filename); 7 | } 8 | public Database getDatabase() { 9 | return _database; 10 | } 11 | public Enumeration names() { 12 | return _database.getProperties().propertyNames(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/after/ItemType.java: -------------------------------------------------------------------------------- 1 | public class ItemType { 2 | public static final ItemType BOOK = new ItemType(0); 3 | public static final ItemType DVD = new ItemType(1); 4 | public static final ItemType SOFTWARE = new ItemType(2); 5 | 6 | private final int _typecode; 7 | 8 | private ItemType(int typecode) { 9 | _typecode = typecode; 10 | } 11 | 12 | public int getTypecode() { return _typecode; } 13 | } 14 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/step2/ItemType.java: -------------------------------------------------------------------------------- 1 | public class ItemType { 2 | public static final ItemType BOOK = new ItemType(0); 3 | public static final ItemType DVD = new ItemType(1); 4 | public static final ItemType SOFTWARE = new ItemType(2); 5 | 6 | private final int _typecode; 7 | 8 | private ItemType(int typecode) { 9 | _typecode = typecode; 10 | } 11 | 12 | public int getTypecode() { return _typecode; } 13 | } 14 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/subclass/ShapeLine.java: -------------------------------------------------------------------------------- 1 | public class ShapeLine extends Shape { 2 | public ShapeLine(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public String getName() { 7 | return "LINE"; 8 | } 9 | 10 | @Override public void draw() { 11 | System.out.println("drawLine: " + this.toString()); 12 | // ... 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/subclass/ShapeOval.java: -------------------------------------------------------------------------------- 1 | public class ShapeOval extends Shape { 2 | public ShapeOval(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public String getName() { 7 | return "OVAL"; 8 | } 9 | 10 | @Override public void draw() { 11 | System.out.println("drawOval: " + this.toString()); 12 | // ... 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ReplaceInheritanceWithDelegation/this/Dice.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Dice { 4 | private final Random _random; 5 | public Dice() { 6 | this(314159L); 7 | } 8 | public Dice(long seed) { 9 | _random = new Random(seed); 10 | } 11 | public int nextInt() { 12 | return _random.nextInt(6) + 1; 13 | } 14 | public void setSeed(long seed) { 15 | _random.setSeed(seed); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/AnsSmell/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Player player = new Player(); 4 | 5 | player.play(); 6 | 7 | player.setMedia(new Video()); 8 | player.play(); 9 | player.stop(); 10 | 11 | player.setMedia(new Music()); 12 | player.loop(); 13 | player.stop(); 14 | 15 | player.setMedia(null); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/ExSmell/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Player player = new Player(); 4 | 5 | player.play(); 6 | 7 | player.setMedia(Media.VIDEO); 8 | player.play(); 9 | player.stop(); 10 | 11 | player.setMedia(Media.MUSIC); 12 | player.loop(); 13 | player.stop(); 14 | 15 | player.setMedia(Media.NULL); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/AnsSmell/MusicPlayer.java: -------------------------------------------------------------------------------- 1 | public class MusicPlayer extends Player { 2 | @Override public void play() { 3 | System.out.println("Music: play"); 4 | // 음악 재생 코드... 5 | } 6 | @Override public void loop() { 7 | System.out.println("Music: loop"); 8 | // 음악 반복 재생 코드... 9 | } 10 | @Override public void stop() { 11 | System.out.println("Music: stop"); 12 | // 음악 정지 코드... 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/subclass/ShapeRectangle.java: -------------------------------------------------------------------------------- 1 | public class ShapeRectangle extends Shape { 2 | public ShapeRectangle(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public String getName() { 7 | return "RECTANGLE"; 8 | } 9 | 10 | @Override public void draw() { 11 | System.out.println("drawRectangle: " + this.toString()); 12 | // ... 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ReplaceInheritanceWithDelegation/after/Dice.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Dice { 4 | private final Random _random; 5 | public Dice() { 6 | _random = new Random(314159L); 7 | } 8 | public Dice(long seed) { 9 | _random = new Random(seed); 10 | } 11 | public int nextInt() { 12 | return _random.nextInt(6) + 1; 13 | } 14 | public void setSeed(long seed) { 15 | _random.setSeed(seed); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ReplaceInheritanceWithDelegation/step2/Dice.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Dice { 4 | private final Random _random; 5 | public Dice() { 6 | _random = new Random(314159L); 7 | } 8 | public Dice(long seed) { 9 | _random = new Random(seed); 10 | } 11 | public int nextInt() { 12 | return _random.nextInt(6) + 1; 13 | } 14 | public void setSeed(long seed) { 15 | _random.setSeed(seed); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/AnsSmell/VideoPlayer.java: -------------------------------------------------------------------------------- 1 | public class VideoPlayer extends Player { 2 | @Override public void play() { 3 | System.out.println("Video: play"); 4 | // 비디오 재생 코드... 5 | } 6 | @Override public void loop() { 7 | System.out.println("Video: loop"); 8 | // 비디오 반복 재생 코드... 9 | } 10 | @Override public void stop() { 11 | System.out.println("Video: stop"); 12 | // 비디오 정지 코드... 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/factory/ShapeLine.java: -------------------------------------------------------------------------------- 1 | public class ShapeLine extends Shape { 2 | public ShapeLine(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public String getName() { return "LINE"; } 7 | 8 | @Override public void draw() { drawLine(); } 9 | 10 | private void drawLine() { 11 | System.out.println("drawLine: " + this.toString()); 12 | // ... 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/factory/ShapeOval.java: -------------------------------------------------------------------------------- 1 | public class ShapeOval extends Shape { 2 | public ShapeOval(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public String getName() { return "OVAL"; } 7 | 8 | @Override public void draw() { drawOval(); } 9 | 10 | private void drawOval() { 11 | System.out.println("drawOval: " + this.toString()); 12 | // ... 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/manyfactory/ShapeLine.java: -------------------------------------------------------------------------------- 1 | public class ShapeLine extends Shape { 2 | public ShapeLine(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public String getName() { return "LINE"; } 7 | 8 | @Override public void draw() { drawLine(); } 9 | 10 | private void drawLine() { 11 | System.out.println("drawLine: " + this.toString()); 12 | // ... 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/manyfactory/ShapeOval.java: -------------------------------------------------------------------------------- 1 | public class ShapeOval extends Shape { 2 | public ShapeOval(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public String getName() { return "OVAL"; } 7 | 8 | @Override public void draw() { drawOval(); } 9 | 10 | private void drawOval() { 11 | System.out.println("drawOval: " + this.toString()); 12 | // ... 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ExtractClass/mutable/Author.java: -------------------------------------------------------------------------------- 1 | public class Author implements ImmutableAuthor { 2 | private String _name; 3 | private String _mail; 4 | public Author(String name, String mail) { 5 | _name = name; 6 | _mail = mail; 7 | } 8 | public String getName() { return _name; } 9 | public String getMail() { return _mail; } 10 | public void setName(String name) { _name = name; } 11 | public void setMail(String mail) { _mail = mail; } 12 | // ... 13 | } 14 | -------------------------------------------------------------------------------- /ExtractClass/immutable/Author.java: -------------------------------------------------------------------------------- 1 | public class Author implements ImmutableAuthor { 2 | private String _name; 3 | private String _mail; 4 | public Author(String name, String mail) { 5 | _name = name; 6 | _mail = mail; 7 | } 8 | public String getName() { return _name; } 9 | public String getMail() { return _mail; } 10 | public void setName(String name) { _name = name; } 11 | public void setMail(String mail) { _mail = mail; } 12 | // ... 13 | } 14 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/ExSmell/Client.java: -------------------------------------------------------------------------------- 1 | public class Client { 2 | private final Printer _printer; 3 | public Client(boolean graphical) { 4 | if (graphical) { 5 | _printer = new GraphPrinter(); 6 | } else { 7 | _printer = new DigitPrinter(); 8 | } 9 | } 10 | public void execute() { 11 | int[] table = { 3, 1, 4, 1, 5, 9 }; 12 | for (int n : table) { 13 | _printer.println(n); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/manyfactory/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Shape line = Shape.createShapeLine(0, 0, 100, 200); 4 | Shape rectangle = Shape.createShapeRectangle(10, 20, 30, 40); 5 | Shape oval = Shape.createShapeOval(100, 200, 300, 400); 6 | 7 | Shape[] shape = { 8 | line, rectangle, oval 9 | }; 10 | 11 | for (Shape s : shape) { 12 | s.draw(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/factory/ShapeRectangle.java: -------------------------------------------------------------------------------- 1 | public class ShapeRectangle extends Shape { 2 | public ShapeRectangle(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public String getName() { return "RECTANGLE"; } 7 | 8 | @Override public void draw() { drawRectangle(); } 9 | 10 | private void drawRectangle() { 11 | System.out.println("drawRectangle: " + this.toString()); 12 | // ... 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/manyfactory/ShapeRectangle.java: -------------------------------------------------------------------------------- 1 | public class ShapeRectangle extends Shape { 2 | public ShapeRectangle(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public String getName() { return "RECTANGLE"; } 7 | 8 | @Override public void draw() { drawRectangle(); } 9 | 10 | private void drawRectangle() { 11 | System.out.println("drawRectangle: " + this.toString()); 12 | // ... 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/after/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Logger logger = new Logger(); 4 | logger.log("information #1"); 5 | 6 | logger.start(); 7 | logger.log("information #2"); 8 | 9 | logger.start(); 10 | logger.log("information #3"); 11 | 12 | logger.stop(); 13 | logger.log("information #4"); 14 | 15 | logger.stop(); 16 | logger.log("information #5"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/before/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Logger logger = new Logger(); 4 | logger.log("information #1"); 5 | 6 | logger.start(); 7 | logger.log("information #2"); 8 | 9 | logger.start(); 10 | logger.log("information #3"); 11 | 12 | logger.stop(); 13 | logger.log("information #4"); 14 | 15 | logger.stop(); 16 | logger.log("information #5"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/enum1/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Logger logger = new Logger(); 4 | logger.log("information #1"); 5 | 6 | logger.start(); 7 | logger.log("information #2"); 8 | 9 | logger.start(); 10 | logger.log("information #3"); 11 | 12 | logger.stop(); 13 | logger.log("information #4"); 14 | 15 | logger.stop(); 16 | logger.log("information #5"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/enum2/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Logger logger = new Logger(); 4 | logger.log("information #1"); 5 | 6 | logger.start(); 7 | logger.log("information #2"); 8 | 9 | logger.start(); 10 | logger.log("information #3"); 11 | 12 | logger.stop(); 13 | logger.log("information #4"); 14 | 15 | logger.stop(); 16 | logger.log("information #5"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/enum3/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Logger logger = new Logger(); 4 | logger.log("information #1"); 5 | 6 | logger.start(); 7 | logger.log("information #2"); 8 | 9 | logger.start(); 10 | logger.log("information #3"); 11 | 12 | logger.stop(); 13 | logger.log("information #4"); 14 | 15 | logger.stop(); 16 | logger.log("information #5"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/step1/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Logger logger = new Logger(); 4 | logger.log("information #1"); 5 | 6 | logger.start(); 7 | logger.log("information #2"); 8 | 9 | logger.start(); 10 | logger.log("information #3"); 11 | 12 | logger.stop(); 13 | logger.log("information #4"); 14 | 15 | logger.stop(); 16 | logger.log("information #5"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/step2/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Logger logger = new Logger(); 4 | logger.log("information #1"); 5 | 6 | logger.start(); 7 | logger.log("information #2"); 8 | 9 | logger.start(); 10 | logger.log("information #3"); 11 | 12 | logger.stop(); 13 | logger.log("information #4"); 14 | 15 | logger.stop(); 16 | logger.log("information #5"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/before/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Shape line = new Shape(Shape.TYPECODE_LINE, 0, 0, 100, 200); 4 | Shape rectangle = new Shape(Shape.TYPECODE_RECTANGLE, 10, 20, 30, 40); 5 | Shape oval = new Shape(Shape.TYPECODE_OVAL, 100, 200, 300, 400); 6 | 7 | Shape[] shape = { 8 | line, rectangle, oval 9 | }; 10 | 11 | for (Shape s : shape) { 12 | s.draw(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/subclass/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Shape line = Shape.createLine(0, 0, 100, 200); 4 | Shape rectangle = Shape.createRectangle(10, 20, 30, 40); 5 | Shape oval = Shape.createOval(100, 200, 300, 400); 6 | 7 | Shape[] shape = { 8 | line, 9 | rectangle, 10 | oval 11 | }; 12 | 13 | for (Shape s : shape) { 14 | s.draw(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /IntroduceNullObject/after/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Person[] people = { 4 | new Person(new Label("Alice"), new Label("alice@example.com")), 5 | new Person(new Label("Bobby"), new Label("bobby@example.com")), 6 | new Person(new Label("Chris")), 7 | }; 8 | 9 | for (Person p : people) { 10 | System.out.println(p.toString()); 11 | p.display(); 12 | System.out.println(""); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /IntroduceNullObject/before/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Person[] people = { 4 | new Person(new Label("Alice"), new Label("alice@example.com")), 5 | new Person(new Label("Bobby"), new Label("bobby@example.com")), 6 | new Person(new Label("Chris")), 7 | }; 8 | 9 | for (Person p : people) { 10 | System.out.println(p.toString()); 11 | p.display(); 12 | System.out.println(""); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /IntroduceNullObject/field/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Person[] people = { 4 | new Person(new Label("Alice"), new Label("alice@example.com")), 5 | new Person(new Label("Bobby"), new Label("bobby@example.com")), 6 | new Person(new Label("Chris")), 7 | }; 8 | 9 | for (Person p : people) { 10 | System.out.println(p.toString()); 11 | p.display(); 12 | System.out.println(""); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /IntroduceNullObject/field/Person.java: -------------------------------------------------------------------------------- 1 | public class Person { 2 | private final Label _name; 3 | private final Label _mail; 4 | public Person(Label name, Label mail) { 5 | _name = name; 6 | _mail = mail; 7 | } 8 | public Person(Label name) { 9 | this(name, Label.NULL); 10 | } 11 | public void display() { 12 | _name.display(); 13 | _mail.display(); 14 | } 15 | public String toString() { 16 | return "[ Person: name=" + _name + " mail=" + _mail + " ]"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /IntroduceNullObject/nested/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Person[] people = { 4 | new Person(new Label("Alice"), new Label("alice@example.com")), 5 | new Person(new Label("Bobby"), new Label("bobby@example.com")), 6 | new Person(new Label("Chris")), 7 | }; 8 | 9 | for (Person p : people) { 10 | System.out.println(p.toString()); 11 | p.display(); 12 | System.out.println(""); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /IntroduceNullObject/step1/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Person[] people = { 4 | new Person(new Label("Alice"), new Label("alice@example.com")), 5 | new Person(new Label("Bobby"), new Label("bobby@example.com")), 6 | new Person(new Label("Chris")), 7 | }; 8 | 9 | for (Person p : people) { 10 | System.out.println(p.toString()); 11 | p.display(); 12 | System.out.println(""); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /IntroduceNullObject/step2/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Person[] people = { 4 | new Person(new Label("Alice"), new Label("alice@example.com")), 5 | new Person(new Label("Bobby"), new Label("bobby@example.com")), 6 | new Person(new Label("Chris")), 7 | }; 8 | 9 | for (Person p : people) { 10 | System.out.println(p.toString()); 11 | p.display(); 12 | System.out.println(""); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /IntroduceNullObject/step3/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Person[] people = { 4 | new Person(new Label("Alice"), new Label("alice@example.com")), 5 | new Person(new Label("Bobby"), new Label("bobby@example.com")), 6 | new Person(new Label("Chris")), 7 | }; 8 | 9 | for (Person p : people) { 10 | System.out.println(p.toString()); 11 | p.display(); 12 | System.out.println(""); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /IntroduceNullObject/after/Person.java: -------------------------------------------------------------------------------- 1 | public class Person { 2 | private final Label _name; 3 | private final Label _mail; 4 | public Person(Label name, Label mail) { 5 | _name = name; 6 | _mail = mail; 7 | } 8 | public Person(Label name) { 9 | this(name, new NullLabel()); 10 | } 11 | public void display() { 12 | _name.display(); 13 | _mail.display(); 14 | } 15 | public String toString() { 16 | return "[ Person: name=" + _name + " mail=" + _mail + " ]"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /IntroduceNullObject/nested/Person.java: -------------------------------------------------------------------------------- 1 | public class Person { 2 | private final Label _name; 3 | private final Label _mail; 4 | public Person(Label name, Label mail) { 5 | _name = name; 6 | _mail = mail; 7 | } 8 | public Person(Label name) { 9 | this(name, Label.newNull()); 10 | } 11 | public void display() { 12 | _name.display(); 13 | _mail.display(); 14 | } 15 | public String toString() { 16 | return "[ Person: name=" + _name + " mail=" + _mail + " ]"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/ExSmell/Something.java: -------------------------------------------------------------------------------- 1 | public class Something { 2 | private String _errorMessage; 3 | public String getErrorMessage() { 4 | return _errorMessage; 5 | } 6 | public boolean method1() { 7 | // ... 8 | return true; 9 | } 10 | public boolean method2() { 11 | // ... 12 | return true; 13 | } 14 | public boolean method3() { 15 | // ... 16 | _errorMessage = "method3: file open error."; 17 | return false; 18 | } 19 | // ... 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/after/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Shape line = Shape.createShape(Shape.TYPECODE_LINE, 0, 0, 100, 200); 4 | Shape rectangle = Shape.createShape(Shape.TYPECODE_RECTANGLE, 10, 20, 30, 40); 5 | Shape oval = Shape.createShape(Shape.TYPECODE_OVAL, 100, 200, 300, 400); 6 | 7 | Shape[] shape = { 8 | line, rectangle, oval 9 | }; 10 | 11 | for (Shape s : shape) { 12 | s.draw(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/step1/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Shape line = Shape.createShape(Shape.TYPECODE_LINE, 0, 0, 100, 200); 4 | Shape rectangle = Shape.createShape(Shape.TYPECODE_RECTANGLE, 10, 20, 30, 40); 5 | Shape oval = Shape.createShape(Shape.TYPECODE_OVAL, 100, 200, 300, 400); 6 | 7 | Shape[] shape = { 8 | line, rectangle, oval 9 | }; 10 | 11 | for (Shape s : shape) { 12 | s.draw(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/step2/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Shape line = Shape.createShape(Shape.TYPECODE_LINE, 0, 0, 100, 200); 4 | Shape rectangle = Shape.createShape(Shape.TYPECODE_RECTANGLE, 10, 20, 30, 40); 5 | Shape oval = Shape.createShape(Shape.TYPECODE_OVAL, 100, 200, 300, 400); 6 | 7 | Shape[] shape = { 8 | line, rectangle, oval 9 | }; 10 | 11 | for (Shape s : shape) { 12 | s.draw(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/after/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Robot robot = new Robot("Andrew"); 4 | System.out.println(robot.toString()); 5 | 6 | robot.execute("forward right forward"); 7 | System.out.println(robot.toString()); 8 | 9 | robot.execute("left backward left forward"); 10 | System.out.println(robot.toString()); 11 | 12 | robot.execute("right forward forward farvard"); 13 | System.out.println(robot.toString()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/before/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Robot robot = new Robot("Andrew"); 4 | System.out.println(robot.toString()); 5 | 6 | robot.execute("forward right forward"); 7 | System.out.println(robot.toString()); 8 | 9 | robot.execute("left backward left forward"); 10 | System.out.println(robot.toString()); 11 | 12 | robot.execute("right forward forward farvard"); 13 | System.out.println(robot.toString()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/inner/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Robot robot = new Robot("Andrew"); 4 | System.out.println(robot.toString()); 5 | 6 | robot.execute("forward right forward"); 7 | System.out.println(robot.toString()); 8 | 9 | robot.execute("left backward left forward"); 10 | System.out.println(robot.toString()); 11 | 12 | robot.execute("right forward forward farvard"); 13 | System.out.println(robot.toString()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/state/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Robot robot = new Robot("Andrew"); 4 | System.out.println(robot.toString()); 5 | 6 | robot.execute("forward right forward"); 7 | System.out.println(robot.toString()); 8 | 9 | robot.execute("left backward left forward"); 10 | System.out.println(robot.toString()); 11 | 12 | robot.execute("right forward forward farvard"); 13 | System.out.println(robot.toString()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/step1/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Robot robot = new Robot("Andrew"); 4 | System.out.println(robot.toString()); 5 | 6 | robot.execute("forward right forward"); 7 | System.out.println(robot.toString()); 8 | 9 | robot.execute("left backward left forward"); 10 | System.out.println(robot.toString()); 11 | 12 | robot.execute("right forward forward farvard"); 13 | System.out.println(robot.toString()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/step2/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Robot robot = new Robot("Andrew"); 4 | System.out.println(robot.toString()); 5 | 6 | robot.execute("forward right forward"); 7 | System.out.println(robot.toString()); 8 | 9 | robot.execute("left backward left forward"); 10 | System.out.println(robot.toString()); 11 | 12 | robot.execute("right forward forward farvard"); 13 | System.out.println(robot.toString()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/step2/ShapeLine.java: -------------------------------------------------------------------------------- 1 | public class ShapeLine extends Shape { 2 | public ShapeLine(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public int getTypecode() { return Shape.TYPECODE_LINE; } 7 | 8 | @Override public String getName() { return "LINE"; } 9 | 10 | @Override public void draw() { drawLine(); } 11 | 12 | private void drawLine() { 13 | System.out.println("drawLine: " + this.toString()); 14 | // ... 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/step2/ShapeOval.java: -------------------------------------------------------------------------------- 1 | public class ShapeOval extends Shape { 2 | public ShapeOval(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public int getTypecode() { return Shape.TYPECODE_OVAL; } 7 | 8 | @Override public String getName() { return "OVAL"; } 9 | 10 | @Override public void draw() { drawOval(); } 11 | 12 | private void drawOval() { 13 | System.out.println("drawOval: " + this.toString()); 14 | // ... 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/after/ShapeLine.java: -------------------------------------------------------------------------------- 1 | public class ShapeLine extends Shape { 2 | protected ShapeLine(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public int getTypecode() { return Shape.TYPECODE_LINE; } 7 | 8 | @Override public String getName() { return "LINE"; } 9 | 10 | @Override public void draw() { drawLine(); } 11 | 12 | private void drawLine() { 13 | System.out.println("drawLine: " + this.toString()); 14 | // ... 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/after/ShapeOval.java: -------------------------------------------------------------------------------- 1 | public class ShapeOval extends Shape { 2 | protected ShapeOval(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public int getTypecode() { return Shape.TYPECODE_OVAL; } 7 | 8 | @Override public String getName() { return "OVAL"; } 9 | 10 | @Override public void draw() { drawOval(); } 11 | 12 | private void drawOval() { 13 | System.out.println("drawOval: " + this.toString()); 14 | // ... 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /UnitTest/failure/Person.java: -------------------------------------------------------------------------------- 1 | public class Person { 2 | private String _name; 3 | private String _mail; 4 | 5 | public Person(String name, String mail) { 6 | _name = name; 7 | _mail = mail; 8 | } 9 | 10 | public String getName() { return _name; } 11 | public String getMail() { return _mail; } 12 | 13 | public void setName(String name) { _name = name; } 14 | public void setMail(String mail) { _mail = mail; } 15 | 16 | public String toString() { 17 | return _name + " <" + _mail + ")"; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /UnitTest/sample/Person.java: -------------------------------------------------------------------------------- 1 | public class Person { 2 | private String _name; 3 | private String _mail; 4 | 5 | public Person(String name, String mail) { 6 | _name = name; 7 | _mail = mail; 8 | } 9 | 10 | public String getName() { return _name; } 11 | public String getMail() { return _mail; } 12 | 13 | public void setName(String name) { _name = name; } 14 | public void setMail(String mail) { _mail = mail; } 15 | 16 | public String toString() { 17 | return _name + " <" + _mail + ">"; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/before/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Shape line = new Shape(Shape.TYPECODE_LINE, 0, 0, 100, 200); 4 | Shape rectangle = new Shape(Shape.TYPECODE_RECTANGLE, 10, 20, 30, 40); 5 | Shape oval = new Shape(Shape.TYPECODE_OVAL, 100, 200, 300, 400); 6 | 7 | Shape[] shape = { 8 | line, 9 | rectangle, 10 | oval, 11 | }; 12 | 13 | for (Shape s : shape) { 14 | s.draw(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/enum2/Logger.java: -------------------------------------------------------------------------------- 1 | public class Logger { 2 | private State _state; 3 | public Logger() { 4 | setState(State.STATE_STOPPED); 5 | } 6 | public void setState(State state) { 7 | _state = state; 8 | } 9 | public void start() { 10 | _state.start(); 11 | setState(State.STATE_LOGGING); 12 | } 13 | public void stop() { 14 | _state.stop(); 15 | setState(State.STATE_STOPPED); 16 | } 17 | public void log(String info) { 18 | _state.log(info); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DuplicateObservedData/after/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | 5 | public class MainTest { 6 | @Test public void testMain() { 7 | IntegerDisplay display = new IntegerDisplay(); 8 | display.setValue(1); 9 | int actual = display.getValue(); 10 | int expected = 1; 11 | assertEquals(expected, actual); 12 | } 13 | 14 | public static junit.framework.Test suite() { 15 | return new JUnit4TestAdapter(MainTest.class); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DuplicateObservedData/before/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | 5 | public class MainTest { 6 | @Test public void testMain() { 7 | IntegerDisplay display = new IntegerDisplay(); 8 | display.setValue(1); 9 | int actual = display.getValue(); 10 | int expected = 1; 11 | assertEquals(expected, actual); 12 | } 13 | 14 | public static junit.framework.Test suite() { 15 | return new JUnit4TestAdapter(MainTest.class); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DuplicateObservedData/graph/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | 5 | public class MainTest { 6 | @Test public void testMain() { 7 | IntegerDisplay display = new IntegerDisplay(); 8 | display.setValue(1); 9 | int actual = display.getValue(); 10 | int expected = 1; 11 | assertEquals(expected, actual); 12 | } 13 | 14 | public static junit.framework.Test suite() { 15 | return new JUnit4TestAdapter(MainTest.class); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DuplicateObservedData/step1/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | 5 | public class MainTest { 6 | @Test public void testMain() { 7 | IntegerDisplay display = new IntegerDisplay(); 8 | display.setValue(1); 9 | int actual = display.getValue(); 10 | int expected = 1; 11 | assertEquals(expected, actual); 12 | } 13 | 14 | public static junit.framework.Test suite() { 15 | return new JUnit4TestAdapter(MainTest.class); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DuplicateObservedData/step2/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | 5 | public class MainTest { 6 | @Test public void testMain() { 7 | IntegerDisplay display = new IntegerDisplay(); 8 | display.setValue(1); 9 | int actual = display.getValue(); 10 | int expected = 1; 11 | assertEquals(expected, actual); 12 | } 13 | 14 | public static junit.framework.Test suite() { 15 | return new JUnit4TestAdapter(MainTest.class); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DuplicateObservedData/step3/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | 5 | public class MainTest { 6 | @Test public void testMain() { 7 | IntegerDisplay display = new IntegerDisplay(); 8 | display.setValue(1); 9 | int actual = display.getValue(); 10 | int expected = 1; 11 | assertEquals(expected, actual); 12 | } 13 | 14 | public static junit.framework.Test suite() { 15 | return new JUnit4TestAdapter(MainTest.class); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/after/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Shape line = Shape.create(Shape.TYPECODE_LINE, 0, 0, 100, 200); 4 | Shape rectangle = Shape.create(Shape.TYPECODE_RECTANGLE, 10, 20, 30, 40); 5 | Shape oval = Shape.create(Shape.TYPECODE_OVAL, 100, 200, 300, 400); 6 | 7 | Shape[] shape = { 8 | line, 9 | rectangle, 10 | oval, 11 | }; 12 | 13 | for (Shape s : shape) { 14 | s.draw(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/step1/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Shape line = Shape.create(Shape.TYPECODE_LINE, 0, 0, 100, 200); 4 | Shape rectangle = Shape.create(Shape.TYPECODE_RECTANGLE, 10, 20, 30, 40); 5 | Shape oval = Shape.create(Shape.TYPECODE_OVAL, 100, 200, 300, 400); 6 | 7 | Shape[] shape = { 8 | line, 9 | rectangle, 10 | oval, 11 | }; 12 | 13 | for (Shape s : shape) { 14 | s.draw(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/step2/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Shape line = Shape.create(Shape.TYPECODE_LINE, 0, 0, 100, 200); 4 | Shape rectangle = Shape.create(Shape.TYPECODE_RECTANGLE, 10, 20, 30, 40); 5 | Shape oval = Shape.create(Shape.TYPECODE_OVAL, 100, 200, 300, 400); 6 | 7 | Shape[] shape = { 8 | line, 9 | rectangle, 10 | oval, 11 | }; 12 | 13 | for (Shape s : shape) { 14 | s.draw(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/AnsSmellEnum/LunchSet.java: -------------------------------------------------------------------------------- 1 | enum LunchType { 2 | A, B, C; 3 | } 4 | 5 | enum DrinkType { 6 | COFFEE, JUICE, TEA; 7 | } 8 | 9 | class LunchSet { 10 | private final LunchType _lunchType; 11 | private final DrinkType _drinkType; 12 | 13 | public LunchSet(LunchType lunchType, DrinkType drinkType) { 14 | _lunchType = lunchType; 15 | _drinkType = drinkType; 16 | } 17 | 18 | public String toString() { 19 | return "[ LunchSet: " + _lunchType + ", " + _drinkType + " ]"; 20 | } 21 | 22 | // ... 23 | } 24 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/step1/ShapeLine.java: -------------------------------------------------------------------------------- 1 | public class ShapeLine extends Shape { 2 | public ShapeLine(int typecode, int startx, int starty, int endx, int endy) { 3 | super(typecode, startx, starty, endx, endy); 4 | } 5 | 6 | @Override public int getTypecode() { return Shape.TYPECODE_LINE; } 7 | 8 | @Override public String getName() { return "LINE"; } 9 | 10 | @Override public void draw() { drawLine(); } 11 | 12 | private void drawLine() { 13 | System.out.println("drawLine: " + this.toString()); 14 | // ... 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/step1/ShapeOval.java: -------------------------------------------------------------------------------- 1 | public class ShapeOval extends Shape { 2 | public ShapeOval(int typecode, int startx, int starty, int endx, int endy) { 3 | super(typecode, startx, starty, endx, endy); 4 | } 5 | 6 | @Override public int getTypecode() { return Shape.TYPECODE_OVAL; } 7 | 8 | @Override public String getName() { return "OVAL"; } 9 | 10 | @Override public void draw() { drawOval(); } 11 | 12 | private void drawOval() { 13 | System.out.println("drawOval: " + this.toString()); 14 | // ... 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /TeaseApartInheritance/before/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class Main { 4 | private static final String SAMPLE_CSV_STRING = 5 | "좋은 아침입니다.,Good morning.\n" 6 | + "안녕하세요~,Hello.\n" 7 | + "안녕하세요.,Good evening.\n" 8 | + "안녕히 주무세요.,Good night.\n"; 9 | 10 | private static final String SAMPLE_CSV_FILE = "file.csv"; 11 | 12 | public static void main(String[] args) throws IOException { 13 | new CSVStringTablePrinter(SAMPLE_CSV_STRING).print(); 14 | new CSVFileTreePrinter(SAMPLE_CSV_FILE).print(); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step2/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class Main { 4 | private static final String SAMPLE_CSV_STRING = 5 | "좋은 아침입니다.,Good morning.\n" 6 | + "안녕하세요~,Hello.\n" 7 | + "안녕하세요.,Good evening.\n" 8 | + "안녕히 주무세요.,Good night.\n"; 9 | 10 | private static final String SAMPLE_CSV_FILE = "file.csv"; 11 | 12 | public static void main(String[] args) throws IOException { 13 | new CSVStringTablePrinter(SAMPLE_CSV_STRING).print(); 14 | new CSVFileTreePrinter(SAMPLE_CSV_FILE).print(); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step3/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class Main { 4 | private static final String SAMPLE_CSV_STRING = 5 | "좋은 아침입니다.,Good morning.\n" 6 | + "안녕하세요~,Hello.\n" 7 | + "안녕하세요.,Good evening.\n" 8 | + "안녕히 주무세요.,Good night.\n"; 9 | 10 | private static final String SAMPLE_CSV_FILE = "file.csv"; 11 | 12 | public static void main(String[] args) throws IOException { 13 | new CSVStringTablePrinter(SAMPLE_CSV_STRING).print(); 14 | new CSVFileTreePrinter(SAMPLE_CSV_FILE).print(); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step4/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class Main { 4 | private static final String SAMPLE_CSV_STRING = 5 | "좋은 아침입니다.,Good morning.\n" 6 | + "안녕하세요~,Hello.\n" 7 | + "안녕하세요.,Good evening.\n" 8 | + "안녕히 주무세요.,Good night.\n"; 9 | 10 | private static final String SAMPLE_CSV_FILE = "file.csv"; 11 | 12 | public static void main(String[] args) throws IOException { 13 | new CSVStringTablePrinter(SAMPLE_CSV_STRING).print(); 14 | new CSVFileTreePrinter(SAMPLE_CSV_FILE).print(); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/step2/ShapeRectangle.java: -------------------------------------------------------------------------------- 1 | public class ShapeRectangle extends Shape { 2 | public ShapeRectangle(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public int getTypecode() { return Shape.TYPECODE_RECTANGLE; } 7 | 8 | @Override public String getName() { return "RECTANGLE"; } 9 | 10 | @Override public void draw() { drawRectangle(); } 11 | 12 | private void drawRectangle() { 13 | System.out.println("drawRectangle: " + this.toString()); 14 | // ... 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/after/ShapeRectangle.java: -------------------------------------------------------------------------------- 1 | public class ShapeRectangle extends Shape { 2 | protected ShapeRectangle(int startx, int starty, int endx, int endy) { 3 | super(startx, starty, endx, endy); 4 | } 5 | 6 | @Override public int getTypecode() { return Shape.TYPECODE_RECTANGLE; } 7 | 8 | @Override public String getName() { return "RECTANGLE"; } 9 | 10 | @Override public void draw() { drawRectangle(); } 11 | 12 | private void drawRectangle() { 13 | System.out.println("drawRectangle: " + this.toString()); 14 | // ... 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/AnsSmell/Player.java: -------------------------------------------------------------------------------- 1 | public abstract class Player { 2 | public static final int MUSIC = 0; 3 | public static final int VIDEO = 1; 4 | public static Player create(int type) { 5 | switch (type) { 6 | case MUSIC: 7 | return new MusicPlayer(); 8 | case VIDEO: 9 | return new VideoPlayer(); 10 | default: 11 | throw new IllegalArgumentException("type = " + type); 12 | } 13 | } 14 | public abstract void play(); 15 | public abstract void loop(); 16 | public abstract void stop(); 17 | } 18 | -------------------------------------------------------------------------------- /TeaseApartInheritance/after/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class Main { 4 | private static final String SAMPLE_CSV_STRING = 5 | "좋은 아침입니다.,Good morning.\n" 6 | + "안녕하세요~,Hello.\n" 7 | + "안녕하세요.,Good evening.\n" 8 | + "안녕히 주무세요.,Good night.\n"; 9 | 10 | private static final String SAMPLE_CSV_FILE = "file.csv"; 11 | 12 | public static void main(String[] args) throws IOException { 13 | new CSVTablePrinter(new CSVStringReader(SAMPLE_CSV_STRING)).print(); 14 | new CSVTreePrinter(new CSVFileReader(SAMPLE_CSV_FILE)).print(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/factory/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Shape line = Shape.createShape(ShapeFactory.LineFactory.getInstance(), 0, 0, 100, 200); 4 | Shape rectangle = Shape.createShape(ShapeFactory.RectangleFactory.getInstance(), 10, 20, 30, 40); 5 | Shape oval = Shape.createShape(ShapeFactory.OvalFactory.getInstance(), 100, 200, 300, 400); 6 | 7 | Shape[] shape = { 8 | line, rectangle, oval 9 | }; 10 | 11 | for (Shape s : shape) { 12 | s.draw(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TeaseApartInheritance/change/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class Main { 4 | private static final String SAMPLE_CSV_STRING = 5 | "좋은 아침입니다.,Good morning.\n" 6 | + "안녕하세요~,Hello.\n" 7 | + "안녕하세요.,Good evening.\n" 8 | + "안녕히 주무세요.,Good night.\n"; 9 | 10 | private static final String SAMPLE_CSV_FILE = "file.csv"; 11 | 12 | public static void main(String[] args) throws IOException { 13 | new CSVTablePrinter(new CSVFileReader(SAMPLE_CSV_FILE)).print(); 14 | new CSVTreePrinter(new CSVStringReader(SAMPLE_CSV_STRING)).print(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /TeaseApartInheritance/rename/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class Main { 4 | private static final String SAMPLE_CSV_STRING = 5 | "좋은 아침입니다.,Good morning.\n" 6 | + "안녕하세요~,Hello.\n" 7 | + "안녕하세요.,Good evening.\n" 8 | + "안녕히 주무세요.,Good night.\n"; 9 | 10 | private static final String SAMPLE_CSV_FILE = "file.csv"; 11 | 12 | public static void main(String[] args) throws IOException { 13 | new CSVTablePrinter(new CSVStringReader(SAMPLE_CSV_STRING)).print(); 14 | new CSVTreePrinter(new CSVFileReader(SAMPLE_CSV_FILE)).print(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/ExSmell/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Something something = new Something(); 4 | if (!something.method1()) { 5 | System.out.println(something.getErrorMessage()); 6 | } else { 7 | if (!something.method2()) { 8 | System.out.println(something.getErrorMessage()); 9 | } else { 10 | if (!something.method3()) { 11 | System.out.println(something.getErrorMessage()); 12 | } 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ReplaceInheritanceWithDelegation/after/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Dice dice0 = new Dice(); 4 | Dice dice1 = new Dice(456L); 5 | Dice dice2 = new Dice(); 6 | Dice[] dices = { 7 | dice0, 8 | dice1, 9 | dice2, 10 | }; 11 | 12 | dice2.setSeed(456L); 13 | 14 | for (Dice d : dices) { 15 | for (int i = 0; i < 10; i++) { 16 | System.out.printf("%d, ", d.nextInt()); 17 | } 18 | System.out.println(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ReplaceInheritanceWithDelegation/step1/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Dice dice0 = new Dice(); 4 | Dice dice1 = new Dice(456L); 5 | Dice dice2 = new Dice(); 6 | Dice[] dices = { 7 | dice0, 8 | dice1, 9 | dice2, 10 | }; 11 | 12 | dice2.setSeed(456L); 13 | 14 | for (Dice d : dices) { 15 | for (int i = 0; i < 10; i++) { 16 | System.out.printf("%d, ", d.nextInt()); 17 | } 18 | System.out.println(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ReplaceInheritanceWithDelegation/step2/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Dice dice0 = new Dice(); 4 | Dice dice1 = new Dice(456L); 5 | Dice dice2 = new Dice(); 6 | Dice[] dices = { 7 | dice0, 8 | dice1, 9 | dice2, 10 | }; 11 | 12 | dice2.setSeed(456L); 13 | 14 | for (Dice d : dices) { 15 | for (int i = 0; i < 10; i++) { 16 | System.out.printf("%d, ", d.nextInt()); 17 | } 18 | System.out.println(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ReplaceInheritanceWithDelegation/this/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Dice dice0 = new Dice(); 4 | Dice dice1 = new Dice(456L); 5 | Dice dice2 = new Dice(); 6 | Dice[] dices = { 7 | dice0, 8 | dice1, 9 | dice2, 10 | }; 11 | 12 | dice2.setSeed(456L); 13 | 14 | for (Dice d : dices) { 15 | for (int i = 0; i < 10; i++) { 16 | System.out.printf("%d, ", d.nextInt()); 17 | } 18 | System.out.println(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/step1/ShapeRectangle.java: -------------------------------------------------------------------------------- 1 | public class ShapeRectangle extends Shape { 2 | public ShapeRectangle(int typecode, int startx, int starty, int endx, int endy) { 3 | super(typecode, startx, starty, endx, endy); 4 | } 5 | 6 | @Override public int getTypecode() { return Shape.TYPECODE_RECTANGLE; } 7 | 8 | @Override public String getName() { return "RECTANGLE"; } 9 | 10 | @Override public void draw() { drawRectangle(); } 11 | 12 | private void drawRectangle() { 13 | System.out.println("drawRectangle: " + this.toString()); 14 | // ... 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ReplaceInheritanceWithDelegation/before/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Dice dice0 = new Dice(); 4 | Dice dice1 = new Dice(456L); 5 | Dice dice2 = new Dice(); 6 | Dice[] dices = { 7 | dice0, 8 | dice1, 9 | dice2, 10 | }; 11 | 12 | dice2.setSeed(456L); 13 | 14 | for (Dice d : dices) { 15 | for (int i = 0; i < 10; i++) { 16 | System.out.printf("%d, ", d.nextInt()); 17 | } 18 | System.out.println(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/before/Robot.java: -------------------------------------------------------------------------------- 1 | public class Robot { 2 | private final String _name; 3 | public Robot(String name) { 4 | _name = name; 5 | } 6 | public void order(int command) { 7 | if (command == 0) { 8 | System.out.println(_name + " walks."); 9 | } else if (command == 1) { 10 | System.out.println(_name + " stops."); 11 | } else if (command == 2) { 12 | System.out.println(_name + " jumps."); 13 | } else { 14 | System.out.println("Command error. command = " + command); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ExtractClass/immutable/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | 5 | public class MainTest { 6 | @Test public void testMain() { 7 | ImmutableAuthor immutable = new Author("Mr.Mutable", "mutable@example.com"); 8 | String actual = immutable.getName() + ":" + immutable.getMail(); 9 | String expected = "Mr.Mutable:mutable@example.com"; 10 | assertEquals(expected, actual); 11 | } 12 | 13 | public static junit.framework.Test suite() { 14 | return new JUnit4TestAdapter(MainTest.class); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ExtractClass/mutable/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | 5 | public class MainTest { 6 | @Test public void testMain() { 7 | ImmutableAuthor immutable = new Author("Mr.Mutable", "mutable@example.com"); 8 | String actual = immutable.getName() + ":" + immutable.getMail(); 9 | String expected = "Mr.Mutable:mutable@example.com"; 10 | assertEquals(expected, actual); 11 | } 12 | 13 | public static junit.framework.Test suite() { 14 | return new JUnit4TestAdapter(MainTest.class); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /HideDelegate/again/AddressFile.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class AddressFile { 5 | private final Database _database; 6 | public AddressFile(String filename) { 7 | _database = new Database(filename); 8 | } 9 | public Enumeration names() { 10 | return _database.keys(); 11 | } 12 | public void set(String key, String value) { 13 | _database.set(key, value); 14 | } 15 | public String get(String key) { 16 | return _database.get(key); 17 | } 18 | public void update() throws IOException { 19 | _database.update(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /HideDelegate/after/AddressFile.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class AddressFile { 5 | private final Database _database; 6 | public AddressFile(String filename) { 7 | _database = new Database(filename); 8 | } 9 | public Enumeration names() { 10 | return _database.getProperties().propertyNames(); 11 | } 12 | public void set(String key, String value) { 13 | _database.set(key, value); 14 | } 15 | public String get(String key) { 16 | return _database.get(key); 17 | } 18 | public void update() throws IOException { 19 | _database.update(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/enum/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Item book = new Item( 4 | ItemType.BOOK, 5 | "세계 역사", 6 | 4800); 7 | 8 | Item dvd = new Item( 9 | ItemType.DVD, 10 | "뉴욕의 꿈 특별판", 11 | 3000); 12 | 13 | Item soft = new Item( 14 | ItemType.SOFTWARE, 15 | "튜링 머신 에뮬레이터", 16 | 3200); 17 | 18 | System.out.println("book = " + book.toString()); 19 | System.out.println("dvd = " + dvd.toString()); 20 | System.out.println("soft = " + soft.toString()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/step2/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Item book = new Item( 4 | ItemType.BOOK, 5 | "세계 역사", 6 | 4800); 7 | 8 | Item dvd = new Item( 9 | ItemType.DVD, 10 | "뉴욕의 꿈 특별판", 11 | 3000); 12 | 13 | Item soft = new Item( 14 | ItemType.SOFTWARE, 15 | "튜링 머신 에뮬레이터", 16 | 3200); 17 | 18 | System.out.println("book = " + book.toString()); 19 | System.out.println("dvd = " + dvd.toString()); 20 | System.out.println("soft = " + soft.toString()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/after/Item.java: -------------------------------------------------------------------------------- 1 | public class Item { 2 | private final ItemType _itemtype; 3 | private final String _title; 4 | private final int _price; 5 | 6 | public Item(ItemType itemtype, String title, int price) { 7 | _itemtype = itemtype; 8 | _title = title; 9 | _price = price; 10 | } 11 | 12 | public String getTitle() { return _title; } 13 | public int getPrice() { return _price; } 14 | 15 | public String toString() { 16 | return "[ " 17 | + _itemtype.getTypecode() + ", " 18 | + getTitle() + ", " 19 | + getPrice() + " ]"; 20 | } 21 | // ... 22 | } 23 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/enum/Item.java: -------------------------------------------------------------------------------- 1 | public class Item { 2 | private final ItemType _itemtype; 3 | private final String _title; 4 | private final int _price; 5 | 6 | public Item(ItemType itemtype, String title, int price) { 7 | _itemtype = itemtype; 8 | _title = title; 9 | _price = price; 10 | } 11 | 12 | public String getTitle() { return _title; } 13 | public int getPrice() { return _price; } 14 | 15 | public String toString() { 16 | return "[ " 17 | + _itemtype.getTypecode() + ", " 18 | + getTitle() + ", " 19 | + getPrice() + " ]"; 20 | } 21 | // ... 22 | } 23 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/step1/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Item book = new Item( 4 | Item.TYPECODE_BOOK, 5 | "세계 역사", 6 | 4800); 7 | 8 | Item dvd = new Item( 9 | Item.TYPECODE_DVD, 10 | "뉴욕의 꿈 특별판", 11 | 3000); 12 | 13 | Item soft = new Item( 14 | Item.TYPECODE_SOFTWARE, 15 | "튜링 머신 에뮬레이터", 16 | 3200); 17 | 18 | System.out.println("book = " + book.toString()); 19 | System.out.println("dvd = " + dvd.toString()); 20 | System.out.println("soft = " + soft.toString()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/step2/Item.java: -------------------------------------------------------------------------------- 1 | public class Item { 2 | private final ItemType _itemtype; 3 | private final String _title; 4 | private final int _price; 5 | 6 | public Item(ItemType itemtype, String title, int price) { 7 | _itemtype = itemtype; 8 | _title = title; 9 | _price = price; 10 | } 11 | 12 | public String getTitle() { return _title; } 13 | public int getPrice() { return _price; } 14 | 15 | public String toString() { 16 | return "[ " 17 | + _itemtype.getTypecode() + ", " 18 | + getTitle() + ", " 19 | + getPrice() + " ]"; 20 | } 21 | // ... 22 | } 23 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/after/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Item book = new Item( 4 | ItemType.BOOK, 5 | "세계 역사", 6 | 4800); 7 | 8 | Item dvd = new Item( 9 | ItemType.DVD, 10 | "뉴욕의 꿈 특별판", 11 | 3000); 12 | 13 | Item soft = new Item( 14 | ItemType.SOFTWARE, 15 | "튜링 머신 에뮬레이터", 16 | 3200); 17 | 18 | System.out.println("book = " + book.toString()); 19 | System.out.println("dvd = " + dvd.toString()); 20 | System.out.println("soft = " + soft.toString()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/before/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Item book = new Item( 4 | Item.TYPECODE_BOOK, 5 | "세계 역사", 6 | 4800); 7 | 8 | Item dvd = new Item( 9 | Item.TYPECODE_DVD, 10 | "뉴욕의 꿈 특별판", 11 | 3000); 12 | 13 | Item soft = new Item( 14 | Item.TYPECODE_SOFTWARE, 15 | "튜링 머신 에뮬레이터", 16 | 3200); 17 | 18 | System.out.println("book = " + book.toString()); 19 | System.out.println("dvd = " + dvd.toString()); 20 | System.out.println("soft = " + soft.toString()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/ExSmell/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "[ LunchSet: A, JUICE ]", 11 | "[ LunchSet: B, COFFEE ]"); 12 | String actual = getActualOutput(); 13 | assertEquals(expected, actual); 14 | } 15 | 16 | public static junit.framework.Test suite() { 17 | return new JUnit4TestAdapter(MainTest.class); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/step1/ItemType.java: -------------------------------------------------------------------------------- 1 | public class ItemType { 2 | public static final ItemType BOOK = new ItemType(0); 3 | public static final ItemType DVD = new ItemType(1); 4 | public static final ItemType SOFTWARE = new ItemType(2); 5 | 6 | private final int _typecode; 7 | 8 | private ItemType(int typecode) { 9 | _typecode = typecode; 10 | } 11 | 12 | public int getTypecode() { return _typecode; } 13 | 14 | public static ItemType getItemType(int typecode) { 15 | switch (typecode) { 16 | case 0: return BOOK; 17 | case 1: return DVD; 18 | case 2: return SOFTWARE; 19 | default: return null; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /RemoveControlFlag/SimpleDatabaseAfter1/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | try { 7 | SimpleDatabase db = new SimpleDatabase(new FileReader("dbfile.txt")); 8 | Iterator it = db.iterator(); 9 | while (it.hasNext()) { 10 | String key = it.next(); 11 | System.out.println("KEY: \"" + key + "\""); 12 | System.out.println("VALUE: \"" + db.getValue(key) + "\""); 13 | System.out.println(); 14 | } 15 | } catch (IOException e) { 16 | e.printStackTrace(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /RemoveControlFlag/SimpleDatabaseAfter2/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | try { 7 | SimpleDatabase db = new SimpleDatabase(new FileReader("dbfile.txt")); 8 | Iterator it = db.iterator(); 9 | while (it.hasNext()) { 10 | String key = it.next(); 11 | System.out.println("KEY: \"" + key + "\""); 12 | System.out.println("VALUE: \"" + db.getValue(key) + "\""); 13 | System.out.println(); 14 | } 15 | } catch (IOException e) { 16 | e.printStackTrace(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /RemoveControlFlag/SimpleDatabaseBefore/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | try { 7 | SimpleDatabase db = new SimpleDatabase(new FileReader("dbfile.txt")); 8 | Iterator it = db.iterator(); 9 | while (it.hasNext()) { 10 | String key = it.next(); 11 | System.out.println("KEY: \"" + key + "\""); 12 | System.out.println("VALUE: \"" + db.getValue(key) + "\""); 13 | System.out.println(); 14 | } 15 | } catch (IOException e) { 16 | e.printStackTrace(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/AnsSmellEnum/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "[ LunchSet: A, JUICE ]", 11 | "[ LunchSet: B, COFFEE ]"); 12 | String actual = getActualOutput(); 13 | assertEquals(expected, actual); 14 | } 15 | 16 | public static junit.framework.Test suite() { 17 | return new JUnit4TestAdapter(MainTest.class); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/AnsSmellClass/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "[ LunchSet: A, JUICE ]", 11 | "[ LunchSet: B, COFFEE ]"); 12 | String actual = getActualOutput(); 13 | assertEquals(expected, actual); 14 | } 15 | 16 | public static junit.framework.Test suite() { 17 | return new JUnit4TestAdapter(MainTest.class); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /TeaseApartInheritance/after/file.csv: -------------------------------------------------------------------------------- 1 | java,applet,Applet 2 | java,applet,AppletContext 3 | java,awt,ActiveEvent 4 | java,awt,Button 5 | java,awt,Canvas 6 | java,awt,color,ColorSpace 7 | java,awt,datatransfer,Clipboard 8 | java,awt,event,ActionEvent 9 | java,awt,event,ActionListener 10 | java,io,BufferedInputStream 11 | java,io,BufferedOutputStream 12 | java,lang,Object 13 | java,lang,String 14 | java,lang,StringBuffer 15 | java,util,ArrayList 16 | java,util,EventListener 17 | java,util,Map 18 | java,util,jar,JarInputStream 19 | java,util,jar,JarOutputStream 20 | java,util,logging,Formatter 21 | java,util,logging,Logger 22 | java,util,regex,Pattern 23 | java,util,zip,CheckedInputStream 24 | java,util,zip,CheckedOutputStream 25 | -------------------------------------------------------------------------------- /TeaseApartInheritance/before/file.csv: -------------------------------------------------------------------------------- 1 | java,applet,Applet 2 | java,applet,AppletContext 3 | java,awt,ActiveEvent 4 | java,awt,Button 5 | java,awt,Canvas 6 | java,awt,color,ColorSpace 7 | java,awt,datatransfer,Clipboard 8 | java,awt,event,ActionEvent 9 | java,awt,event,ActionListener 10 | java,io,BufferedInputStream 11 | java,io,BufferedOutputStream 12 | java,lang,Object 13 | java,lang,String 14 | java,lang,StringBuffer 15 | java,util,ArrayList 16 | java,util,EventListener 17 | java,util,Map 18 | java,util,jar,JarInputStream 19 | java,util,jar,JarOutputStream 20 | java,util,logging,Formatter 21 | java,util,logging,Logger 22 | java,util,regex,Pattern 23 | java,util,zip,CheckedInputStream 24 | java,util,zip,CheckedOutputStream 25 | -------------------------------------------------------------------------------- /TeaseApartInheritance/change/file.csv: -------------------------------------------------------------------------------- 1 | java,applet,Applet 2 | java,applet,AppletContext 3 | java,awt,ActiveEvent 4 | java,awt,Button 5 | java,awt,Canvas 6 | java,awt,color,ColorSpace 7 | java,awt,datatransfer,Clipboard 8 | java,awt,event,ActionEvent 9 | java,awt,event,ActionListener 10 | java,io,BufferedInputStream 11 | java,io,BufferedOutputStream 12 | java,lang,Object 13 | java,lang,String 14 | java,lang,StringBuffer 15 | java,util,ArrayList 16 | java,util,EventListener 17 | java,util,Map 18 | java,util,jar,JarInputStream 19 | java,util,jar,JarOutputStream 20 | java,util,logging,Formatter 21 | java,util,logging,Logger 22 | java,util,regex,Pattern 23 | java,util,zip,CheckedInputStream 24 | java,util,zip,CheckedOutputStream 25 | -------------------------------------------------------------------------------- /TeaseApartInheritance/rename/file.csv: -------------------------------------------------------------------------------- 1 | java,applet,Applet 2 | java,applet,AppletContext 3 | java,awt,ActiveEvent 4 | java,awt,Button 5 | java,awt,Canvas 6 | java,awt,color,ColorSpace 7 | java,awt,datatransfer,Clipboard 8 | java,awt,event,ActionEvent 9 | java,awt,event,ActionListener 10 | java,io,BufferedInputStream 11 | java,io,BufferedOutputStream 12 | java,lang,Object 13 | java,lang,String 14 | java,lang,StringBuffer 15 | java,util,ArrayList 16 | java,util,EventListener 17 | java,util,Map 18 | java,util,jar,JarInputStream 19 | java,util,jar,JarOutputStream 20 | java,util,logging,Formatter 21 | java,util,logging,Logger 22 | java,util,regex,Pattern 23 | java,util,zip,CheckedInputStream 24 | java,util,zip,CheckedOutputStream 25 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step2/file.csv: -------------------------------------------------------------------------------- 1 | java,applet,Applet 2 | java,applet,AppletContext 3 | java,awt,ActiveEvent 4 | java,awt,Button 5 | java,awt,Canvas 6 | java,awt,color,ColorSpace 7 | java,awt,datatransfer,Clipboard 8 | java,awt,event,ActionEvent 9 | java,awt,event,ActionListener 10 | java,io,BufferedInputStream 11 | java,io,BufferedOutputStream 12 | java,lang,Object 13 | java,lang,String 14 | java,lang,StringBuffer 15 | java,util,ArrayList 16 | java,util,EventListener 17 | java,util,Map 18 | java,util,jar,JarInputStream 19 | java,util,jar,JarOutputStream 20 | java,util,logging,Formatter 21 | java,util,logging,Logger 22 | java,util,regex,Pattern 23 | java,util,zip,CheckedInputStream 24 | java,util,zip,CheckedOutputStream 25 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step3/file.csv: -------------------------------------------------------------------------------- 1 | java,applet,Applet 2 | java,applet,AppletContext 3 | java,awt,ActiveEvent 4 | java,awt,Button 5 | java,awt,Canvas 6 | java,awt,color,ColorSpace 7 | java,awt,datatransfer,Clipboard 8 | java,awt,event,ActionEvent 9 | java,awt,event,ActionListener 10 | java,io,BufferedInputStream 11 | java,io,BufferedOutputStream 12 | java,lang,Object 13 | java,lang,String 14 | java,lang,StringBuffer 15 | java,util,ArrayList 16 | java,util,EventListener 17 | java,util,Map 18 | java,util,jar,JarInputStream 19 | java,util,jar,JarOutputStream 20 | java,util,logging,Formatter 21 | java,util,logging,Logger 22 | java,util,regex,Pattern 23 | java,util,zip,CheckedInputStream 24 | java,util,zip,CheckedOutputStream 25 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step4/file.csv: -------------------------------------------------------------------------------- 1 | java,applet,Applet 2 | java,applet,AppletContext 3 | java,awt,ActiveEvent 4 | java,awt,Button 5 | java,awt,Canvas 6 | java,awt,color,ColorSpace 7 | java,awt,datatransfer,Clipboard 8 | java,awt,event,ActionEvent 9 | java,awt,event,ActionListener 10 | java,io,BufferedInputStream 11 | java,io,BufferedOutputStream 12 | java,lang,Object 13 | java,lang,String 14 | java,lang,StringBuffer 15 | java,util,ArrayList 16 | java,util,EventListener 17 | java,util,Map 18 | java,util,jar,JarInputStream 19 | java,util,jar,JarOutputStream 20 | java,util,logging,Formatter 21 | java,util,logging,Logger 22 | java,util,regex,Pattern 23 | java,util,zip,CheckedInputStream 24 | java,util,zip,CheckedOutputStream 25 | -------------------------------------------------------------------------------- /IntroduceNullObject/step3/Person.java: -------------------------------------------------------------------------------- 1 | public class Person { 2 | private final Label _name; 3 | private final Label _mail; 4 | public Person(Label name, Label mail) { 5 | _name = name; 6 | _mail = mail; 7 | } 8 | public Person(Label name) { 9 | this(name, new NullLabel()); 10 | } 11 | public void display() { 12 | _name.display(); 13 | _mail.display(); 14 | } 15 | public String toString() { 16 | String result = "[ Person:"; 17 | 18 | result += " name="; 19 | result += _name; 20 | 21 | result += " mail="; 22 | result += _mail; 23 | 24 | result += " ]"; 25 | 26 | return result; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /TeaseApartInheritance/before/CSVStringReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVStringReader extends CSVReader { 4 | private final BufferedReader _bufReader; 5 | 6 | public CSVStringReader(String string) { 7 | _bufReader = new BufferedReader(new StringReader(string)); 8 | } 9 | 10 | @Override public String[] readCSV() throws IOException { 11 | String line = _bufReader.readLine(); 12 | if (line == null) { 13 | return null; 14 | } else { 15 | String[] item = CSV_PATTERN.split(line); 16 | return item; 17 | } 18 | } 19 | 20 | @Override public void close() throws IOException { 21 | _bufReader.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /TeaseApartInheritance/rename/CSVStringReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVStringReader extends CSVReader { 4 | private final BufferedReader _bufReader; 5 | 6 | public CSVStringReader(String string) { 7 | _bufReader = new BufferedReader(new StringReader(string)); 8 | } 9 | 10 | @Override public String[] readCSV() throws IOException { 11 | String line = _bufReader.readLine(); 12 | if (line == null) { 13 | return null; 14 | } else { 15 | String[] item = CSV_PATTERN.split(line); 16 | return item; 17 | } 18 | } 19 | 20 | @Override public void close() throws IOException { 21 | _bufReader.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step2/CSVStringReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVStringReader extends CSVReader { 4 | private final BufferedReader _bufReader; 5 | 6 | public CSVStringReader(String string) { 7 | _bufReader = new BufferedReader(new StringReader(string)); 8 | } 9 | 10 | @Override public String[] readCSV() throws IOException { 11 | String line = _bufReader.readLine(); 12 | if (line == null) { 13 | return null; 14 | } else { 15 | String[] item = CSV_PATTERN.split(line); 16 | return item; 17 | } 18 | } 19 | 20 | @Override public void close() throws IOException { 21 | _bufReader.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step3/CSVStringReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVStringReader extends CSVReader { 4 | private final BufferedReader _bufReader; 5 | 6 | public CSVStringReader(String string) { 7 | _bufReader = new BufferedReader(new StringReader(string)); 8 | } 9 | 10 | @Override public String[] readCSV() throws IOException { 11 | String line = _bufReader.readLine(); 12 | if (line == null) { 13 | return null; 14 | } else { 15 | String[] item = CSV_PATTERN.split(line); 16 | return item; 17 | } 18 | } 19 | 20 | @Override public void close() throws IOException { 21 | _bufReader.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ExtractMethod/after/Banner.java: -------------------------------------------------------------------------------- 1 | public class Banner { 2 | private final String _content; 3 | public Banner(String content) { 4 | _content = content; 5 | } 6 | public void print(int times) { 7 | printBorder(); 8 | printContent(times); 9 | printBorder(); 10 | } 11 | 12 | private void printBorder() { 13 | System.out.print("+"); 14 | for (int i = 0; i < _content.length(); i++) { 15 | System.out.print("-"); 16 | } 17 | System.out.println("+"); 18 | } 19 | 20 | private void printContent(int times) { 21 | for (int i = 0; i < times; i++) { 22 | System.out.println("|" + _content + "|"); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ExtractMethod/step2/Banner.java: -------------------------------------------------------------------------------- 1 | public class Banner { 2 | private final String _content; 3 | public Banner(String content) { 4 | _content = content; 5 | } 6 | public void print(int times) { 7 | printBorder(); 8 | printContent(times); 9 | printBorder(); 10 | } 11 | 12 | private void printBorder() { 13 | System.out.print("+"); 14 | for (int i = 0; i < _content.length(); i++) { 15 | System.out.print("-"); 16 | } 17 | System.out.println("+"); 18 | } 19 | 20 | private void printContent(int times) { 21 | for (int i = 0; i < times; i++) { 22 | System.out.println("|" + _content + "|"); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ExtractClass/after/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Book refactoring = new Book( 4 | "Refactoring: improving the design of existing code", 5 | "ISBN0201485672", 6 | "$44.95", 7 | "Martin Fowler", 8 | "fowler@acm.org"); 9 | 10 | Book math = new Book( 11 | "프로그래머 수학", 12 | "ISBN4797329734", 13 | "20000원", 14 | "유키 히로시", 15 | "hyuki@hyuki.com"); 16 | 17 | System.out.println("refactoring:"); 18 | System.out.println(refactoring.toXml()); 19 | 20 | System.out.println("math:"); 21 | System.out.println(math.toXml()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ExtractClass/before/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Book refactoring = new Book( 4 | "Refactoring: improving the design of existing code", 5 | "ISBN0201485672", 6 | "$44.95", 7 | "Martin Fowler", 8 | "fowler@acm.org"); 9 | 10 | Book math = new Book( 11 | "프로그래머 수학", 12 | "ISBN4797329734", 13 | "20000원", 14 | "유키 히로시", 15 | "hyuki@hyuki.com"); 16 | 17 | System.out.println("refactoring:"); 18 | System.out.println(refactoring.toXml()); 19 | 20 | System.out.println("math:"); 21 | System.out.println(math.toXml()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ExtractClass/step12/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Book refactoring = new Book( 4 | "Refactoring: improving the design of existing code", 5 | "ISBN0201485672", 6 | "$44.95", 7 | "Martin Fowler", 8 | "fowler@acm.org"); 9 | 10 | Book math = new Book( 11 | "프로그래머 수학", 12 | "ISBN4797329734", 13 | "20000원", 14 | "유키 히로시", 15 | "hyuki@hyuki.com"); 16 | 17 | System.out.println("refactoring:"); 18 | System.out.println(refactoring.toXml()); 19 | 20 | System.out.println("math:"); 21 | System.out.println(math.toXml()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /HideDelegate/step1/AddressFile.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class AddressFile { 5 | private final Database _database; 6 | public AddressFile(String filename) { 7 | _database = new Database(filename); 8 | } 9 | public Database getDatabase() { 10 | return _database; 11 | } 12 | public Enumeration names() { 13 | return _database.getProperties().propertyNames(); 14 | } 15 | public void set(String key, String value) { 16 | _database.set(key, value); 17 | } 18 | public String get(String key) { 19 | return _database.get(key); 20 | } 21 | public void update() throws IOException { 22 | _database.update(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /HideDelegate/step2/AddressFile.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class AddressFile { 5 | private final Database _database; 6 | public AddressFile(String filename) { 7 | _database = new Database(filename); 8 | } 9 | // public Database getDatabase() { 10 | // return _database; 11 | // } 12 | public Enumeration names() { 13 | return _database.getProperties().propertyNames(); 14 | } 15 | public void set(String key, String value) { 16 | _database.set(key, value); 17 | } 18 | public String get(String key) { 19 | return _database.get(key); 20 | } 21 | public void update() throws IOException { 22 | _database.update(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/AnsSmell/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | // Expected 9 | String expected = getExpectedOutput("method3: file open error."); 10 | 11 | // Actual 12 | Main.main(new String[0]); 13 | String actual = getActualOutput(); 14 | 15 | // Assert 16 | assertEquals(expected, actual); 17 | } 18 | 19 | public static junit.framework.Test suite() { 20 | return new JUnit4TestAdapter(MainTest.class); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ReplaceErrorCodeWithException/ExSmell/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | // Expected 9 | String expected = getExpectedOutput("method3: file open error."); 10 | 11 | // Actual 12 | Main.main(new String[0]); 13 | String actual = getActualOutput(); 14 | 15 | // Assert 16 | assertEquals(expected, actual); 17 | } 18 | 19 | public static junit.framework.Test suite() { 20 | return new JUnit4TestAdapter(MainTest.class); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/enum/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "book = [ 0, 세계 역사, 4800 ]", 11 | "dvd = [ 1, 뉴욕의 꿈 특별판, 3000 ]", 12 | "soft = [ 2, 튜링 머신 에뮬레이터, 3200 ]"); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/step1/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "book = [ 0, 세계 역사, 4800 ]", 11 | "dvd = [ 1, 뉴욕의 꿈 특별판, 3000 ]", 12 | "soft = [ 2, 튜링 머신 에뮬레이터, 3200 ]"); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/step2/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "book = [ 0, 세계 역사, 4800 ]", 11 | "dvd = [ 1, 뉴욕의 꿈 특별판, 3000 ]", 12 | "soft = [ 2, 튜링 머신 에뮬레이터, 3200 ]"); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /TeaseApartInheritance/before/CSVFileReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVFileReader extends CSVReader { 4 | private final BufferedReader _bufReader; 5 | 6 | public CSVFileReader(String filename) throws IOException { 7 | _bufReader = new BufferedReader(new FileReader(filename)); 8 | } 9 | 10 | @Override public String[] readCSV() throws IOException { 11 | String line = _bufReader.readLine(); 12 | if (line == null) { 13 | return null; 14 | } else { 15 | String[] item = CSV_PATTERN.split(line); 16 | return item; 17 | } 18 | } 19 | 20 | @Override public void close() throws IOException { 21 | _bufReader.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /TeaseApartInheritance/rename/CSVFileReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVFileReader extends CSVReader { 4 | private final BufferedReader _bufReader; 5 | 6 | public CSVFileReader(String filename) throws IOException { 7 | _bufReader = new BufferedReader(new FileReader(filename)); 8 | } 9 | 10 | @Override public String[] readCSV() throws IOException { 11 | String line = _bufReader.readLine(); 12 | if (line == null) { 13 | return null; 14 | } else { 15 | String[] item = CSV_PATTERN.split(line); 16 | return item; 17 | } 18 | } 19 | 20 | @Override public void close() throws IOException { 21 | _bufReader.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step2/CSVFileReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVFileReader extends CSVReader { 4 | private final BufferedReader _bufReader; 5 | 6 | public CSVFileReader(String filename) throws IOException { 7 | _bufReader = new BufferedReader(new FileReader(filename)); 8 | } 9 | 10 | @Override public String[] readCSV() throws IOException { 11 | String line = _bufReader.readLine(); 12 | if (line == null) { 13 | return null; 14 | } else { 15 | String[] item = CSV_PATTERN.split(line); 16 | return item; 17 | } 18 | } 19 | 20 | @Override public void close() throws IOException { 21 | _bufReader.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step3/CSVFileReader.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVFileReader extends CSVReader { 4 | private final BufferedReader _bufReader; 5 | 6 | public CSVFileReader(String filename) throws IOException { 7 | _bufReader = new BufferedReader(new FileReader(filename)); 8 | } 9 | 10 | @Override public String[] readCSV() throws IOException { 11 | String line = _bufReader.readLine(); 12 | if (line == null) { 13 | return null; 14 | } else { 15 | String[] item = CSV_PATTERN.split(line); 16 | return item; 17 | } 18 | } 19 | 20 | @Override public void close() throws IOException { 21 | _bufReader.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /TeaseApartInheritance/after/CSVReader.java: -------------------------------------------------------------------------------- 1 | import java.util.regex.*; 2 | import java.io.*; 3 | 4 | public class CSVReader { 5 | protected static final Pattern CSV_PATTERN = Pattern.compile("\\s*,\\s*"); 6 | protected final BufferedReader _bufReader; 7 | protected CSVReader(BufferedReader bufReader) { 8 | _bufReader = bufReader; 9 | } 10 | public String[] readCSV() throws IOException { 11 | String line = _bufReader.readLine(); 12 | if (line == null) { 13 | return null; 14 | } else { 15 | String[] item = CSV_PATTERN.split(line); 16 | return item; 17 | } 18 | } 19 | public void close() throws IOException { 20 | _bufReader.close(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /TeaseApartInheritance/change/CSVReader.java: -------------------------------------------------------------------------------- 1 | import java.util.regex.*; 2 | import java.io.*; 3 | 4 | public class CSVReader { 5 | protected static final Pattern CSV_PATTERN = Pattern.compile("\\s*,\\s*"); 6 | protected final BufferedReader _bufReader; 7 | protected CSVReader(BufferedReader bufReader) { 8 | _bufReader = bufReader; 9 | } 10 | public String[] readCSV() throws IOException { 11 | String line = _bufReader.readLine(); 12 | if (line == null) { 13 | return null; 14 | } else { 15 | String[] item = CSV_PATTERN.split(line); 16 | return item; 17 | } 18 | } 19 | public void close() throws IOException { 20 | _bufReader.close(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step4/CSVReader.java: -------------------------------------------------------------------------------- 1 | import java.util.regex.*; 2 | import java.io.*; 3 | 4 | public class CSVReader { 5 | protected static final Pattern CSV_PATTERN = Pattern.compile("\\s*,\\s*"); 6 | protected final BufferedReader _bufReader; 7 | protected CSVReader(BufferedReader bufReader) { 8 | _bufReader = bufReader; 9 | } 10 | public String[] readCSV() throws IOException { 11 | String line = _bufReader.readLine(); 12 | if (line == null) { 13 | return null; 14 | } else { 15 | String[] item = CSV_PATTERN.split(line); 16 | return item; 17 | } 18 | } 19 | public void close() throws IOException { 20 | _bufReader.close(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/enum/Robot.java: -------------------------------------------------------------------------------- 1 | public class Robot { 2 | private final String _name; 3 | public enum Command { 4 | WALK, 5 | STOP, 6 | JUMP, 7 | }; 8 | public Robot(String name) { 9 | _name = name; 10 | } 11 | public void order(Robot.Command command) { 12 | if (command == Command.WALK) { 13 | System.out.println(_name + " walks."); 14 | } else if (command == Command.STOP) { 15 | System.out.println(_name + " stops."); 16 | } else if (command == Command.JUMP) { 17 | System.out.println(_name + " jumps."); 18 | } else { 19 | System.out.println("Command error. command = " + command); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/ExSmell/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "Music: play", 12 | "Music: stop", 13 | "Video: play", 14 | "Video: stop"); 15 | String actual = getActualOutput(); 16 | assertEquals(expected, actual); 17 | } 18 | 19 | public static junit.framework.Test suite() { 20 | return new JUnit4TestAdapter(MainTest.class); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/after/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "book = [ 0, 세계 역사, 4800 ]", 11 | "dvd = [ 1, 뉴욕의 꿈 특별판, 3000 ]", 12 | "soft = [ 2, 튜링 머신 에뮬레이터, 3200 ]"); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/AnsSmell/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "Music: play", 12 | "Music: stop", 13 | "Video: play", 14 | "Video: stop"); 15 | String actual = getActualOutput(); 16 | assertEquals(expected, actual); 17 | } 18 | 19 | public static junit.framework.Test suite() { 20 | return new JUnit4TestAdapter(MainTest.class); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/before/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "book = [ 0, 세계 역사, 4800 ]", 11 | "dvd = [ 1, 뉴욕의 꿈 특별판, 3000 ]", 12 | "soft = [ 2, 튜링 머신 에뮬레이터, 3200 ]"); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceInheritanceWithDelegation/after/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "1, 5, 3, 4, 6, 3, 3, 4, 5, 3, ", 11 | "3, 5, 6, 6, 5, 4, 2, 1, 2, 4, ", 12 | "3, 5, 6, 6, 5, 4, 2, 1, 2, 4, "); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceInheritanceWithDelegation/before/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "1, 5, 3, 4, 6, 3, 3, 4, 5, 3, ", 11 | "3, 5, 6, 6, 5, 4, 2, 1, 2, 4, ", 12 | "3, 5, 6, 6, 5, 4, 2, 1, 2, 4, "); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceInheritanceWithDelegation/step1/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "1, 5, 3, 4, 6, 3, 3, 4, 5, 3, ", 11 | "3, 5, 6, 6, 5, 4, 2, 1, 2, 4, ", 12 | "3, 5, 6, 6, 5, 4, 2, 1, 2, 4, "); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceInheritanceWithDelegation/step2/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "1, 5, 3, 4, 6, 3, 3, 4, 5, 3, ", 11 | "3, 5, 6, 6, 5, 4, 2, 1, 2, 4, ", 12 | "3, 5, 6, 6, 5, 4, 2, 1, 2, 4, "); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceInheritanceWithDelegation/this/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "1, 5, 3, 4, 6, 3, 3, 4, 5, 3, ", 11 | "3, 5, 6, 6, 5, 4, 2, 1, 2, 4, ", 12 | "3, 5, 6, 6, 5, 4, 2, 1, 2, 4, "); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ExtractMethod/before/Banner.java: -------------------------------------------------------------------------------- 1 | public class Banner { 2 | private final String _content; 3 | public Banner(String content) { 4 | _content = content; 5 | } 6 | public void print(int times) { 7 | // 테두리 출력 8 | System.out.print("+"); 9 | for (int i = 0; i < _content.length(); i++) { 10 | System.out.print("-"); 11 | } 12 | System.out.println("+"); 13 | 14 | // 내용 출력 15 | for (int i = 0; i < times; i++) { 16 | System.out.println("|" + _content + "|"); 17 | } 18 | 19 | // 테두리 출력 20 | System.out.print("+"); 21 | for (int i = 0; i < _content.length(); i++) { 22 | System.out.print("-"); 23 | } 24 | System.out.println("+"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /HideDelegate/after/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "name=Hiroshi Yuki, mail=hyuki@example.com", 12 | "name=Tomura, mail=tomura@example.com", 13 | "name=Hanako Sato, mail=hanako@example.com"); 14 | String actual = getActualOutput(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | public static junit.framework.Test suite() { 19 | return new JUnit4TestAdapter(MainTest.class); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /HideDelegate/again/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "name=Hiroshi Yuki, mail=hyuki@example.com", 12 | "name=Tomura, mail=tomura@example.com", 13 | "name=Hanako Sato, mail=hanako@example.com"); 14 | String actual = getActualOutput(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | public static junit.framework.Test suite() { 19 | return new JUnit4TestAdapter(MainTest.class); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /HideDelegate/before/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "name=Hiroshi Yuki, mail=hyuki@example.com", 12 | "name=Tomura, mail=tomura@example.com", 13 | "name=Hanako Sato, mail=hanako@example.com"); 14 | String actual = getActualOutput(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | public static junit.framework.Test suite() { 19 | return new JUnit4TestAdapter(MainTest.class); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /HideDelegate/nested/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "name=Hiroshi Yuki, mail=hyuki@example.com", 12 | "name=Tomura, mail=tomura@example.com", 13 | "name=Hanako Sato, mail=hanako@example.com"); 14 | String actual = getActualOutput(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | public static junit.framework.Test suite() { 19 | return new JUnit4TestAdapter(MainTest.class); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /HideDelegate/step1/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "name=Hiroshi Yuki, mail=hyuki@example.com", 12 | "name=Tomura, mail=tomura@example.com", 13 | "name=Hanako Sato, mail=hanako@example.com"); 14 | String actual = getActualOutput(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | public static junit.framework.Test suite() { 19 | return new JUnit4TestAdapter(MainTest.class); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /HideDelegate/step2/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "name=Hiroshi Yuki, mail=hyuki@example.com", 12 | "name=Tomura, mail=tomura@example.com", 13 | "name=Hanako Sato, mail=hanako@example.com"); 14 | String actual = getActualOutput(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | public static junit.framework.Test suite() { 19 | return new JUnit4TestAdapter(MainTest.class); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/ExSmell/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "Null: play", 12 | "Video: play", 13 | "Video: stop", 14 | "Music: loop", 15 | "Music: stop"); 16 | String actual = getActualOutput(); 17 | assertEquals(expected, actual); 18 | } 19 | 20 | public static junit.framework.Test suite() { 21 | return new JUnit4TestAdapter(MainTest.class); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithStateStrategy/AnsSmell/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "Null: play", 12 | "Video: play", 13 | "Video: stop", 14 | "Music: loop", 15 | "Music: stop"); 16 | String actual = getActualOutput(); 17 | assertEquals(expected, actual); 18 | } 19 | 20 | public static junit.framework.Test suite() { 21 | return new JUnit4TestAdapter(MainTest.class); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/after/Robot.java: -------------------------------------------------------------------------------- 1 | public class Robot { 2 | public static final int COMMAND_WALK = 0; 3 | public static final int COMMAND_STOP = 1; 4 | public static final int COMMAND_JUMP = 2; 5 | private final String _name; 6 | public Robot(String name) { 7 | _name = name; 8 | } 9 | public void order(int command) { 10 | if (command == COMMAND_WALK) { 11 | System.out.println(_name + " walks."); 12 | } else if (command == COMMAND_STOP) { 13 | System.out.println(_name + " stops."); 14 | } else if (command == COMMAND_JUMP) { 15 | System.out.println(_name + " jumps."); 16 | } else { 17 | System.out.println("Command error. command = " + command); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/step1/Robot.java: -------------------------------------------------------------------------------- 1 | public class Robot { 2 | public static final int COMMAND_WALK = 0; 3 | public static final int COMMAND_STOP = 1; 4 | public static final int COMMAND_JUMP = 2; 5 | private final String _name; 6 | public Robot(String name) { 7 | _name = name; 8 | } 9 | public void order(int command) { 10 | if (command == COMMAND_WALK) { 11 | System.out.println(_name + " walks."); 12 | } else if (command == COMMAND_STOP) { 13 | System.out.println(_name + " stops."); 14 | } else if (command == COMMAND_JUMP) { 15 | System.out.println(_name + " jumps."); 16 | } else { 17 | System.out.println("Command error. command = " + command); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /IntroduceNullObject/field/Label.java: -------------------------------------------------------------------------------- 1 | public class Label { 2 | public final static Label NULL = new NullLabel(); 3 | private final String _label; 4 | public Label(String label) { 5 | _label = label; 6 | } 7 | public void display() { 8 | System.out.println("display: " + _label); 9 | } 10 | public String toString() { 11 | return "\"" + _label + "\""; 12 | } 13 | public boolean isNull() { 14 | return false; 15 | } 16 | 17 | // 널 객체 18 | private static class NullLabel extends Label { 19 | public NullLabel() { 20 | super("(none)"); 21 | } 22 | @Override public void display() { 23 | } 24 | @Override public boolean isNull() { 25 | return true; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/step2/Robot.java: -------------------------------------------------------------------------------- 1 | public class Robot { 2 | public static final int COMMAND_WALK = 100; 3 | public static final int COMMAND_STOP = 200; 4 | public static final int COMMAND_JUMP = 300; 5 | private final String _name; 6 | public Robot(String name) { 7 | _name = name; 8 | } 9 | public void order(int command) { 10 | if (command == COMMAND_WALK) { 11 | System.out.println(_name + " walks."); 12 | } else if (command == COMMAND_STOP) { 13 | System.out.println(_name + " stops."); 14 | } else if (command == COMMAND_JUMP) { 15 | System.out.println(_name + " jumps."); 16 | } else { 17 | System.out.println("Command error. command = " + command); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/after/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "drawLine: [ LINE, (0, 0)-(100, 200) ]", 11 | "drawRectangle: [ RECTANGLE, (10, 20)-(30, 40) ]", 12 | "drawOval: [ OVAL, (100, 200)-(300, 400) ]"); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/before/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "drawLine: [ LINE, (0, 0)-(100, 200) ]", 11 | "drawRectangle: [ RECTANGLE, (10, 20)-(30, 40) ]", 12 | "drawOval: [ OVAL, (100, 200)-(300, 400) ]"); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/step1/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "drawLine: [ LINE, (0, 0)-(100, 200) ]", 11 | "drawRectangle: [ RECTANGLE, (10, 20)-(30, 40) ]", 12 | "drawOval: [ OVAL, (100, 200)-(300, 400) ]"); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/step2/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "drawLine: [ LINE, (0, 0)-(100, 200) ]", 11 | "drawRectangle: [ RECTANGLE, (10, 20)-(30, 40) ]", 12 | "drawOval: [ OVAL, (100, 200)-(300, 400) ]"); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceConstructorWithFactoryMethod/subclass/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | String expected = getExpectedOutput( 10 | "drawLine: [ LINE, (0, 0)-(100, 200) ]", 11 | "drawRectangle: [ RECTANGLE, (10, 20)-(30, 40) ]", 12 | "drawOval: [ OVAL, (100, 200)-(300, 400) ]"); 13 | String actual = getActualOutput(); 14 | assertEquals(expected, actual); 15 | } 16 | 17 | public static junit.framework.Test suite() { 18 | return new JUnit4TestAdapter(MainTest.class); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/enum/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | // Expected 9 | String expected = getExpectedOutput( 10 | "Andrew walks.", 11 | "Andrew stops.", 12 | "Andrew jumps."); 13 | 14 | // Actual 15 | Main.main(new String[0]); 16 | String actual = getActualOutput(); 17 | 18 | // Assert 19 | assertEquals(expected, actual); 20 | } 21 | 22 | public static junit.framework.Test suite() { 23 | return new JUnit4TestAdapter(MainTest.class); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/after/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "drawLine: [ LINE, (0, 0)-(100, 200) ]", 12 | "drawRectangle: [ RECTANGLE, (10, 20)-(30, 40) ]", 13 | "drawOval: [ OVAL, (100, 200)-(300, 400) ]"); 14 | String actual = getActualOutput(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | public static junit.framework.Test suite() { 19 | return new JUnit4TestAdapter(MainTest.class); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/before/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "drawLine: [ LINE, (0, 0)-(100, 200) ]", 12 | "drawRectangle: [ RECTANGLE, (10, 20)-(30, 40) ]", 13 | "drawOval: [ OVAL, (100, 200)-(300, 400) ]"); 14 | String actual = getActualOutput(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | public static junit.framework.Test suite() { 19 | return new JUnit4TestAdapter(MainTest.class); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/factory/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "drawLine: [ LINE, (0, 0)-(100, 200) ]", 12 | "drawRectangle: [ RECTANGLE, (10, 20)-(30, 40) ]", 13 | "drawOval: [ OVAL, (100, 200)-(300, 400) ]"); 14 | String actual = getActualOutput(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | public static junit.framework.Test suite() { 19 | return new JUnit4TestAdapter(MainTest.class); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/step1/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "drawLine: [ LINE, (0, 0)-(100, 200) ]", 12 | "drawRectangle: [ RECTANGLE, (10, 20)-(30, 40) ]", 13 | "drawOval: [ OVAL, (100, 200)-(300, 400) ]"); 14 | String actual = getActualOutput(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | public static junit.framework.Test suite() { 19 | return new JUnit4TestAdapter(MainTest.class); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/step2/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "drawLine: [ LINE, (0, 0)-(100, 200) ]", 12 | "drawRectangle: [ RECTANGLE, (10, 20)-(30, 40) ]", 13 | "drawOval: [ OVAL, (100, 200)-(300, 400) ]"); 14 | String actual = getActualOutput(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | public static junit.framework.Test suite() { 19 | return new JUnit4TestAdapter(MainTest.class); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/after/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | // Expected 9 | String expected = getExpectedOutput( 10 | "Andrew walks.", 11 | "Andrew stops.", 12 | "Andrew jumps."); 13 | 14 | // Actual 15 | Main.main(new String[0]); 16 | String actual = getActualOutput(); 17 | 18 | // Assert 19 | assertEquals(expected, actual); 20 | } 21 | 22 | public static junit.framework.Test suite() { 23 | return new JUnit4TestAdapter(MainTest.class); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/before/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | // Expected 9 | String expected = getExpectedOutput( 10 | "Andrew walks.", 11 | "Andrew stops.", 12 | "Andrew jumps."); 13 | 14 | // Actual 15 | Main.main(new String[0]); 16 | String actual = getActualOutput(); 17 | 18 | // Assert 19 | assertEquals(expected, actual); 20 | } 21 | 22 | public static junit.framework.Test suite() { 23 | return new JUnit4TestAdapter(MainTest.class); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/step1/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | // Expected 9 | String expected = getExpectedOutput( 10 | "Andrew walks.", 11 | "Andrew stops.", 12 | "Andrew jumps."); 13 | 14 | // Actual 15 | Main.main(new String[0]); 16 | String actual = getActualOutput(); 17 | 18 | // Assert 19 | assertEquals(expected, actual); 20 | } 21 | 22 | public static junit.framework.Test suite() { 23 | return new JUnit4TestAdapter(MainTest.class); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/step2/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | // Expected 9 | String expected = getExpectedOutput( 10 | "Andrew walks.", 11 | "Andrew stops.", 12 | "Andrew jumps."); 13 | 14 | // Actual 15 | Main.main(new String[0]); 16 | String actual = getActualOutput(); 17 | 18 | // Assert 19 | assertEquals(expected, actual); 20 | } 21 | 22 | public static junit.framework.Test suite() { 23 | return new JUnit4TestAdapter(MainTest.class); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/typecheck/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | // Expected 9 | String expected = getExpectedOutput( 10 | "Andrew walks.", 11 | "Andrew stops.", 12 | "Andrew jumps."); 13 | 14 | // Actual 15 | Main.main(new String[0]); 16 | String actual = getActualOutput(); 17 | 18 | // Assert 19 | assertEquals(expected, actual); 20 | } 21 | 22 | public static junit.framework.Test suite() { 23 | return new JUnit4TestAdapter(MainTest.class); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/typecode/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | // Expected 9 | String expected = getExpectedOutput( 10 | "Andrew walks.", 11 | "Andrew stops.", 12 | "Andrew jumps."); 13 | 14 | // Actual 15 | Main.main(new String[0]); 16 | String actual = getActualOutput(); 17 | 18 | // Assert 19 | assertEquals(expected, actual); 20 | } 21 | 22 | public static junit.framework.Test suite() { 23 | return new JUnit4TestAdapter(MainTest.class); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/manyfactory/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | import com.hyuki.refbook.StandardOutputTest; 5 | 6 | public class MainTest extends StandardOutputTest { 7 | @Test public void testMain() { 8 | Main.main(new String[0]); 9 | 10 | String expected = getExpectedOutput( 11 | "drawLine: [ LINE, (0, 0)-(100, 200) ]", 12 | "drawRectangle: [ RECTANGLE, (10, 20)-(30, 40) ]", 13 | "drawOval: [ OVAL, (100, 200)-(300, 400) ]"); 14 | String actual = getActualOutput(); 15 | assertEquals(expected, actual); 16 | } 17 | 18 | public static junit.framework.Test suite() { 19 | return new JUnit4TestAdapter(MainTest.class); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /HideDelegate/after/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | try { 7 | AddressFile file = new AddressFile("address.txt"); 8 | file.set("Hiroshi Yuki", "hyuki@example.com"); 9 | file.set("Tomura", "tomura@example.com"); 10 | file.set("Hanako Sato", "hanako@example.com"); 11 | file.update(); 12 | 13 | Enumeration e = file.names(); 14 | while (e.hasMoreElements()) { 15 | String name = (String)e.nextElement(); 16 | String mail = file.get(name); 17 | System.out.println("name=" + name + ", mail=" + mail); 18 | } 19 | } catch (IOException e) { 20 | e.printStackTrace(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /HideDelegate/again/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | try { 7 | AddressFile file = new AddressFile("address.txt"); 8 | file.set("Hiroshi Yuki", "hyuki@example.com"); 9 | file.set("Tomura", "tomura@example.com"); 10 | file.set("Hanako Sato", "hanako@example.com"); 11 | file.update(); 12 | 13 | Enumeration e = file.names(); 14 | while (e.hasMoreElements()) { 15 | String name = (String)e.nextElement(); 16 | String mail = file.get(name); 17 | System.out.println("name=" + name + ", mail=" + mail); 18 | } 19 | } catch (IOException e) { 20 | e.printStackTrace(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /HideDelegate/step1/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | try { 7 | AddressFile file = new AddressFile("address.txt"); 8 | file.set("Hiroshi Yuki", "hyuki@example.com"); 9 | file.set("Tomura", "tomura@example.com"); 10 | file.set("Hanako Sato", "hanako@example.com"); 11 | file.update(); 12 | 13 | Enumeration e = file.names(); 14 | while (e.hasMoreElements()) { 15 | String name = (String)e.nextElement(); 16 | String mail = file.get(name); 17 | System.out.println("name=" + name + ", mail=" + mail); 18 | } 19 | } catch (IOException e) { 20 | e.printStackTrace(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /HideDelegate/step2/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | try { 7 | AddressFile file = new AddressFile("address.txt"); 8 | file.set("Hiroshi Yuki", "hyuki@example.com"); 9 | file.set("Tomura", "tomura@example.com"); 10 | file.set("Hanako Sato", "hanako@example.com"); 11 | file.update(); 12 | 13 | Enumeration e = file.names(); 14 | while (e.hasMoreElements()) { 15 | String name = (String)e.nextElement(); 16 | String mail = file.get(name); 17 | System.out.println("name=" + name + ", mail=" + mail); 18 | } 19 | } catch (IOException e) { 20 | e.printStackTrace(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /HideDelegate/nested/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | try { 7 | AddressFile file = new AddressFile("address.txt"); 8 | file.set("Hiroshi Yuki", "hyuki@example.com"); 9 | file.set("Tomura", "tomura@example.com"); 10 | file.set("Hanako Sato", "hanako@example.com"); 11 | file.update(); 12 | 13 | Enumeration e = file.names(); 14 | while (e.hasMoreElements()) { 15 | String name = (String)e.nextElement(); 16 | String mail = file.get(name); 17 | System.out.println("name=" + name + ", mail=" + mail); 18 | } 19 | } catch (IOException e) { 20 | e.printStackTrace(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /IntroduceAssertion/after1/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class Main { 4 | private static final Random random = new Random(1234); 5 | 6 | private static void execute(int length) { 7 | // 난수로 데이터 작성 8 | int[] data = new int[length]; 9 | for (int i = 0; i < data.length; i++) { 10 | data[i] = random.nextInt(data.length); 11 | } 12 | 13 | // 데이터 표시 14 | SortSample sorter = new SortSample(data); 15 | System.out.println("BEFORE:" + sorter); 16 | 17 | // 정렬해서 표시 18 | sorter.sort(); 19 | System.out.println(" AFTER:" + sorter); 20 | 21 | System.out.println(); 22 | } 23 | 24 | public static void main(String[] args) { 25 | execute(10); 26 | execute(10); 27 | execute(10); 28 | execute(10); 29 | execute(10); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithClass/before/Item.java: -------------------------------------------------------------------------------- 1 | public class Item { 2 | public static final int TYPECODE_BOOK = 0; 3 | public static final int TYPECODE_DVD = 1; 4 | public static final int TYPECODE_SOFTWARE = 2; 5 | 6 | private final int _typecode; 7 | private final String _title; 8 | private final int _price; 9 | 10 | public Item(int typecode, String title, int price) { 11 | _typecode = typecode; 12 | _title = title; 13 | _price = price; 14 | } 15 | 16 | public int getTypecode() { return _typecode; } 17 | public String getTitle() { return _title; } 18 | public int getPrice() { return _price; } 19 | 20 | public String toString() { 21 | return "[ " 22 | + getTypecode() + ", " 23 | + getTitle() + ", " 24 | + getPrice() + " ]"; 25 | } 26 | // ... 27 | } 28 | -------------------------------------------------------------------------------- /RemoveControlFlag/FindIntAfter1/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | 5 | public class MainTest { 6 | @Test public void testFound() { 7 | int[] data = { 8 | 1, 9, 0, 2, 8, 5, 6, 3, 4, 7, 9 | }; 10 | boolean actual = FindInt.find(data, 5); 11 | boolean expected = true; 12 | assertEquals(expected, actual); 13 | } 14 | 15 | @Test public void testNotFound() { 16 | int[] data = { 17 | 1, 9, 0, 2, 8, 5, 6, 3, 4, 7, 18 | }; 19 | boolean actual = FindInt.find(data, 10); 20 | boolean expected = false; 21 | assertEquals(expected, actual); 22 | } 23 | 24 | public static junit.framework.Test suite() { 25 | return new JUnit4TestAdapter(MainTest.class); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /RemoveControlFlag/FindIntAfter2/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | 5 | public class MainTest { 6 | @Test public void testFound() { 7 | int[] data = { 8 | 1, 9, 0, 2, 8, 5, 6, 3, 4, 7, 9 | }; 10 | boolean actual = FindInt.find(data, 5); 11 | boolean expected = true; 12 | assertEquals(expected, actual); 13 | } 14 | 15 | @Test public void testNotFound() { 16 | int[] data = { 17 | 1, 9, 0, 2, 8, 5, 6, 3, 4, 7, 18 | }; 19 | boolean actual = FindInt.find(data, 10); 20 | boolean expected = false; 21 | assertEquals(expected, actual); 22 | } 23 | 24 | public static junit.framework.Test suite() { 25 | return new JUnit4TestAdapter(MainTest.class); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /RemoveControlFlag/FindIntBefore/MainTest.java: -------------------------------------------------------------------------------- 1 | import static org.junit.Assert.assertEquals; 2 | import junit.framework.JUnit4TestAdapter; 3 | import org.junit.*; 4 | 5 | public class MainTest { 6 | @Test public void testFound() { 7 | int[] data = { 8 | 1, 9, 0, 2, 8, 5, 6, 3, 4, 7, 9 | }; 10 | boolean actual = FindInt.find(data, 5); 11 | boolean expected = true; 12 | assertEquals(expected, actual); 13 | } 14 | 15 | @Test public void testNotFound() { 16 | int[] data = { 17 | 1, 9, 0, 2, 8, 5, 6, 3, 4, 7, 18 | }; 19 | boolean actual = FindInt.find(data, 10); 20 | boolean expected = false; 21 | assertEquals(expected, actual); 22 | } 23 | 24 | public static junit.framework.Test suite() { 25 | return new JUnit4TestAdapter(MainTest.class); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /IntroduceAssertion/after/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class Main { 4 | private static final Random random = new Random(1234); 5 | 6 | private static void execute(int length) { 7 | // 난수로 데이터 작성 8 | int[] data = new int[length]; 9 | for (int i = 0; i < data.length; i++) { 10 | data[i] = random.nextInt(data.length); 11 | } 12 | 13 | // 데이터 표시 14 | SortSample sorter = new SortSample(data); 15 | System.out.println("BEFORE:" + sorter); 16 | 17 | // 정렬해서 표시 18 | sorter.sort(); 19 | System.out.println(" AFTER:" + sorter); 20 | 21 | System.out.println(); 22 | } 23 | 24 | public static void main(String[] args) { 25 | execute(10); 26 | execute(10); 27 | execute(10); 28 | execute(10); 29 | execute(10); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /IntroduceAssertion/after2/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class Main { 4 | private static final Random random = new Random(1234); 5 | 6 | private static void execute(int length) { 7 | // 난수로 데이터 작성 8 | int[] data = new int[length]; 9 | for (int i = 0; i < data.length; i++) { 10 | data[i] = random.nextInt(data.length); 11 | } 12 | 13 | // 데이터 표시 14 | SortSample sorter = new SortSample(data); 15 | System.out.println("BEFORE:" + sorter); 16 | 17 | // 정렬해서 표시 18 | sorter.sort(); 19 | System.out.println(" AFTER:" + sorter); 20 | 21 | System.out.println(); 22 | } 23 | 24 | public static void main(String[] args) { 25 | execute(10); 26 | execute(10); 27 | execute(10); 28 | execute(10); 29 | execute(10); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /IntroduceAssertion/before/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class Main { 4 | private static final Random random = new Random(1234); 5 | 6 | private static void execute(int length) { 7 | // 난수로 데이터 작성 8 | int[] data = new int[length]; 9 | for (int i = 0; i < data.length; i++) { 10 | data[i] = random.nextInt(data.length); 11 | } 12 | 13 | // 데이터 표시 14 | SortSample sorter = new SortSample(data); 15 | System.out.println("BEFORE:" + sorter); 16 | 17 | // 정렬해서 표시 18 | sorter.sort(); 19 | System.out.println(" AFTER:" + sorter); 20 | 21 | System.out.println(); 22 | } 23 | 24 | public static void main(String[] args) { 25 | execute(10); 26 | execute(10); 27 | execute(10); 28 | execute(10); 29 | execute(10); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /TeaseApartInheritance/before/CSVStringTablePrinter.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVStringTablePrinter extends CSVStringReader { 4 | public CSVStringTablePrinter(String string) { 5 | super(string); 6 | } 7 | 8 | public void print() throws IOException { 9 | System.out.println(""); 10 | for (int row = 0; true; row++) { 11 | String[] item = readCSV(); 12 | if (item == null) { 13 | break; 14 | } 15 | System.out.print(""); 16 | for (int column = 0; column < item.length; column++) { 17 | System.out.print(""); 20 | } 21 | System.out.println(""); 22 | } 23 | System.out.println("
"); 18 | System.out.print(item[column]); 19 | System.out.print("
"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step2/CSVStringTablePrinter.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVStringTablePrinter extends CSVStringReader { 4 | public CSVStringTablePrinter(String string) { 5 | super(string); 6 | } 7 | 8 | public void print() throws IOException { 9 | System.out.println(""); 10 | for (int row = 0; true; row++) { 11 | String[] item = readCSV(); 12 | if (item == null) { 13 | break; 14 | } 15 | System.out.print(""); 16 | for (int column = 0; column < item.length; column++) { 17 | System.out.print(""); 20 | } 21 | System.out.println(""); 22 | } 23 | System.out.println("
"); 18 | System.out.print(item[column]); 19 | System.out.print("
"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /TeaseApartInheritance/step4/CSVStringTablePrinter.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | public class CSVStringTablePrinter extends CSVStringReader { 4 | public CSVStringTablePrinter(String string) { 5 | super(string); 6 | } 7 | 8 | public void print() throws IOException { 9 | System.out.println(""); 10 | for (int row = 0; true; row++) { 11 | String[] item = readCSV(); 12 | if (item == null) { 13 | break; 14 | } 15 | System.out.print(""); 16 | for (int column = 0; column < item.length; column++) { 17 | System.out.print(""); 20 | } 21 | System.out.println(""); 22 | } 23 | System.out.println("
"); 18 | System.out.print(item[column]); 19 | System.out.print("
"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /DuplicateObservedData/after/Value.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Value { 4 | private int _value = 0; 5 | private final List _listeners = new ArrayList(); 6 | 7 | public Value(int value) { 8 | _value = value; 9 | } 10 | public void setValue(int value) { 11 | _value = value; 12 | notifyToListeners(); 13 | } 14 | public int getValue() { 15 | return _value; 16 | } 17 | public void addValueListener(ValueListener listener) { 18 | _listeners.add(listener); 19 | } 20 | public boolean removeValueListener(ValueListener listener) { 21 | return _listeners.remove(listener); 22 | } 23 | private void notifyToListeners() { 24 | for (ValueListener listener : _listeners) { 25 | listener.valueChanged(new ValueChangeEvent(this)); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DuplicateObservedData/graph/Value.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Value { 4 | private int _value = 0; 5 | private final List _listeners = new ArrayList(); 6 | 7 | public Value(int value) { 8 | _value = value; 9 | } 10 | public void setValue(int value) { 11 | _value = value; 12 | notifyToListeners(); 13 | } 14 | public int getValue() { 15 | return _value; 16 | } 17 | public void addValueListener(ValueListener listener) { 18 | _listeners.add(listener); 19 | } 20 | public boolean removeValueListener(ValueListener listener) { 21 | return _listeners.remove(listener); 22 | } 23 | private void notifyToListeners() { 24 | for (ValueListener listener : _listeners) { 25 | listener.valueChanged(new ValueChangeEvent(this)); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /DuplicateObservedData/step3/Value.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Value { 4 | private int _value = 0; 5 | private final List _listeners = new ArrayList(); 6 | 7 | public Value(int value) { 8 | _value = value; 9 | } 10 | public void setValue(int value) { 11 | _value = value; 12 | notifyToListeners(); 13 | } 14 | public int getValue() { 15 | return _value; 16 | } 17 | public void addValueListener(ValueListener listener) { 18 | _listeners.add(listener); 19 | } 20 | public boolean removeValueListener(ValueListener listener) { 21 | return _listeners.remove(listener); 22 | } 23 | private void notifyToListeners() { 24 | for (ValueListener listener : _listeners) { 25 | listener.valueChanged(new ValueChangeEvent(this)); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /HideDelegate/after/Database.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Database { 5 | private final Properties _properties; 6 | private final String _filename; 7 | public Database(String filename) { 8 | _filename = filename; 9 | _properties = new Properties(); 10 | try { 11 | _properties.load(new FileInputStream(_filename)); 12 | } catch (IOException ignore) { 13 | } 14 | } 15 | public void set(String key, String value) { 16 | _properties.setProperty(key, value); 17 | } 18 | public String get(String key) { 19 | return _properties.getProperty(key, null); 20 | } 21 | public void update() throws IOException { 22 | _properties.store(new FileOutputStream(_filename), ""); 23 | } 24 | public Properties getProperties() { 25 | return _properties; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /HideDelegate/before/Database.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Database { 5 | private final Properties _properties; 6 | private final String _filename; 7 | public Database(String filename) { 8 | _filename = filename; 9 | _properties = new Properties(); 10 | try { 11 | _properties.load(new FileInputStream(_filename)); 12 | } catch (IOException ignore) { 13 | } 14 | } 15 | public void set(String key, String value) { 16 | _properties.setProperty(key, value); 17 | } 18 | public String get(String key) { 19 | return _properties.getProperty(key, null); 20 | } 21 | public void update() throws IOException { 22 | _properties.store(new FileOutputStream(_filename), ""); 23 | } 24 | public Properties getProperties() { 25 | return _properties; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /HideDelegate/step1/Database.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Database { 5 | private final Properties _properties; 6 | private final String _filename; 7 | public Database(String filename) { 8 | _filename = filename; 9 | _properties = new Properties(); 10 | try { 11 | _properties.load(new FileInputStream(_filename)); 12 | } catch (IOException ignore) { 13 | } 14 | } 15 | public void set(String key, String value) { 16 | _properties.setProperty(key, value); 17 | } 18 | public String get(String key) { 19 | return _properties.getProperty(key, null); 20 | } 21 | public void update() throws IOException { 22 | _properties.store(new FileOutputStream(_filename), ""); 23 | } 24 | public Properties getProperties() { 25 | return _properties; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /HideDelegate/step2/Database.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Database { 5 | private final Properties _properties; 6 | private final String _filename; 7 | public Database(String filename) { 8 | _filename = filename; 9 | _properties = new Properties(); 10 | try { 11 | _properties.load(new FileInputStream(_filename)); 12 | } catch (IOException ignore) { 13 | } 14 | } 15 | public void set(String key, String value) { 16 | _properties.setProperty(key, value); 17 | } 18 | public String get(String key) { 19 | return _properties.getProperty(key, null); 20 | } 21 | public void update() throws IOException { 22 | _properties.store(new FileOutputStream(_filename), ""); 23 | } 24 | public Properties getProperties() { 25 | return _properties; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/typecode/Robot.java: -------------------------------------------------------------------------------- 1 | public class Robot { 2 | private final String _name; 3 | public static final RobotCommand COMMAND_WALK = new RobotCommand("WALK"); 4 | public static final RobotCommand COMMAND_STOP = new RobotCommand("STOP"); 5 | public static final RobotCommand COMMAND_JUMP = new RobotCommand("JUMP"); 6 | public Robot(String name) { 7 | _name = name; 8 | } 9 | public void order(RobotCommand command) { 10 | if (command == COMMAND_WALK) { 11 | System.out.println(_name + " walks."); 12 | } else if (command == COMMAND_STOP) { 13 | System.out.println(_name + " stops."); 14 | } else if (command == COMMAND_JUMP) { 15 | System.out.println(_name + " jumps."); 16 | } else { 17 | System.out.println("Command error. command = " + command); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /HideDelegate/again/Database.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Database { 5 | private final Properties _properties; 6 | private final String _filename; 7 | public Database(String filename) { 8 | _filename = filename; 9 | _properties = new Properties(); 10 | try { 11 | _properties.load(new FileInputStream(_filename)); 12 | } catch (IOException ignore) { 13 | } 14 | } 15 | public void set(String key, String value) { 16 | _properties.setProperty(key, value); 17 | } 18 | public String get(String key) { 19 | return _properties.getProperty(key, null); 20 | } 21 | public void update() throws IOException { 22 | _properties.store(new FileOutputStream(_filename), ""); 23 | } 24 | public Enumeration keys() { 25 | return _properties.propertyNames(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /ReplaceMagicNumberWithSymbolicConstant/typecheck/Robot.java: -------------------------------------------------------------------------------- 1 | public class Robot { 2 | private final String _name; 3 | public static final RobotCommand COMMAND_WALK = new RobotCommand() { 4 | public void apply(Robot robot) { 5 | System.out.println(robot._name + " walks."); 6 | } 7 | }; 8 | public static final RobotCommand COMMAND_STOP = new RobotCommand() { 9 | public void apply(Robot robot) { 10 | System.out.println(robot._name + " stops."); 11 | } 12 | }; 13 | public static final RobotCommand COMMAND_JUMP = new RobotCommand() { 14 | public void apply(Robot robot) { 15 | System.out.println(robot._name + " jumps."); 16 | } 17 | }; 18 | public Robot(String name) { 19 | _name = name; 20 | } 21 | public void order(RobotCommand command) { 22 | command.apply(this); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ReplaceTypeCodeWithSubclasses/factory/Shape.java: -------------------------------------------------------------------------------- 1 | public abstract class Shape { 2 | private final int _startx; 3 | private final int _starty; 4 | private final int _endx; 5 | private final int _endy; 6 | 7 | public static Shape createShape(ShapeFactory factory, int startx, int starty, int endx, int endy) { 8 | return factory.create(startx, starty, endx, endy); 9 | } 10 | 11 | public Shape(int startx, int starty, int endx, int endy) { 12 | _startx = startx; 13 | _endx = endx; 14 | _starty = starty; 15 | _endy = endy; 16 | } 17 | 18 | public abstract String getName(); 19 | 20 | public String toString() { 21 | return "[ " 22 | + getName() + ", " 23 | + "(" + _startx + ", " + _starty + ")-" 24 | + "(" + _endx + ", " + _endy + ") ]"; 25 | } 26 | 27 | public abstract void draw(); 28 | } 29 | -------------------------------------------------------------------------------- /HideDelegate/before/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | try { 7 | AddressFile file = new AddressFile("address.txt"); 8 | file.getDatabase().set("Hiroshi Yuki", "hyuki@example.com"); 9 | file.getDatabase().set("Tomura", "tomura@example.com"); 10 | file.getDatabase().set("Hanako Sato", "hanako@example.com"); 11 | file.getDatabase().update(); 12 | 13 | Enumeration e = file.names(); 14 | while (e.hasMoreElements()) { 15 | String name = (String)e.nextElement(); 16 | String mail = file.getDatabase().get(name); 17 | System.out.println("name=" + name + ", mail=" + mail); 18 | } 19 | } catch (IOException e) { 20 | e.printStackTrace(); 21 | } 22 | } 23 | } 24 | --------------------------------------------------------------------------------