File tree 8 files changed +68
-28
lines changed
src/main/java/demo/invoice 8 files changed +68
-28
lines changed Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ public class Invoice {
21
21
private BigDecimal totalAmount ;
22
22
23
23
/**
24
- * 火车票的专属字段
24
+ * 火车票的专属字段,是放在主类里面,还是单独拆分一个新的类好?
25
25
*/
26
26
private String trainNum ;
27
27
private String passengerName ;
@@ -31,4 +31,5 @@ public class Invoice {
31
31
private String electronicTicketNum ;
32
32
private String customerIdentityNum ;
33
33
34
+
34
35
}
Original file line number Diff line number Diff line change
1
+ package demo .invoice ;
2
+
3
+ public class InvoiceConstant {
4
+
5
+ public static final int INVOICE_TRAIN =8 ;
6
+
7
+ public static final int INVOICE_AIR =9 ;
8
+
9
+ }
Original file line number Diff line number Diff line change
1
+ package demo .invoice ;
2
+
3
+ public class InvoiceOperation {
4
+
5
+ /**
6
+ * 计算发票类型。
7
+ * 根据发票代码、发票号码计算得到发票类型。
8
+ * @param invoiceNo
9
+ * @param invoiceCode
10
+ * @return
11
+ */
12
+ public static int calculateInvoiceType (String invoiceNo ,String invoiceCode ){
13
+ int invoiceType =(int )(Math .random ()*2 +8 );
14
+ System .out .println ("发票类型为:" +invoiceType );
15
+ return invoiceType ;
16
+ }
17
+
18
+ /**
19
+ * 根据不同的发票类型生成不同的发票服务
20
+ * @param invoice
21
+ */
22
+ public static void updateInvoice (Invoice invoice ){
23
+ String invoiceNo =invoice .getInvoiceNo ();
24
+ String invoiceCode =invoice .getInvoiceCode ();
25
+ int invoiceType =calculateInvoiceType (invoiceNo ,invoiceCode );
26
+ InvoiceService invoiceService ;
27
+ if (invoiceType ==InvoiceConstant .INVOICE_TRAIN ) {
28
+ invoiceService =new InvoiceServiceAir ();
29
+ }else if (invoiceType ==InvoiceConstant .INVOICE_AIR ){
30
+ invoiceService =new InvoiceServiceTrain ();
31
+ }else {
32
+ invoiceService =new InvoiceServiceOther ();
33
+ }
34
+ invoiceService .updateInvoice ();
35
+ }
36
+
37
+ }
Original file line number Diff line number Diff line change 5
5
*/
6
6
public class InvoicePatternTest {
7
7
public static void main (String [] args ) {
8
- int invoiceType =9 ;
9
- updateInvoice (invoiceType );
10
-
11
- }
12
-
13
- public static void updateInvoice (int invoiceType ){
14
- Invoice invoice =new Invoice ();
15
- //根据不同的发票类型生成不同的发票服务
16
- InvoiceService invoiceService =null ;
17
- if (invoiceType ==8 ) {
18
- invoiceService =new InvoiceServiceAir ();
19
- }else if (invoiceType ==9 ){
20
- invoiceService =new InvoiceServiceTrain ();
21
- }else {
22
- invoiceService =new InvoiceServiceOther ();
23
- }
24
- invoiceService .updateInvoice ();
25
-
8
+ String invoiceNo ="invoiceNo" ;
9
+ String invoiceCode ="invoiceCode" ;
10
+ Invoice invoice =Invoice .builder ().invoiceCode (invoiceCode ).invoiceNo (invoiceNo ).build ();
11
+ InvoiceOperation .updateInvoice (invoice );
26
12
}
27
13
28
14
}
Original file line number Diff line number Diff line change 1
1
package demo .invoice ;
2
2
3
- public interface InvoiceService {
4
3
5
- Invoice queryInvoice (String invoiceNo ,String invoiceCode );
4
+ /**
5
+ * 变化的部分,使用接口实现。相同的部分,使用继承复用。
6
+ */
7
+ public interface InvoiceService {
8
+ Invoice queryInvoice (Invoice invoice );
6
9
7
10
int saveInvoice ();
8
11
Original file line number Diff line number Diff line change 1
1
package demo .invoice ;
2
2
3
- public class InvoiceServiceAir implements InvoiceService {
4
-
3
+ public class InvoiceServiceAir extends InvoiceOperation implements InvoiceService {
5
4
5
+ /**
6
+ * 此处的参数类型,应该是Invoice还是InvoiceAir?
7
+ * @param invoice
8
+ * @return
9
+ */
6
10
@ Override
7
- public Invoice queryInvoice (String invoiceNo , String invoiceCode ) {
11
+ public Invoice queryInvoice (Invoice invoice ) {
8
12
System .out .println ("查询飞机票" );
9
13
return null ;
10
14
}
Original file line number Diff line number Diff line change 1
1
package demo .invoice ;
2
2
3
- public class InvoiceServiceOther implements InvoiceService {
3
+ public class InvoiceServiceOther extends InvoiceOperation implements InvoiceService {
4
4
@ Override
5
- public Invoice queryInvoice (String invoiceNo , String invoiceCode ) {
5
+ public Invoice queryInvoice (Invoice invoice ) {
6
6
System .out .println ("查询其他发票" );
7
7
return null ;
8
8
}
Original file line number Diff line number Diff line change 1
1
package demo .invoice ;
2
2
3
- public class InvoiceServiceTrain implements InvoiceService {
3
+ public class InvoiceServiceTrain extends InvoiceOperation implements InvoiceService {
4
4
@ Override
5
- public Invoice queryInvoice (String invoiceNo , String invoiceCode ) {
5
+ public Invoice queryInvoice (Invoice invoice ) {
6
6
System .out .println ("查询火车票" );
7
7
return null ;
8
8
}
You can’t perform that action at this time.
0 commit comments