File tree 24 files changed +544
-1
lines changed
template_method/inheritance 24 files changed +544
-1
lines changed Original file line number Diff line number Diff line change
1
+ package j .facade ;
2
+
3
+ import j .facade .pagemaker .PageMaker ;
4
+
5
+ public class Main {
6
+ public static void main (String [] args ) {
7
+ PageMaker .
makeWelcomePage (
"[email protected] " ,
"welcome.html" );
8
+ }
9
+ }
Original file line number Diff line number Diff line change
1
+
2
+
3
+
4
+ [email protected] =Mamoru Takahashi
Original file line number Diff line number Diff line change
1
+ package j .facade .pagemaker ;
2
+
3
+ import java .io .FileReader ;
4
+ import java .io .IOException ;
5
+ import java .util .Properties ;
6
+
7
+ public class Database {
8
+ private Database () {
9
+ }
10
+
11
+ // データベース名からPropertiesを得る
12
+ public static Properties getProperties (String dbname ) throws IOException {
13
+ String filename = dbname + ".txt" ;
14
+ Properties prop = new Properties ();
15
+ prop .load (new FileReader (filename ));
16
+ return prop ;
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ package j .facade .pagemaker ;
2
+
3
+ import java .io .IOException ;
4
+ import java .io .Writer ;
5
+
6
+ public class HtmlWriter {
7
+ private Writer writer ;
8
+
9
+ public HtmlWriter (Writer writer ) {
10
+ this .writer = writer ;
11
+ }
12
+
13
+ // タイトルの出力
14
+ public void title (String title ) throws IOException {
15
+ writer .write ("<!DOCTYPE html>" );
16
+ writer .write ("<html>" );
17
+ writer .write ("<head>" );
18
+ writer .write ("<title>" + title + "</title>" );
19
+ writer .write ("</head>" );
20
+ writer .write ("<body>" );
21
+ writer .write ("\n " );
22
+ writer .write ("<h1>" + title + "</h1>" );
23
+ writer .write ("\n " );
24
+ }
25
+
26
+ // 段落の出力
27
+ public void paragraph (String msg ) throws IOException {
28
+ writer .write ("<p>" + msg + "</p>" );
29
+ writer .write ("\n " );
30
+ }
31
+
32
+ // リンクの出力
33
+ public void link (String href , String caption ) throws IOException {
34
+ paragraph ("<a href=\" " + href + "\" >" + caption + "</a>" );
35
+ }
36
+
37
+ // メールアドレスの出力
38
+ public void mailto (String mailaddr , String username ) throws IOException {
39
+ link ("mailto:" + mailaddr , username );
40
+ }
41
+
42
+ // 閉じる
43
+ public void close () throws IOException {
44
+ writer .write ("</body>" );
45
+ writer .write ("</html>" );
46
+ writer .write ("\n " );
47
+ writer .close ();
48
+ }
49
+ }
Original file line number Diff line number Diff line change
1
+ package j .facade .pagemaker ;
2
+
3
+ import java .io .FileWriter ;
4
+ import java .io .IOException ;
5
+ import java .util .Properties ;
6
+
7
+ public class PageMaker {
8
+ private PageMaker () {
9
+ }
10
+
11
+ public static void makeWelcomePage (String mailaddr , String filename ) {
12
+ try {
13
+ Properties mailprop = Database .getProperties ("maildata" );
14
+ String username = mailprop .getProperty (mailaddr );
15
+ HtmlWriter writer = new HtmlWriter (new FileWriter (filename ));
16
+ writer .title (username + "'s web page" );
17
+ writer .paragraph ("Welcome to " + username + "'s web page!" );
18
+ writer .paragraph ("Nice to meet you!" );
19
+ writer .mailto (mailaddr , username );
20
+ writer .close ();
21
+ System .out .println (filename + " is created for " + mailaddr + " (" + username + ")" );
22
+ } catch (IOException e ) {
23
+ e .printStackTrace ();
24
+ }
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head > < title > Hiroshi Yuki's web page</ title > </ head >
4
+ < body >
5
+ < h1 > Hiroshi Yuki's web page</ h1 >
6
+ < p > Welcome to Hiroshi Yuki's web page!</ p >
7
+ < p > Nice to meet you!</ p >
8
+ < p > < a href ="
mailto:[email protected] "
> Hiroshi Yuki
</ a > </ p >
9
+ </ body >
10
+ </ html >
Original file line number Diff line number Diff line change
1
+ package j .observer .inheritance ;
2
+
3
+ public class DigitObserver implements Observer {
4
+ @ Override
5
+ public void update (NumberGenerator generator ) {
6
+ System .out .println ("DigitObserver:" + generator .getNumber ());
7
+ try {
8
+ Thread .sleep (100 );
9
+ } catch (InterruptedException e ) {
10
+ }
11
+ }
12
+ }
Original file line number Diff line number Diff line change
1
+ package j .observer .inheritance ;
2
+
3
+ public class GraphObserver implements Observer {
4
+ @ Override
5
+ public void update (NumberGenerator generator ) {
6
+ System .out .print ("GraphObserver:" );
7
+ int count = generator .getNumber ();
8
+ for (int i = 0 ; i < count ; i ++) {
9
+ System .out .print ("*" );
10
+ }
11
+ System .out .println ("" );
12
+ try {
13
+ Thread .sleep (100 );
14
+ } catch (InterruptedException e ) {
15
+ }
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ package j .observer .inheritance ;
2
+
3
+ public class Main {
4
+ public static void main (String [] args ) {
5
+ NumberGenerator generator = new RandomNumberGenerator ();
6
+ Observer observer1 = new DigitObserver ();
7
+ Observer observer2 = new GraphObserver ();
8
+ generator .addObserver (observer1 );
9
+ generator .addObserver (observer2 );
10
+ generator .execute ();
11
+ }
12
+ }
Original file line number Diff line number Diff line change
1
+ package j .observer .inheritance ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+
6
+ public abstract class NumberGenerator {
7
+ // 保持しているObserverたち
8
+ private List <Observer > observers = new ArrayList <>();
9
+
10
+ // Observerを追加する
11
+ public void addObserver (Observer observer ) {
12
+ observers .add (observer );
13
+ }
14
+
15
+ // Observerを削除する
16
+ public void deleteObserver (Observer observer ) {
17
+ observers .remove (observer );
18
+ }
19
+
20
+ // Observerへ通知する
21
+ public void notifyObservers () {
22
+ for (Observer o : observers ) {
23
+ o .update (this );
24
+ }
25
+ }
26
+
27
+ // 数を取得する
28
+ public abstract int getNumber ();
29
+
30
+ // 数を生成する
31
+ public abstract void execute ();
32
+ }
Original file line number Diff line number Diff line change
1
+ package j .observer .inheritance ;
2
+
3
+ public interface Observer {
4
+ public abstract void update (NumberGenerator generator );
5
+ }
Original file line number Diff line number Diff line change
1
+ package j .observer .inheritance ;
2
+
3
+ import java .util .Random ;
4
+
5
+ public class RandomNumberGenerator extends NumberGenerator {
6
+ private Random random = new Random (); // 乱数生成器
7
+ private int number ; // 現在の数
8
+
9
+ // 数を取得する
10
+ @ Override
11
+ public int getNumber () {
12
+ return number ;
13
+ }
14
+
15
+ // 数を生成する
16
+ @ Override
17
+ public void execute () {
18
+ for (int i = 0 ; i < 20 ; i ++) {
19
+ number = random .nextInt (50 );
20
+ notifyObservers ();
21
+ }
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ package j .prototype .inheritance ;
2
+
3
+ import j .prototype .inheritance .framework .Manager ;
4
+ import j .prototype .inheritance .framework .Product ;
5
+
6
+ public class Main {
7
+ public static void main (String [] args ) {
8
+ // 準備
9
+ Manager manager = new Manager ();
10
+ UnderlinePen upen = new UnderlinePen ('-' );
11
+ MessageBox mbox = new MessageBox ('*' );
12
+ MessageBox sbox = new MessageBox ('/' );
13
+
14
+ // 登録
15
+ manager .register ("strong message" , upen );
16
+ manager .register ("warning box" , mbox );
17
+ manager .register ("slash box" , sbox );
18
+
19
+ // 生成と使用
20
+ Product p1 = manager .create ("strong message" );
21
+ p1 .use ("Hello, world." );
22
+
23
+ Product p2 = manager .create ("warning box" );
24
+ p2 .use ("Hello, world." );
25
+
26
+ Product p3 = manager .create ("slash box" );
27
+ p3 .use ("Hello, world." );
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ package j .prototype .inheritance ;
2
+
3
+ import j .prototype .inheritance .framework .Product ;
4
+
5
+ public class MessageBox implements Product {
6
+ private char decochar ;
7
+
8
+ public MessageBox (char decochar ) {
9
+ this .decochar = decochar ;
10
+ }
11
+
12
+ @ Override
13
+ public void use (String s ) {
14
+ int decolen = 1 + s .length () + 1 ;
15
+ for (int i = 0 ; i < decolen ; i ++) {
16
+ System .out .print (decochar );
17
+ }
18
+ System .out .println ();
19
+ System .out .println (decochar + s + decochar );
20
+ for (int i = 0 ; i < decolen ; i ++) {
21
+ System .out .print (decochar );
22
+ }
23
+ System .out .println ();
24
+ }
25
+
26
+ @ Override
27
+ public Product createCopy () {
28
+ Product p = null ;
29
+ try {
30
+ p = (Product ) clone ();
31
+ } catch (CloneNotSupportedException e ) {
32
+ e .printStackTrace ();
33
+ }
34
+ return p ;
35
+ }
36
+ }
Original file line number Diff line number Diff line change
1
+ package j .prototype .inheritance ;
2
+
3
+ import j .prototype .inheritance .framework .Product ;
4
+
5
+ public class UnderlinePen implements Product {
6
+ private char ulchar ;
7
+
8
+ public UnderlinePen (char ulchar ) {
9
+ this .ulchar = ulchar ;
10
+ }
11
+
12
+ @ Override
13
+ public void use (String s ) {
14
+ int ulen = s .length ();
15
+ System .out .println (s );
16
+ for (int i = 0 ; i < ulen ; i ++) {
17
+ System .out .print (ulchar );
18
+ }
19
+ System .out .println ();
20
+ }
21
+
22
+ @ Override
23
+ public Product createCopy () {
24
+ Product p = null ;
25
+ try {
26
+ p = (Product ) clone ();
27
+ } catch (CloneNotSupportedException e ) {
28
+ e .printStackTrace ();
29
+ }
30
+ return p ;
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ package j .prototype .inheritance .framework ;
2
+
3
+ import java .util .HashMap ;
4
+ import java .util .Map ;
5
+
6
+ public class Manager {
7
+ private Map <String , Product > showcase = new HashMap <>();
8
+
9
+ public void register (String name , Product prototype ) {
10
+ showcase .put (name , prototype );
11
+ }
12
+
13
+ public Product create (String prototypeName ) {
14
+ Product p = showcase .get (prototypeName );
15
+ return p .createCopy ();
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ package j .prototype .inheritance .framework ;
2
+
3
+ public interface Product extends Cloneable {
4
+ public abstract void use (String s );
5
+
6
+ public abstract Product createCopy ();
7
+ }
Original file line number Diff line number Diff line change
1
+ package j .state .delegate ;
2
+
3
+ public interface Context {
4
+ public abstract void setClock (int hour ); // 時刻の設定
5
+
6
+ public abstract void changeState (State state ); // 状態変化
7
+
8
+ public abstract void callSecurityCenter (String msg ); // 警備センター警備員呼び出し
9
+
10
+ public abstract void recordLog (String msg ); // 警備センター記録
11
+ }
You can’t perform that action at this time.
0 commit comments