博客
关于我
设计模式——工厂模式
阅读量: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 InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>
mysql insert update 同时执行_MySQL进阶三板斧(三)看清“触发器 (Trigger)”的真实面目...
查看>>
mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
查看>>
Mysql join原理
查看>>
MySQL Join算法与调优白皮书(二)
查看>>
Mysql order by与limit混用陷阱
查看>>
Mysql order by与limit混用陷阱
查看>>
mysql order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>