博客
关于我
设计模式——工厂模式
阅读量: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 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
mysql 状态检查,备份,修复
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>
MySQL 用 limit 为什么会影响性能?有什么优化方案?
查看>>
MySQL 用户权限管理:授权、撤销、密码更新和用户删除(图文解析)
查看>>
mysql 用户管理和权限设置
查看>>
MySQL 的 varchar 水真的太深了!
查看>>
mysql 的GROUP_CONCAT函数的使用(group_by 如何显示分组之前的数据)
查看>>
MySQL 的instr函数
查看>>
MySQL 的mysql_secure_installation安全脚本执行过程介绍
查看>>
MySQL 的Rename Table语句
查看>>
MySQL 的全局锁、表锁和行锁
查看>>