Last active
November 6, 2016 07:17
-
-
Save shoheiyokoyama/0def5b93834e408e2ca2 to your computer and use it in GitHub Desktop.
デザインパターン「Adapter」 ref: http://qiita.com/shoheiyokoyama/items/bd1c692db480b640c976
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Apple=iPod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public interface FileIo { | |
public void readFromFile(String filename) throws IOException; | |
public void writeToFile(String filename) throws IOException; | |
public void setValue(String key, String value); | |
public String getValue(String key); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
public class FileProperties extends Properties implements FileIo { | |
public void readFromFile(String filename) throws IOException { | |
load(new FileInputStream(filename)); | |
} | |
public void writeToFile(String filename) throws IOException { | |
store(new FileOutputStream(filename), "Written by FileProperties"); | |
} | |
public void setValue(String key, String value) { | |
setProperty(key, value); | |
} | |
public String getValue(String key) { | |
return getProperty(key, ""); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
FileIo fileIo = new FileProperties();//※ | |
try { | |
fileIo.readFromFile("file.txt"); | |
fileIo.setValue("Apple", "iPhone"); | |
fileIo.setValue("Google", "Android"); | |
fileIo.setValue("Git", "Hub"); | |
fileIo.setValue("DesignPattern", "Adapter"); | |
fileIo.writeToFile("newfile.txt"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Written by FileProperties | |
#Sun Feb 07 20:49:50 JST 2016 | |
DesignPattern=Adapter | |
Apple=iPhone | |
Git=Hub | |
Google=Android |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment