File tree 11 files changed +237
-1
lines changed
src/main/java/demo/invoice 11 files changed +237
-1
lines changed Original file line number Diff line number Diff line change 13
13
<artifactId >cglib</artifactId >
14
14
<version >2.2.2</version >
15
15
</dependency >
16
-
16
+ <dependency >
17
+ <groupId >org.projectlombok</groupId >
18
+ <artifactId >lombok</artifactId >
19
+ <version >1.18.8</version >
20
+ </dependency >
17
21
</dependencies >
18
22
19
23
<build >
Original file line number Diff line number Diff line change
1
+ package demo .invoice ;
2
+
3
+ import lombok .AllArgsConstructor ;
4
+ import lombok .Builder ;
5
+ import lombok .Data ;
6
+ import lombok .NoArgsConstructor ;
7
+
8
+ import java .math .BigDecimal ;
9
+
10
+ @ Data
11
+ @ Builder
12
+ @ AllArgsConstructor
13
+ @ NoArgsConstructor
14
+ public class Invoice {
15
+ private String invoiceNo ;
16
+ private String invoiceCode ;
17
+ private Integer invoiceType ;
18
+
19
+ private BigDecimal invoiceAmount ;
20
+ private BigDecimal taxAmount ;
21
+ private BigDecimal totalAmount ;
22
+
23
+ /**
24
+ * 火车票的专属字段,是放在主类里面,还是单独拆分一个新的类好?
25
+ */
26
+ private String trainNum ;
27
+ private String passengerName ;
28
+ /**
29
+ * 飞机票专属字段。
30
+ */
31
+ private String electronicTicketNum ;
32
+ private String customerIdentityNum ;
33
+
34
+
35
+ }
Original file line number Diff line number Diff line change
1
+ package demo .invoice ;
2
+
3
+ import lombok .AllArgsConstructor ;
4
+ import lombok .Builder ;
5
+ import lombok .Data ;
6
+ import lombok .NoArgsConstructor ;
7
+
8
+ import java .math .BigDecimal ;
9
+
10
+ @ Data
11
+ @ Builder
12
+ @ AllArgsConstructor
13
+ @ NoArgsConstructor
14
+ public class InvoiceAir {
15
+ private String invoiceNo ;
16
+ private String invoiceCode ;
17
+ private Integer invoiceType ;
18
+
19
+ private BigDecimal invoiceAmount ;
20
+ private BigDecimal taxAmount ;
21
+ private BigDecimal totalAmount ;
22
+
23
+ private String electronicTicketNum ;
24
+ private String customerIdentityNum ;
25
+ }
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
1
+ package demo .invoice ;
2
+
3
+ /**
4
+ *
5
+ */
6
+ public class InvoicePatternTest {
7
+ public static void main (String [] args ) {
8
+ String invoiceNo ="invoiceNo" ;
9
+ String invoiceCode ="invoiceCode" ;
10
+ Invoice invoice =Invoice .builder ().invoiceCode (invoiceCode ).invoiceNo (invoiceNo ).build ();
11
+ InvoiceOperation .updateInvoice (invoice );
12
+ }
13
+
14
+ }
Original file line number Diff line number Diff line change
1
+ package demo .invoice ;
2
+
3
+
4
+ /**
5
+ * 变化的部分,使用接口实现。相同的部分,使用继承复用。
6
+ */
7
+ public interface InvoiceService {
8
+ Invoice queryInvoice (Invoice invoice );
9
+
10
+ int saveInvoice ();
11
+
12
+ int updateInvoice ();
13
+
14
+ }
Original file line number Diff line number Diff line change
1
+ package demo .invoice ;
2
+
3
+ public class InvoiceServiceAir extends InvoiceOperation implements InvoiceService {
4
+
5
+ /**
6
+ * 此处的参数类型,应该是Invoice还是InvoiceAir?
7
+ * @param invoice
8
+ * @return
9
+ */
10
+ @ Override
11
+ public Invoice queryInvoice (Invoice invoice ) {
12
+ System .out .println ("查询飞机票" );
13
+ return null ;
14
+ }
15
+
16
+ @ Override
17
+ public int saveInvoice () {
18
+ System .out .println ("保存飞机票" );
19
+ return 0 ;
20
+ }
21
+
22
+ @ Override
23
+ public int updateInvoice () {
24
+ System .out .println ("更新飞机票" );
25
+ return 0 ;
26
+ }
27
+
28
+ }
Original file line number Diff line number Diff line change
1
+ package demo .invoice ;
2
+
3
+ public class InvoiceServiceOther extends InvoiceOperation implements InvoiceService {
4
+ @ Override
5
+ public Invoice queryInvoice (Invoice invoice ) {
6
+ System .out .println ("查询其他发票" );
7
+ return null ;
8
+ }
9
+
10
+ @ Override
11
+ public int saveInvoice () {
12
+ System .out .println ("保存其他发票" );
13
+ return 0 ;
14
+ }
15
+
16
+ @ Override
17
+ public int updateInvoice () {
18
+ System .out .println ("更新其他发票" );
19
+ return 0 ;
20
+ }
21
+
22
+ }
Original file line number Diff line number Diff line change
1
+ package demo .invoice ;
2
+
3
+ public class InvoiceServiceTrain extends InvoiceOperation implements InvoiceService {
4
+ @ Override
5
+ public Invoice queryInvoice (Invoice invoice ) {
6
+ System .out .println ("查询火车票" );
7
+ return null ;
8
+ }
9
+
10
+ @ Override
11
+ public int saveInvoice () {
12
+ System .out .println ("保存火车票" );
13
+ return 0 ;
14
+ }
15
+
16
+ @ Override
17
+ public int updateInvoice () {
18
+ System .out .println ("更新火车票" );
19
+ return 0 ;
20
+ }
21
+
22
+ }
Original file line number Diff line number Diff line change
1
+ package demo .invoice ;
2
+
3
+ import lombok .AllArgsConstructor ;
4
+ import lombok .Builder ;
5
+ import lombok .Data ;
6
+ import lombok .NoArgsConstructor ;
7
+
8
+ import java .math .BigDecimal ;
9
+
10
+
11
+ @ Data
12
+ @ Builder
13
+ @ AllArgsConstructor
14
+ @ NoArgsConstructor
15
+ public class InvoiceTrain {
16
+ private String invoiceNo ;
17
+ private String invoiceCode ;
18
+ private Integer invoiceType ;
19
+
20
+ private BigDecimal invoiceAmount ;
21
+ private BigDecimal taxAmount ;
22
+ private BigDecimal totalAmount ;
23
+
24
+ private String trainNum ;
25
+ private String passengerName ;
26
+ }
You can’t perform that action at this time.
0 commit comments