Skip to content

Commit 68627b9

Browse files
author
ChenShao_sina
committed
AppCompatActivity
1 parent 6dcc950 commit 68627b9

14 files changed

Lines changed: 352 additions & 1 deletion

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Activity/Fragment中onCreate、onStart。。之类,多少也体现了模版方法模式
2+
所以,看山不是山
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.youngpeanut.designpatterns.TemplateMethod.demo1;
2+
3+
/**
4+
* An abstract class which can get content from a file or a HTTP URL
5+
* or other resource
6+
*/
7+
public abstract class AbstractRead {
8+
protected String resource;
9+
public void getContent() { // Template Method
10+
if(open()) {
11+
readContent();
12+
close();
13+
}
14+
}
15+
public void setResource(String s) {
16+
resource = s;
17+
}
18+
protected abstract boolean open();
19+
protected abstract void readContent();
20+
protected abstract void close();
21+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.github.youngpeanut.designpatterns.TemplateMethod.demo1;
2+
3+
/**
4+
* A concrete class extends AbstractRead
5+
* This class can read from a file
6+
*/
7+
import java.io.*;
8+
9+
public class ReadFile extends AbstractRead {
10+
private BufferedReader in = null;
11+
public ReadFile() {
12+
}
13+
public ReadFile(String fileName) {
14+
resource = fileName;
15+
}
16+
protected boolean open() {
17+
try {
18+
in = new BufferedReader(new FileReader(resource));
19+
} catch(IOException e) {
20+
System.out.println("Can not open file!");
21+
return false;
22+
}
23+
return true;
24+
}
25+
protected void readContent() {
26+
try {
27+
if(in != null) {
28+
String str;
29+
while((str = in.readLine()) != null) {
30+
System.out.println(str);
31+
}
32+
}
33+
} catch(IOException e) {
34+
System.out.println("Read file error !");
35+
}
36+
}
37+
protected void close() {
38+
if(in != null) {
39+
try {
40+
in.close();
41+
} catch(IOException e) {
42+
System.out.println("IO error !");
43+
}
44+
}
45+
}
46+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.github.youngpeanut.designpatterns.TemplateMethod.demo1; /**
2+
* A concrete class extends AbstractRead
3+
* This class can read HTML from a HTTP URL
4+
*/
5+
import java.io.*;
6+
import java.net.*;
7+
8+
public class ReadHtml extends AbstractRead {
9+
private URLConnection conn;
10+
private BufferedReader in;
11+
12+
public ReadHtml() {
13+
}
14+
public ReadHtml(String s) {
15+
resource = s;
16+
}
17+
18+
public boolean open() {
19+
try {
20+
URL url = new URL(resource);
21+
conn = url.openConnection();
22+
in = new BufferedReader (
23+
new InputStreamReader(conn.getInputStream()));
24+
} catch (MalformedURLException e) {
25+
System.out.println("Uable to connect URL:" + resource);
26+
return false;
27+
} catch (IOException e) {
28+
System.out.println("IOExeption when connecting to URL" + resource);
29+
return false;
30+
}
31+
return true;
32+
}
33+
protected void readContent() {
34+
try {
35+
if(in != null) {
36+
String str;
37+
while((str = in.readLine()) != null) {
38+
System.out.println(str);
39+
}
40+
}
41+
} catch(IOException e) {
42+
System.out.println("Read file error !");
43+
}
44+
}
45+
protected void close() {
46+
if(in != null) {
47+
try {
48+
in.close();
49+
} catch(IOException e) {
50+
System.out.println("IO error !");
51+
}
52+
}
53+
}
54+
55+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.youngpeanut.designpatterns.TemplateMethod.demo1;
2+
3+
/**
4+
* A test client
5+
*/
6+
public class Test {
7+
public static void main(String[] args) {
8+
// You should change the path of "README.md" in your local machine
9+
String fileName = "..\\README.md";
10+
String url = "http://www.github.com";
11+
12+
AbstractRead fileRead = new ReadFile();
13+
AbstractRead htmlRead = new ReadHtml();
14+
15+
fileRead.setResource(fileName);
16+
htmlRead.setResource(url);
17+
18+
System.out.println("----- Read from a file -----");
19+
fileRead.getContent();
20+
System.out.println("----- Read from a url -----");
21+
htmlRead.getContent();
22+
}
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.github.youngpeanut.designpatterns.java.nio;
2+
3+
/**
4+
* Created by chenshao on 16/11/8.
5+
*/
6+
public class Selector {
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.github.youngpeanut.designpatterns.proxy.ActivityDelegate;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.NonNull;
5+
import android.support.annotation.Nullable;
6+
7+
/**
8+
* Created by chenshao on 16/11/10.
9+
*/
10+
public class AppCompatActivity {
11+
private AppCompatDelegat mDelegate;
12+
13+
14+
// @Override
15+
protected void onCreate(@Nullable Bundle savedInstanceState) {
16+
final AppCompatDelegat delegate = getDelegate();
17+
delegate.onCreate(savedInstanceState);
18+
19+
}
20+
21+
22+
23+
24+
25+
/**
26+
* @return The {@link AppCompatDelegat} being used by this Activity.
27+
*/
28+
@NonNull
29+
public AppCompatDelegat getDelegate() {
30+
if (mDelegate == null) {
31+
// mDelegate = AppCompatDelegat.create(this, this);
32+
}
33+
return mDelegate;
34+
}
35+
36+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.github.youngpeanut.designpatterns.proxy.ActivityDelegate;
2+
3+
import android.content.Context;
4+
import android.os.Build;
5+
import android.os.Bundle;
6+
import android.support.v7.app.AppCompatCallback;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.view.Window;
10+
11+
/**
12+
* 示例代码,详见android.support.v7.app.AppCompatDelegate
13+
* Created by chenshao on 16/11/10.
14+
*/
15+
public abstract class AppCompatDelegat {
16+
17+
18+
/**
19+
* Private constructor
20+
*/
21+
AppCompatDelegat() {}
22+
23+
public abstract void onCreate(Bundle savedInstanceState);
24+
25+
public abstract void onDestroy();
26+
27+
public abstract void setContentView(View v, ViewGroup.LayoutParams lp);
28+
29+
public abstract void addContentView(View v, ViewGroup.LayoutParams lp);
30+
31+
32+
private static AppCompatDelegat create(Context context, Window window,
33+
AppCompatCallback callback) {
34+
final int sdk = Build.VERSION.SDK_INT;
35+
// if (BuildCompat.isAtLeastN()) {
36+
// return new AppCompatDelegateImplN(context, window, callback);
37+
// } else if (sdk >= 23) {
38+
// return new AppCompatDelegateImplV23(context, window, callback);
39+
// } else if (sdk >= 14) {
40+
// return new AppCompatDelegateImplV14(context, window, callback);
41+
// } else
42+
if (sdk >= 11) {
43+
return new AppCompatDelegatImplV11(context, window, callback);
44+
} else {
45+
return new AppCompatDelegatImplV9(context, window, callback);
46+
}
47+
}
48+
49+
50+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.github.youngpeanut.designpatterns.proxy.ActivityDelegate;
2+
3+
import android.content.Context;
4+
import android.os.Bundle;
5+
import android.support.v7.app.AppCompatCallback;
6+
import android.util.Log;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.view.Window;
10+
11+
/**
12+
* Created by chenshao on 16/11/10.
13+
*/
14+
public class AppCompatDelegatImplV11 extends AppCompatDelegat {
15+
public static final String TAG = "AppCompatDelegatImplV9";
16+
17+
public AppCompatDelegatImplV11(Context context, Window window, AppCompatCallback callback) {
18+
19+
}
20+
21+
@Override
22+
public void onCreate(Bundle savedInstanceState) {
23+
Log.d(TAG,"onCreate impl by AppCompatDelegatImplV9");
24+
}
25+
26+
@Override
27+
public void onDestroy() {
28+
Log.d(TAG,"onDestroy impl by AppCompatDelegatImplV9");
29+
30+
}
31+
32+
@Override
33+
public void setContentView(View v, ViewGroup.LayoutParams lp) {
34+
Log.d(TAG,"setContentView impl by AppCompatDelegatImplV9");
35+
36+
}
37+
38+
@Override
39+
public void addContentView(View v, ViewGroup.LayoutParams lp) {
40+
Log.d(TAG,"addContentView impl by AppCompatDelegatImplV9");
41+
42+
}
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.github.youngpeanut.designpatterns.proxy.ActivityDelegate;
2+
3+
import android.content.Context;
4+
import android.os.Bundle;
5+
import android.support.v7.app.AppCompatCallback;
6+
import android.util.Log;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.view.Window;
10+
11+
/**
12+
* Created by chenshao on 16/11/10.
13+
*/
14+
public class AppCompatDelegatImplV9 extends AppCompatDelegat {
15+
public static final String TAG = "AppCompatDelegatImplV9";
16+
17+
public AppCompatDelegatImplV9(Context context, Window window, AppCompatCallback callback) {
18+
19+
}
20+
21+
@Override
22+
public void onCreate(Bundle savedInstanceState) {
23+
Log.d(TAG,"onCreate impl by AppCompatDelegatImplV9");
24+
}
25+
26+
@Override
27+
public void onDestroy() {
28+
Log.d(TAG,"onDestroy impl by AppCompatDelegatImplV9");
29+
30+
}
31+
32+
@Override
33+
public void setContentView(View v, ViewGroup.LayoutParams lp) {
34+
Log.d(TAG,"setContentView impl by AppCompatDelegatImplV9");
35+
36+
}
37+
38+
@Override
39+
public void addContentView(View v, ViewGroup.LayoutParams lp) {
40+
Log.d(TAG,"addContentView impl by AppCompatDelegatImplV9");
41+
42+
}
43+
}

0 commit comments

Comments
 (0)