代码精进之路-设计模式-(一)创建型模式

一、创建型模式(5个)

抽象工厂模式

//用于一个系列或风格的产品生成
public class Client {


    public static void main(String[] args) {
        System.out.println("来一套A牌茶具");
        FactoryTeaset teasetA = new FactoryTeasetA();
        Tea teaA = teasetA.getTea();
        Teacup teacupA = teasetA.getTeacup();
        Teapot teapotA = teasetA.getTeapot();
        teaA.show();
        teacupA.show();
        teapotA.show();


        System.out.println("来一套B牌茶具");
        FactoryTeaset teasetB = new FactoryTeasetB();
        Tea teaB = teasetB.getTea();
        Teacup teacupB = teasetB.getTeacup();
        Teapot teapotB = teasetB.getTeapot();
        teaB.show();
        teacupB.show();
        teapotB.show();
    }
}
public interface FactoryTeaset {
    //生产茶壶
    Teapot getTeapot();
    //生产茶杯
    Teacup getTeacup();
    //生产茶叶
    Tea getTea();
}
public interface Tea {
    void show();
}
public interface Teacup {
    void show();
}
public interface Teapot {
    void show();
}
//A品牌茶具工厂
public class FactoryTeasetA implements FactoryTeaset {
    @Override
    public Teapot getTeapot() {
        return new TeapotA();
    }
    @Override
    public Teacup getTeacup() {
        return new TeacupA();
    }
    @Override
    public Tea getTea() {
        return new TeaA();
    }
}
public class TeaA implements Tea {
    @Override
    public void show() {
        System.out.println("我是A牌茶叶");
    }
}
public class TeacupA implements Teacup {
    @Override
    public void show() {
        System.out.println("我是A牌茶杯");
    }
}
public class TeapotA implements Teapot {
    @Override
    public void show() {
        System.out.println("我是A牌茶壶");
    }
}
//B品牌茶具工厂
public class FactoryTeasetB implements FactoryTeaset {
    @Override
    public Teapot getTeapot() {
        return new TeapotB();
    }
    @Override
    public Teacup getTeacup() {
        return new TeacupB();
    }
    @Override
    public Tea getTea() {
        return new TeaB();
    }
}
public class TeaB implements Tea {
    @Override
    public void show() {
        System.out.println("我是B牌茶叶");
    }
}
public class TeacupB implements Teacup {
    @Override
    public void show() {
        System.out.println("我是B牌茶杯");
    }
}
public class TeapotB implements Teapot {
    @Override
    public void show() {
        System.out.println("我是B牌茶壶");
    }
}

构建者模式

//用于复杂对象的构建,定制化构建
public class Client {
    public static void main(String[] args) {
        HouseBuilder houseBuilder = new HouseBuilder();
        House house = houseBuilder
                .setDoors(new House.Door())
                .setWindows(new House.Window())
                .setWalls(new House.Wall())
                .setSwimmingPool(new House.SwimmingPool())
                .setRoof(new House.Roof())
                .setGarden(new House.Garden())
                .setGarage(new House.Garage())
                .build();


    }
}


@Getter
public class HouseBuilder {


    private House.Wall walls;
    private House.Door doors;
    private House.Window windows;
    private House.Roof roof;
    private House.Garage garage;
    private House.SwimmingPool swimmingPool;
    private House.Garden garden;


    public HouseBuilder setWalls(House.Wall walls) {
        this.walls = walls;
        return this;
    }
    public HouseBuilder setDoors(House.Door doors) {
        this.doors = doors;
        return this;
    }
    public HouseBuilder setWindows(House.Window windows) {
        this.windows = windows;
        return this;
    }
    public HouseBuilder setRoof(House.Roof roof) {
        this.roof = roof;
        return this;
    }
    public HouseBuilder setGarage(House.Garage garage) {
        this.garage = garage;
        return this;
    }
    public HouseBuilder setSwimmingPool(House.SwimmingPool swimmingPool) {
        this.swimmingPool = swimmingPool;
        return this;
    }
    public HouseBuilder setGarden(House.Garden garden) {
        this.garden = garden;
        return this;
    }


