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

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

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

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

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

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

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

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

// ???Clothes
package 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;
}
}
// ?????AutumnCloth
package special;
import features.Clothes;
public class AutumnCloth extends Clothes {
@Override
public void hold() {
System.out.println("??????????");
}
}
// ?????SpringCloth
package special;
import features.Clothes;
public class SpringCloth extends Clothes {
@Override
public void hold() {
System.out.println("????????????");
}
}
// ?????SummerCloth
package special;
import features.Clothes;
public class SummerCloth extends Clothes {
@Override
public void hold() {
System.out.println("???????????");
}
}
// ???ClothFactory
package 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/

你可能感兴趣的文章
Objective-C实现一个Pangram字符串至少包含一次所有字母算法(附完整源码)
查看>>
Objective-C实现一个stack算法(附完整源码)
查看>>
Objective-C实现一个通用的堆算法(附完整源码)
查看>>
Objective-C实现一分钟倒计时(附完整源码)
查看>>
Objective-C实现一阶高斯滤波(附完整源码)
查看>>
Objective-C实现万年历(附完整源码)
查看>>
Objective-C实现三次样条曲线(附完整源码)
查看>>
Objective-C实现三维空间点到直线的距离(附完整源码)
查看>>
Objective-C实现三维空间点到直线的距离(附完整源码)
查看>>
Objective-C实现三重缓冲区(附完整源码)
查看>>
Objective-C实现上传文件到FTP服务器(附完整源码)
查看>>
Objective-C实现下载文件(附完整源码)
查看>>
Objective-C实现不重复字符的最长子串算法(附完整源码)
查看>>
Objective-C实现两个字符串由相同的字母组成但排列方式不同(字符串字谜)算法(附完整源码)
查看>>
Objective-C实现两个日期之间的天数(附完整源码)
查看>>
Objective-C实现两个栈实现队列算法(附完整源码)
查看>>
Objective-C实现两个队列实现栈算法(附完整源码)
查看>>
Objective-C实现两数之和问题(附完整源码)
查看>>
Objective-C实现中介者模式(附完整源码)
查看>>
Objective-C实现中值滤波(附完整源码)
查看>>