浅析java设计模式之建造者模式

建造者模式

描述:
建造者模式的主要目的是创建一个包含同一些种类的对象,根据其排列组合形成不同复杂对象时使用的创建型模式。

常用场景:
系统中一个复杂对象通过其他多个对象不同的组合所构成的时候,可以使用建造者模式。
比如点外卖时点套餐,套餐包含一份主食,一份小食,一杯饮料,此时就很适合使用建造者模式。

项目结构:
在这里插入图片描述
实现:
1.Clothing服装接口类,即复杂对象

/**
 * @author Carl
 * @version 1.0
 * @date 2020/8/14 14:24
 * @description
 **/
public interface Clothing {
    public String name();
    public String size();
    public float price();
    public String type();
}

2.Cloth上衣抽象类,实现Clothing服装接口类

/**
 * @author Carl
 * @version 1.0
 * @date 2020/8/14 14:28
 * @description
 **/
public abstract class Cloth implements Clothing {
    @Override
    public abstract String name();

    @Override
    public abstract String size();

    @Override
    public abstract float price();

    @Override
    public String type(){
        return "Cloth";
    }
}

3.衬衫类-具体服装,继承Cloth上衣抽象类

/**
 * @author Carl
 * @version 1.0
 * @date 2020/8/14 14:34
 * @description
 **/
public class Shirt extends Cloth {
    @Override
    public String name() {
        return "shirt";
    }

    @Override
    public String size() {
        return "35";
    }

    @Override
    public float price() {
        return 100;
    }
}

4.西装-具体服装,实现cloth抽象类

/**
 * @author Carl
 * @version 1.0
 * @date 2020/8/14 14:36
 * @description
 **/
public class WsCloth extends Cloth {
    @Override
    public String name() {
        return "western-style clothes";
    }

    @Override
    public String size() {
        return "37";
    }

    @Override
    public float price() {
        return 1000;
    }
}

5.裤子类,实现Clothing接口类

import com.yzj.dobetter.designPattern.builderPattern.Clothing;

/**
 * @author Carl
 * @version 1.0
 * @date 2020/8/14 14:29
 * @description
 **/
public abstract class Pants implements Clothing {
    @Override
    public abstract String name();

    @Override
    public abstract String size();

    @Override
    public abstract float price();

    @Override
    public String type(){
        return "Pants";
    }
}

6.休闲裤类-具体服装,继承Pants抽象类

/**
 * @author Carl
 * @version 1.0
 * @date 2020/8/14 14:38
 * @description
 **/
public class Casualpants extends Pants{
    @Override
    public String name() {
        return "Casual pants";
    }

    @Override
    public String size() {
        return "29";
    }

    @Override
    public float price() {
        return 100;
    }
}

7.西服裤子类-具体服装类,继承pants抽象类


/**
 * @author Carl
 * @version 1.0
 * @date 2020/8/14 14:39
 * @description
 **/
public class WsPants extends Pants {
    @Override
    public String name() {
        return "western-style pants";
    }

    @Override
    public String size() {
        return "30";
    }

    @Override
    public float price() {
        return 500;
    }
}

8.鞋子类,实现Clothing接口类

import com.yzj.dobetter.designPattern.builderPattern.Clothing;

/**
 * @author Carl
 * @version 1.0
 * @date 2020/8/14 14:29
 * @description
 **/
public abstract class Shoes implements Clothing {
    @Override
    public abstract String name();

    @Override
    public abstract String size();

    @Override
    public abstract float price();

    @Override
    public String type(){
        return "shoes";
    }
}

9.皮鞋类-具体服装,继承Shoes抽象类


/**
 * @author Carl
 * @version 1.0
 * @date 2020/8/14 14:41
 * @description
 **/
public class LeatherShoes extends Shoes {
    @Override
    public String name() {
        return "leather shoes";
    }

    @Override
    public String size() {
        return "42.5";
    }

    @Override
    public float price() {
        return 500;
    }
}

10.休闲鞋-具体服装,实现Shoes抽象类

/**
 * @author Carl
 * @version 1.0
 * @date 2020/8/14 14:40
 * @description
 **/
public class SportShoes extends Shoes {
    @Override
    public String name() {
        return "sport shoes";
    }

    @Override
    public String size() {
        return "42.5";
    }

