您的位置:首页 > 编程语言 > Java开发

Java 设计模式(三)工厂模式

2018-02-07 11:50 302 查看
一、简介

工厂模式细分下有抽象工厂、抽象方法、简单工厂等等(这个不算一种设计模式,但也经常使用),它们的共同点就在于统一管理对象的创建,这样在修改时只需修改工厂类或工厂方法即可。

二、代码示例

要求(不要在意与上一节要求相同):

商家做活动要求有多种活动方案作为备选使用:

1、满30元减5元,满50元减10元,满100元减30元

2、满30元打9.5折,满50元打9折,满100元打8折

3、…..更多活动待扩展

商品接口与方案接口及方案类型常数:

//商品接口
public interface IProduct {
int getPrice();
String getDescription();
}
//方案接口
public interface IScheme {
//对价格进行处理
int processPrice(int price);
//对商品描述添加活动标记
String processDescription(String desc);
}
//方案类别常数
public enum SchemeType {
DISCOUNT,DECREACE;
}


商品实现类(仅包含构造函数和get方法):

public class ProductImpl implements IProduct{
private String description;
private int price;
public ProductImpl(String description,int price) {
this.description = description;
this.price = price;
}
public String getDescription(){
return this.description;
}
public int getPrice(){
return this.price;
}
}


满减活动方案:

//活动方案:满10减5元,满50减10元,满100减30元
public class DecreaseScheme implements IScheme {
@Override
//处理价格
public int processPrice(int price) {
if(10<=price&&price<50){
price -= 5;
}else if(price<100){
price -= 10;
}else{
price -= 30;
}
return price;
}
@Override
//添加活动标记
public String processDescription(String desc) {
return desc+"--满减活动商品";
}
}


折扣活动方案:

//活动方案:满10元打9.5折,满50元打9折,满100元打8折
public class DiscountScheme implements IScheme{
@Override
//处理价格
public int processPrice(int price) {
if(10<=price&&price<50){
price *= 0.95;
}else if(price<100){
price *= 0.9;
}else{
price *= 0.8;
}
return price;
}
@Override
//添加活动标记
public String processDescription(String desc) {
return desc+"--折扣活动商品";
}
}


方案工厂:

public class SchemeFactory {
//根据方案类别类型返回不同的方案
public static IScheme newScheme(SchemeType type){
switch (type) {
case DISCOUNT:
return new DiscountScheme();
case DECREACE:
return new DecreaseScheme();
default:
return null;
}
}
}


活动商品类(这里使用装饰模式装饰普通商品)

//活动商品类
public class ActiveProduct implements IProduct{
private IScheme scheme;
private  IProduct product;
public ActiveProduct(IProduct product,SchemeType type) {
this.product = product;
this.scheme = SchemeFactory.newScheme(type);
}
//获得处理后的价格
public int getPrice() {
return scheme.processPrice(product.getPrice());
}
//获得处理后的商品描述
public String getDescription(){
return scheme.processDescription(product.getDescription());
}
}


场景模拟:

public class FactoryTest {
//获取随机数的工具
static Random random = new Random();
public static void main(String[] args) {
IProduct product;
//50个测试用例
for(int i=0;i<50;i++){
//初始化普通商品
product = new ProductImpl(getRandomDesc(), getRandomPrice());
//通过活动方案进行商品包装
product = new ActiveProduct(product,getRandomType());
System.out.println("----------------------");
System.out.println("desc:"+product.getDescription());
System.out.println("price:"+product.getPrice());
}
}
//获得随机商品名
static String getRandomDesc(){
return (char)(random.nextInt(26)+65)+"";
}
//获得随机商品价格
static int getRandomPrice(){
return random.nextInt(150);
}
//获得随机活动类型
static SchemeType getRandomType(){
return random.nextInt(10)>5?SchemeType.DECREACE:SchemeType.DISCOUNT;
}
}


结果打印:

----------------------
desc:R--折扣活动商品
price:55
----------------------
desc:R--满减活动商品
price:64
----------------------
desc:Z--折扣活动商品
price:61
----------------------
desc:L--折扣活动商品
price:47
----------------------
desc:C--满减活动商品
price:67


分析:

整个示例通过方案工厂产生不同的方案,由活动商品类对普通商品进行包装,以实现活动方案的灵活切换。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  设计模式 学习 笔记