    public House build(){
        return new House(this);
    }


}


@Data
public class House {
    private Wall walls;
    private Door doors;
    private Window windows;
    private Roof roof;
    private Garage garage;
    private SwimmingPool swimmingPool;
    private Garden garden;


    public House(HouseBuilder builder){
        this.walls = builder.getWalls();
        this.doors = builder.getDoors();
        this.windows = builder.getWindows();
        this.roof = builder.getRoof();
        this.garage = builder.getGarage();
        this.swimmingPool = builder.getSwimmingPool();
        this.garden = builder.getGarden();
        //如果为null可以进行默认值设置
    }
    static class SwimmingPool {
    }
    static class Garden {
    }
    static class Wall {
    }
    static class Door {
    }
    static class Window {
    }
    static class Roof {
    }
    static class Garage {
    }
}

工厂方法模式

//只生产一种产品,生产不同的产品需要不同的工厂
public class Client {
    public static void main(String[] args) {
        IFactory factory;
        IProduct product;
        
        //华为工厂 生产华为播放器
        factory = new FactoryHuawei();
        product = factory.getProduct();
        product.sing();


        //小米工厂 生产小米播放器
        factory = new FactoryXiaomi();
        product = factory.getProduct();
        product.sing();
    }
}


public interface IFactory {
    IProduct getProduct();
}
public interface IProduct {
    /**
     * 这个产品可以唱歌
     */
    void sing();
}
public class FactoryHuawei implements IFactory {
    @Override
    public IProduct getProduct() {
        return new ProductHuawei();
    }
}
public class FactoryXiaomi implements IFactory {
    @Override
    public IProduct getProduct() {
        return new ProductXiaomi();
    }
}
public class ProductHuawei implements IProduct {
    @Override
    public void sing() {
        System.out.println("hi,我是华为音乐播放器,我正在唱歌。。。。。");
    }
}
public class ProductXiaomi implements IProduct {
    @Override
    public void sing() {
        System.out.println("hi,我是小米音乐播放器,我正在唱歌。。。。。");
    }
}

原型模式(克隆模式)

//浅克隆
public class Main {
    public static void main(String[] args) throws CloneNotSupportedException {
        Address address = new Address();
        address.location = "beijing";


        User user = new User();
        user.id = "id1";
        user.name = "name1";
        user.address = address;
        //-------------------------------------------------
        User clone =(User) user.clone();
        System.out.println("clone:"+clone.id);
        System.out.println("clone:"+clone.name);
        System.out.println("clone:"+clone.address.location);
    }
}


//需要实现Cloneable 接口才能使用 clone方法
class User implements Cloneable{
    String id;
    String name;
    Address address;
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}


class Address{
     String location;
}
//深克隆
public class Main {
    public static void main(String[] args) throws CloneNotSupportedException {
        Address address = new Address();
        address.location = "beijing";


        User user = new User();
        user.id = "id1";
        user.name = "name1";
        user.address = address;


        User clone =(User) user.clone();
        System.out.println("clone:"+clone.id);
        System.out.println("clone:"+clone.name);
        System.out.println("clone:"+clone.address.location);




    }
}


//需要实现Cloneable 接口才能使用 clone方法
class User implements Cloneable{
    String id;
    String name;
    Address address;
    @Override
    protected Object clone() throws CloneNotSupportedException {
        User clone = (User) super.clone();
        clone.address = (Address) address.clone();
        return clone;
    }
}


class Address implements Cloneable{
     String location;
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

单例模式

public class Singleton {
    private static Singleton singleton = new Singleton();
    //构造函数私有化
    private Singleton(){}
    public static Singleton getInstance(){
        return singleton;
    }
}

aad0121ba7393ab3a9c881e0863d6e1d.png

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吕哥架构

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值