    @Override
    public float price() {
        return 100;
    }
}

11.服装套装清单类

import java.util.ArrayList;
import java.util.List;

/**
 * @author Carl
 * @version 1.0
 * @date 2020/8/14 14:43
 * @description
 **/
public class Repertoire {

    public List<Clothing> clothingss = new ArrayList<>();

    public void addClothing(Clothing clothing){
        clothingss.add(clothing);
    }

    public float getPrice(){
        float price = 0;
        for(Clothing clothing:clothingss){
            price += clothing.price();
        }
        return price;
    }

    public void showDetails(){
        for(Clothing clothing:clothingss){
            System.out.print("Name:"+clothing.name());
            System.out.print("      type:"+clothing.type());
            System.out.print("      size:"+clothing.size());
            System.out.println("        price:"+clothing.price());
        }
    }
}

12.服装建造者类

import com.yzj.dobetter.designPattern.builderPattern.myCloth.Shirt;
import com.yzj.dobetter.designPattern.builderPattern.myCloth.WsCloth;
import com.yzj.dobetter.designPattern.builderPattern.myPants.Casualpants;
import com.yzj.dobetter.designPattern.builderPattern.myPants.WsPants;
import com.yzj.dobetter.designPattern.builderPattern.myShoes.LeatherShoes;
import com.yzj.dobetter.designPattern.builderPattern.myShoes.SportShoes;

/**
 * @author Carl
 * @version 1.0
 * @date 2020/8/14 14:51
 * @description
 **/
public class RepertoireBuilder {
    public static Repertoire getWsClothing(){
        Repertoire repe = new Repertoire();
        repe.addClothing(new WsCloth());
        repe.addClothing(new WsPants());
        repe.addClothing(new LeatherShoes());
        return repe;
    }

    public static Repertoire getCasupant(){
        Repertoire repe = new Repertoire();
        repe.addClothing(new Shirt());
        repe.addClothing(new Casualpants());
        repe.addClothing(new SportShoes());
        return repe;
    }
}

13.测试类

/**
 * @author Carl
 * @version 1.0
 * @date 2020/8/14 14:55
 * @description
 **/
public class Domain {
    public static void main(String[] args) {
        Repertoire repertoire = RepertoireBuilder.getCasupant();
        System.out.println("休闲套装清单");
        repertoire.showDetails();
        System.out.println("总价:"+repertoire.getPrice());

        Repertoire WSRepertoire = RepertoireBuilder.getWsClothing();
        System.out.println("西服套装清单");
        WSRepertoire.showDetails();
        System.out.println("总价:"+WSRepertoire.getPrice());
    }
}

14.测试结果

休闲套装清单
Name:shirt      type:Cloth      size:35        price:100.0
Name:Casual pants      type:Pants      size:29        price:100.0
Name:sport shoes      type:shoes      size:42.5        price:100.0
总价:300.0
西服套装清单
Name:western-style clothes      type:Cloth      size:37        price:1000.0
Name:western-style pants      type:Pants      size:30        price:500.0
Name:leather shoes      type:shoes      size:42.5        price:500.0
总价:2000.0

说明:
1.创建一个Clothing服装接口类,所有的Cloth衣服,Pants裤子,Shoes鞋子都是这个接口类的子类。
2.创建Cloth上衣,Pants裤子,Shoes鞋子的抽象类,每个抽象类相当于一个维度的,相当于一个服装套装,由上衣,裤子, 鞋子三个维度构成,三个维度的具体商品可以自由配置,以此来组成休闲套装,西服套装等。
3.创建Cloth上衣抽象类的实现类,Shirt衬衫类和WsCloth西服类。
4.创建Pants裤子抽象类的实现类,CasualPants休闲裤类和WsPants西裤类。
5.创建Shoes鞋子类的实现类,SportShoes运动鞋类和LeatherShoes皮鞋类。
6.创建Repertoire 服装套装类,一个套装包含三个维度的服饰
7.创建RepertoireBuilder服装套装建造者类,通过两种对上衣下装鞋子的组合,组成休闲装和西服套装两种服装套装
8.创建测试类,使用RepertoireBuilder的getCasupant()和getWsClothing()两种方法分别获得休闲装和西服套装,并输出两种服装的具体信息

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值