Skip to content

Commit 30d4ab3

Browse files
author
sheng
committed
dp
1 parent 8807be3 commit 30d4ab3

17 files changed

+290
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.coderising.dp.bridge;
2+
3+
public class Circle extends Shape {
4+
5+
@Override
6+
public void draw() {
7+
super.getDrawing().drawCircle();
8+
System.out.println("I'm a Line");
9+
10+
}
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.coderising.dp.bridge;
2+
3+
public interface Drawing {
4+
void drawLine();
5+
void drawCircle();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coderising.dp.bridge;
2+
3+
public class DrawingGL1 implements Drawing{
4+
5+
@Override
6+
public void drawLine() {
7+
System.out.println("DrawingGL1.drawLine");
8+
}
9+
10+
@Override
11+
public void drawCircle() {
12+
System.out.println("DrawingGL1.drawCircle");
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coderising.dp.bridge;
2+
3+
public class DrawingGL2 implements Drawing{
4+
5+
@Override
6+
public void drawLine() {
7+
System.out.println("DrawingGL2.drawLine");
8+
}
9+
10+
@Override
11+
public void drawCircle() {
12+
System.out.println("DrawingGL2.drawCircle");
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.coderising.dp.bridge;
2+
3+
public class Rectangle extends Shape {
4+
5+
@Override
6+
public void draw() {
7+
super.getDrawing().drawLine();
8+
System.out.println("I'm a Rectangle");
9+
}
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.coderising.dp.bridge;
2+
3+
public abstract class Shape {
4+
public Drawing drawing;
5+
public Drawing getDrawing() {
6+
return drawing;
7+
}
8+
public void setDrawing(Drawing drawing) {
9+
this.drawing = drawing;
10+
}
11+
public abstract void draw();
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.coderising.dp.bridge;
2+
3+
public class Test {
4+
5+
public static void main(String[] args) {
6+
Shape rectangel=new Rectangle();
7+
rectangel.setDrawing(new DrawingGL1());
8+
rectangel.draw();
9+
10+
11+
Shape circle=new Circle();
12+
circle.setDrawing(new DrawingGL2());
13+
circle.draw();
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.coderising.dp.composite;
2+
3+
public class Line implements Shape {
4+
5+
@Override
6+
public void draw() {
7+
System.out.println("I'm a Line");
8+
9+
}
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.coderising.dp.composite;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Picture implements Shape{
7+
private List<Shape> shapes=new ArrayList<>();
8+
@Override
9+
public void draw() {
10+
for (Shape shape : shapes) {
11+
shape.draw();
12+
}
13+
}
14+
15+
public void add(Shape shape){
16+
shapes.add(shape);
17+
}
18+
19+
public static void main(String[] args) {
20+
Picture aPicture=new Picture();
21+
aPicture.add(new Line());
22+
aPicture.add(new Rectangle());
23+
24+
Picture a=new Picture();
25+
a.add(new Text());
26+
a.add(new Line());
27+
a.add(new Square());
28+
aPicture.add(a);
29+
30+
aPicture.draw();
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.coderising.dp.composite;
2+
3+
public class Rectangle implements Shape {
4+
5+
@Override
6+
public void draw() {
7+
System.out.println("I'm a Rectangle");
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.coderising.dp.composite;
2+
3+
public interface Shape {
4+
public void draw();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.coderising.dp.composite;
2+
3+
public class Square implements Shape {
4+
5+
@Override
6+
public void draw() {
7+
System.out.println("I'm a Square");
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.coderising.dp.composite;
2+
3+
public class Text implements Shape {
4+
5+
@Override
6+
public void draw() {
7+
System.out.println("I'm a Text");
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.coderising.dp.decorator;
2+
3+
public interface Email {
4+
public String getContent();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.coderising.dp.decorator;
2+
3+
public class EmailImpl implements Email{
4+
private String content;
5+
6+
public EmailImpl(String content) {
7+
this.content = content;
8+
}
9+
10+
@Override
11+
public String getContent() {
12+
return content;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.coderising.dp.decorator;
2+
3+
public class EmailProxy implements Email{
4+
Email email;
5+
6+
@Override
7+
public String getContent() {
8+
String content=email.getContent()+"\n本邮件仅为个人观点,并不代表公司立场";
9+
System.out.println(content);
10+
content=Encrypt.SHA256(content);
11+
System.out.println(content);
12+
return content;
13+
}
14+
15+
public void setEmail(Email email) {
16+
this.email = email;
17+
}
18+
19+
public static void main(String[] args) {
20+
EmailImpl email=new EmailImpl("hello world!");
21+
EmailProxy proxy=new EmailProxy();
22+
EmailProxy proxy1=new EmailProxy();
23+
proxy.setEmail(email);
24+
// proxy.getContent();
25+
proxy1.setEmail(proxy);
26+
proxy1.getContent();
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.coderising.dp.decorator;
2+
3+
import java.security.MessageDigest;
4+
import java.security.NoSuchAlgorithmException;
5+
6+
public class Encrypt {
7+
/**
8+
* 传入文本内容,返回 SHA-256 串
9+
*
10+
* @param strText
11+
* @return
12+
*/
13+
public static String SHA256(final String strText)
14+
{
15+
return SHA(strText, "SHA-256");
16+
}
17+
18+
/**
19+
* 传入文本内容,返回 SHA-512 串
20+
*
21+
* @param strText
22+
* @return
23+
*/
24+
public static String SHA512(final String strText)
25+
{
26+
return SHA(strText, "SHA-512");
27+
}
28+
29+
/**
30+
* 字符串 SHA 加密
31+
*
32+
* @param strSourceText
33+
* @return
34+
*/
35+
private static String SHA(final String strText, final String strType)
36+
{
37+
// 返回值
38+
String strResult = null;
39+
40+
// 是否是有效字符串
41+
if (strText != null && strText.length() > 0)
42+
{
43+
try
44+
{
45+
// SHA 加密开始
46+
// 创建加密对象 并傳入加密類型
47+
MessageDigest messageDigest = MessageDigest.getInstance(strType);
48+
// 传入要加密的字符串
49+
messageDigest.update(strText.getBytes());
50+
// 得到 byte 類型结果
51+
byte byteBuffer[] = messageDigest.digest();
52+
53+
// 將 byte 轉換爲 string
54+
StringBuffer strHexString = new StringBuffer();
55+
// 遍歷 byte buffer
56+
for (int i = 0; i < byteBuffer.length; i++)
57+
{
58+
String hex = Integer.toHexString(0xff & byteBuffer[i]);
59+
if (hex.length() == 1)
60+
{
61+
strHexString.append('0');
62+
}
63+
strHexString.append(hex);
64+
}
65+
// 得到返回結果
66+
strResult = strHexString.toString();
67+
}
68+
catch (NoSuchAlgorithmException e)
69+
{
70+
e.printStackTrace();
71+
}
72+
}
73+
74+
return strResult;
75+
}
76+
}

0 commit comments

Comments
 (0)