博客
关于我
设计模式——工厂模式
阅读量:86 次
发布时间:2019-02-25

本文共 3008 字,大约阅读时间需要 10 分钟。

??????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????new??????????????????????????????????????????????????????????????????

?????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????

??????????Java???????

// ???Clothespackage features;public abstract class Clothes {    private int wristband = 2;    private int neckline = 1;        public void hold() {        System.out.println("??????????");    }        public int getWristband() {        return wristband;    }    public void setWristband(int wristband) {        this.wristband = wristband;    }        public int getNeckline() {        return neckline;    }    public void setNeckline(int neckline) {        this.neckline = neckline;    }}
// ?????AutumnClothpackage special;import features.Clothes;public class AutumnCloth extends Clothes {    @Override    public void hold() {        System.out.println("??????????");    }}
// ?????SpringClothpackage special;import features.Clothes;public class SpringCloth extends Clothes {    @Override    public void hold() {        System.out.println("????????????");    }}
// ?????SummerClothpackage special;import features.Clothes;public class SummerCloth extends Clothes {    @Override    public void hold() {        System.out.println("???????????");    }}
// ???ClothFactorypackage factory;import features.Clothes;import special.AutumnCloth;import special.SpringCloth;import special.SummerCloth;public class ClothFactory {    public static Clothes createClothes(String type) throws Exception {        Clothes clothes = null;        switch (type) {            case "spring":                clothes = new SpringCloth();                break;            case "summer":                clothes = new SummerCloth();                break;            case "autumn":                clothes = new AutumnCloth();                break;            default:                throw new Exception("???????????");        }        return clothes;    }}
// ?????package client;import features.Clothes;import factory.ClothFactory;public class Client {    public static void main(String[] args) throws Exception {        System.out.println("?????????");        Clothes spring = ClothFactory.createClothes("spring");        spring.hold();        System.out.println("********************");        System.out.println("?????????");        Clothes autum = ClothFactory.createClothes("autumn");        autum.hold();        System.out.println("********************");        System.out.println("?????????");        Clothes summer = ClothFactory.createClothes("summer");        summer.hold();        System.out.println("********************");    }}

?????????????????????????????????????????????????????????????????????????????????????????????????

转载地址:http://mso.baihongyu.com/

你可能感兴趣的文章
MySQL 精选 60 道面试题(含答案)
查看>>
mysql 索引
查看>>
MySQL 索引失效的 15 种场景!
查看>>
MySQL 索引深入解析及优化策略
查看>>
MySQL 索引的面试题总结
查看>>
mysql 索引类型以及创建
查看>>
MySQL 索引连环问题,你能答对几个?
查看>>
Mysql 索引问题集锦
查看>>
Mysql 纵表转换为横表
查看>>
mysql 编译安装 window篇
查看>>
mysql 网络目录_联机目录数据库
查看>>
MySQL 聚簇索引&&二级索引&&辅助索引
查看>>
Mysql 脏页 脏读 脏数据
查看>>
mysql 自增id和UUID做主键性能分析,及最优方案
查看>>
Mysql 自定义函数
查看>>
mysql 行转列 列转行
查看>>
Mysql 表分区
查看>>
mysql 表的操作
查看>>
mysql 视图,视图更新删除
查看>>
MySQL 触发器
查看>>