java 23中 模式随时记

一:单例模式

   1 懒汉式

   2 饿汉式

二:简单工厂设计模式

 

/**
 * 服装类
 */
public abstract class Clothes {
    public abstract  void prepare();
    public abstract  void make();
    public abstract  void box();
}


public class ClothesFactory {
    public static Clothes creat(int type){
        Clothes clothes = null;
        if (type==1){
            clothes = new Trousers();
            clothes.prepare();
            clothes.make();
            clothes.box();
        }else if (type ==2){
            clothes = new Tshirt();
            clothes.prepare();
            clothes.make();
            clothes.box();
        }

        return clothes;
    }
}
public class Trousers extends Clothes {
    @Override
    public void prepare() {
        System.out.println("准备制作裤子的布料...");
    }

    @Override
    public void make() {
        System.out.println("开始制作裤子........");
    }

    @Override
    public void box() {
        System.out.println("裤子包装完毕.........");
    }
}


public class Tshirt extends Clothes {
    @Override
    public void prepare() {
        System.out.println("准备制作T的布料");
    }

    @Override
    public void make() {
        System.out.println("开始制作T恤");
    }

    @Override
    public void box() {
        System.out.println("打包T恤");
    }
}

public class Demo {
    public static void main(String[] args) {
        ClothesFactory factory = new ClothesFactory();
        Clothes clothes = factory.creat(2);

    }
}

 

posted @ 2019-08-22 20:33  lcj12121  阅读(114)  评论(0编辑  收藏  举报