博客
关于我
设计模式——工厂模式
阅读量: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 中的all,5分钟了解MySQL5.7中union all用法的黑科技
查看>>
MySQL 中的外键检查设置:SET FOREIGN_KEY_CHECKS = 1
查看>>
Mysql 中的日期时间字符串查询
查看>>
mysql 中索引的问题
查看>>
MySQL 中锁的面试题总结
查看>>
MySQL 中随机抽样:order by rand limit 的替代方案
查看>>
MySQL 为什么需要两阶段提交?
查看>>
mysql 为某个字段的值加前缀、去掉前缀
查看>>
mysql 主从
查看>>
mysql 主从 lock_mysql 主从同步权限mysql 行锁的实现
查看>>
mysql 主从互备份_mysql互为主从实战设置详解及自动化备份(Centos7.2)
查看>>
mysql 主从关系切换
查看>>
MYSQL 主从同步文档的大坑
查看>>
mysql 主键重复则覆盖_数据库主键不能重复
查看>>
Mysql 事务知识点与优化建议
查看>>
Mysql 优化 or
查看>>
mysql 优化器 key_mysql – 选择*和查询优化器
查看>>
MySQL 优化:Explain 执行计划详解
查看>>
Mysql 会导致锁表的语法
查看>>
mysql 使用sql文件恢复数据库
查看>>