package
com.zj.books.strategy;
public
abstract
class
DiscountStrategy {
protected
double
_price
= 0.0;
public
DiscountStrategy(
double
price) {
_price
= price;
}
abstract
public
double
calculateDiscount();
……
}
|
package
com.zj.books.strategy;
public
class
NoDiscountStrategy
extends
DiscountStrategy {
public
NoDiscountStrategy(
double
price) {
super
(price);
}
public
double
calculateDiscount() {
return
0.0;
}
}
|
package
com.zj.books.strategy;
public
class
FlatRateStrategy
extends
DiscountStrategy {
private
double
_discount
= 0.0;
public
FlatRateStrategy(
double
price,
double
discount) {
super
(price);
_discount
= discount;
}
public
double
calculateDiscount() {
return
_discount
;
}
……
}
|
package
com.zj.books.strategy;
public
class
PercentageStrategy
extends
DiscountStrategy {
private
double
_percent
= 1.0;
public
PercentageStrategy(
double
price,
double
percent) {
super
(price);
if
(percent > 1.0)
percent = 1.0;
_percent
= percent;
}
public
double
calculateDiscount() {
return
_price
*
_percent
;
}
……
}
|
package
com.zj.books;
import
com.zj.books.strategy.DiscountStrategy;
public
abstract
class
Book {
protected
String
_name
;
protected
int
_typeCode
;
protected
double
_price
;
protected
DiscountStrategy
_strategy
;
public
Book(String name,
int
bookType,
double
price) {
_name
= name;
_typeCode
= bookType;
_price
= price;
}
public
double
getDiscount() {
return
_strategy
.calculateDiscount();
}
……
}
|
package
com.zj.purchase;
import
com.zj.books.Book;
public
interface
Publisher {
Book bookFactory(String name,
double
price);
}
|
package
com.zj.purchase;
import
com.zj.books.Book;
import
com.zj.books.ComputerBook;
import
com.zj.books.strategy.FlatRateStrategy;
public
class
ComputerBookPublish
implements
Publisher{
private
double
_discount
=0.0;
public
ComputerBookPublish(
double
discount){
_discount
=discount;
}
public
Book bookFactory(String name,
double
price) {
Book book=
new
ComputerBook(name,price);
book.setStrategy(
new
FlatRateStrategy(price,
_discount
));
return
book;
}
……
}
|
package
com.zj.purchase;
import
com.zj.books.Book;
import
com.zj.books.ManagementBook;
import
com.zj.books.strategy.PercentageStrategy;
public
class
ManagementBookPublish
implements
Publisher{
private
double
_percent
=1.0;
public
ManagementBookPublish(
double
percent){
_percent
=percent;
}
public
Book bookFactory(String name,
double
price) {
Book book=
new
ManagementBook(name,price);
book.setStrategy(
new
PercentageStrategy(price,
_percent
));
return
book;
}
……
}
|
package
com.zj.purchase;
import
com.zj.books.Book;
import
com.zj.books.NewReleaseBook;
import
com.zj.books.strategy.NoDiscountStrategy;
public
class
NewReleaseBookPublisher
implements
Publisher{
public
Book bookFactory(String name,
double
price) {
Book book=
new
NewReleaseBook(name,price);
book.setStrategy(
new
NoDiscountStrategy(price));
return
book;
}
}
|
package
com.zj.order;
public
interface
Order {
void
buy(
int
copies,String name,
int
price,
int
type);
double
originalPay();
double
actualPay();
void
setActualPay(
double
pay);
double
discount();
void
setDiscount(
double
discount);
int
getCopies();
void
printPayList();
void
addPolicy();
}
|
package
com.zj.order;
import
java.util.HashMap;
import
java.util.Map;
import
com.zj.books.Book;
import
com.zj.books.BookType;
import
com.zj.purchase.ComputerBookPublish;
import
com.zj.purchase.ManagementBookPublish;
import
com.zj.purchase.NewReleaseBookPublisher;
public
class
PayOrder
implements
Order {
private
Map<String, Integer>
payList
=
new
HashMap<String, Integer>();
private
double
_pay
= 0.0;
private
double
_discount
= 0.0;
private
int
_copies
= 0;
private
double
_discountPolicy
= 0.0;
private
double
_percentagePolicy
= 1.0;
public
PayOrder(
double
discountPolicy,
double
percentagePolicy) {
_discountPolicy
= discountPolicy;
_percentagePolicy
= percentagePolicy;
}
public
void
buy(
int
copies, String name,
int
price,
int
type) {
Book book =
null
;
switch
(type) {
case
BookType.
NEW_RELEASE
:
book =
new
NewReleaseBookPublisher().bookFactory(name, price);
break
;
case
BookType.
COMPUTER
:
book =
new
ComputerBookPublish(
_discountPolicy
).bookFactory(name,
price);
break
;
case
BookType.
MANAGEMENT
:
book =
new
ManagementBookPublish(
_percentagePolicy
).bookFactory(
name, price);
break
;
default
:
throw
new
RuntimeException(
"Type not found."
);
}
_copies
+= copies;
payList
.put(book.getName(), copies);
_pay
+= copies * book.getPrice();
_discount
+= copies * book.getDiscount();
}
public
double
originalPay() {
return
_pay
;
}
public
double
actualPay() {
return
_pay
-
_discount
;
}
public
void
setActualPay(
double
pay) {
_pay
= pay;
}
public
double
discount() {
return
_discount
;
}
public
void
setDiscount(
double
discount) {
_discount
= discount;
}
public
int
getCopies() {
return
_copies
;
}
public
void
printPayList() {
System.out.println(toString());
}
public
void
addPolicy() {
}
public
String toString() {
return
payList
.toString();
}
}
|
package
com.zj.order.decorator;
import
com.zj.order.Order;
public
class
OrderDecorator
implements
Order {
protected
Order
_order
;
public
OrderDecorator(Order order) {
_order
= order;
}
public
double
actualPay() {
return
_order
.actualPay();
}
public
void
setActualPay(
double
pay) {
_order
.setActualPay(pay);
}
public
void
buy(
int
copies, String name,
int
price,
int
type) {
_order
.buy(copies, name, price, type);
}
public
double
discount() {
return
_order
.discount();
}
public
void
setDiscount(
double
discount) {
_order
.setDiscount(discount);
}
public
double
originalPay() {
return
_order
.originalPay();
}
public
int
getCopies() {
return
_order
.getCopies();
}
public
void
printPayList(){
_order
.printPayList();
}
public
void
addPolicy(){
_order
.addPolicy();
}
}
|
package
com.zj.order.decorator;
import
com.zj.order.Order;
public
class
CopyDecorator
extends
OrderDecorator {
public
CopyDecorator(Order order) {
super
(order);
}
public
void
addPolicy() {
if
(getCopies() > 5)
setDiscount(discount() + 20);
super
.
_order
.addPolicy();
}
}
|
package
com.zj.order.decorator;
import
com.zj.order.Order;
public
class
PayDecorator
extends
OrderDecorator {
public
PayDecorator(Order order) {
super
(order);
}
public
void
addPolicy() {
if
(discount() > 50)
setDiscount(50);
super
.
_order
.addPolicy();
}
}
|
package
com.zj.client;
import
com.zj.books.BookType;
import
com.zj.order.Order;
import
com.zj.order.PayOrder;
import
com.zj.order.decorator.CopyDecorator;
import
com.zj.order.decorator.PayDecorator;
public
class
Client {
public
static
void
main(String[] args) {
Order order =
new
PayOrder(10, 0.1);
order.buy(1,
"Java"
, 40, BookType.
COMPUTER
);
order.buy(1,
"C++"
, 60, BookType.
COMPUTER
);
order.buy(1,
"Design Pattern"
, 100, BookType.
COMPUTER
);
order.buy(1,
"Manager"
, 60, BookType.
MANAGEMENT
);
order.buy(1,
" Apo "
, 20, BookType.
NEW_RELEASE
);
order.buy(1,
"droAq"
, 20, BookType.
NEW_RELEASE
);
order.printPayList();
System.
out
.println(
"==========="
);
System.
out
.println(
"original\t"
+order.originalPay());
System.
out
.println(
"discount\t"
+order.discount());
System.
out
.println(
"actual\t\t"
+order.actualPay());
System.
out
.println(
"==========="
);
order=
new
CopyDecorator(
new
PayDecorator(order));
order.addPolicy();
System.
out
.println(
"original\t"
+order.originalPay());
System.
out
.println(
"discount\t"
+order.discount());
System.
out
.println(
"actual\t\t"
+order.actualPay());
}
}
|