下面用两个例子来说明:
螺丝螺母的例子
public interface Screw {
public void useScrew();
}
public class Screw6 implements Screw{
@Override
public void useScrew() {
System.out.println("使用6号螺丝");
}
}
public class Screw8 implements Screw{
@Override
public void useScrew() {
System.out.println("使用8号螺丝");
}
}
public interface Nut {
public void useNut();
}
public class Nut6 implements Nut{
@Override
public void useNut() {
System.out.println("使用6号螺母");
}
}
public class Nut8 implements Nut{
@Override
public void useNut() {
System.out.println("使用8号螺母");
}
}
public interface Product {
public Screw createScrew();
public Nut createNut();
}
public class Factory6 implements Product{
@Override
public Screw createScrew() {
return new Screw6();
}
@Override
public Nut createNut() {
return new Nut6();
}
}
public class Factory8 implements Product{
@Override
public Screw createScrew() {
return new Screw8();
}
@Override
public Nut createNut() {
return new Nut8();
}
}
public class Store {
public static void main(String[] args) {
useProduct(new Factory6());
useProduct(new Factory8());
}
public static void useProduct(Product product){
Screw screw = product.createScrew();
screw.useScrew();
Nut nut = product.createNut();
nut.useNut();
}
}
家用电器的例子
public interface BingXiang {
public void useBX();
}
public class HaierBX implements BingXiang{
@Override
public void useBX() {
// TODO Auto-generated method stub
System.out.println("我正在使用海尔冰箱。");
}
}
public class RongsBX implements BingXiang{
@Override
public void useBX() {
// TODO Auto-generated method stub
System.out.println("我正在使用容声冰箱。");
}
}
public class XiaomBX implements BingXiang{
@Override
public void useBX() {
System.out.println("我正在使用小米冰箱。");
}
}
public interface TV {
public void watchTV();
}
public class HaierTV implements TV{
@Override
public void watchTV() {
// TODO Auto-generated method stub
System.out.println("正在用海尔电视观看节目");
}
}
public class RongsTV implements TV {
@Override
public void watchTV() {
// TODO Auto-generated method stub
System.out.println("正在用容声电视观看节目");
}
}
public class XiaomTV implements TV{
@Override
public void watchTV() {
// TODO Auto-generated method stub
System.out.println("正在用小米电视观看节目");
}
}
/**
* TODO 家电接口,主要生产家电
*
* @author ZhangPeng
* @date 2021年6月2日 下午2:52:09
*/
public interface JiaDianProduct {
public BingXiang createBX();
public TV createTV();
}
public class HaierFactory implements JiaDianProduct{
@Override
public BingXiang createBX() {
// TODO Auto-generated method stub
return new HaierBX();
}
@Override
public TV createTV() {
// TODO Auto-generated method stub
return new HaierTV();
}
}
public class RongsFactory implements JiaDianProduct{
@Override
public BingXiang createBX() {
// TODO Auto-generated method stub
return new RongsBX();
}
@Override
public TV createTV() {
// TODO Auto-generated method stub
return new RongsTV();
}
}
public class XiaomFactory implements JiaDianProduct{
@Override
public BingXiang createBX() {
// TODO Auto-generated method stub
return new XiaomBX();
}
@Override
public TV createTV() {
// TODO Auto-generated method stub
return new XiaomTV();
}
}
public class Customer {
public static void main(String[] args) {
// buyJiaDian(new XiaomFactory());
buyJiaDian(new HaierFactory());
}
public static void buyJiaDian(JiaDianProduct product){
BingXiang bx = product.createBX();
bx.useBX();
TV tv = product.createTV();
tv.watchTV();
}
}
针对此图
比如说当多一个产品等级的时候,例如京东,只需要建京东工厂以及创建对应的产品即可,但是当多一个产品簇的时候,例如烘干机,那么每个产品单位都需要新加方法。