博客
关于我
设计模式——工厂模式
阅读量: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 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>