zoukankan      html  css  js  c++  java
  • 设计模式之抽象工厂模式

    题目

    [实验任务一]:人与肤色
    使用抽象工厂模式,完成下述产品等级结构:

    类图

    Java

    AbsFacrtory抽象工厂类

    public interface AbsFactory {
        public Man produceMan();
        public Woman produceWoman();
    } 
    

    男人抽象类

    public interface Man {
        public void introduce();
    }
    

    女人抽象类

    package com.gazikel;
    
    public interface Woman {
        public void introduce();
    }
    

    工厂实现类

    BlackFactory

    package com.gazikel;
    
    import com.gazikel.black.BlackMan;
    import com.gazikel.black.BlackWoman;
    
    public class BlackFactory implements AbsFactory{
        @Override
        public Man produceMan() {
            return new BlackMan();
        }
    
        @Override
        public Woman produceWoman() {
            return new BlackWoman();
        }
    }
    
    

    WhiteFactory

    package com.gazikel;
    
    import com.gazikel.white.WhiteMan;
    import com.gazikel.white.WhiteWoman;
    
    public class WhiteFactory implements AbsFactory{
        @Override
        public Man produceMan() {
            return new WhiteMan();
        }
    
        @Override
        public Woman produceWoman() {
            return new WhiteWoman();
        }
    }
    

    YellowFactory

    package com.gazikel;
    
    import com.gazikel.yellow.YellowMan;
    import com.gazikel.yellow.YellowWoman;
    
    public class YellowFactory implements AbsFactory {
        @Override
        public Man produceMan() {
            return new YellowMan();
        }
    
        @Override
        public Woman produceWoman() {
            return new YellowWoman();
        }
    }
    

    实现接口

    黑人实现接口

    man

    package com.gazikel.black;
    
    import com.gazikel.Man;
    
    public class BlackMan implements Man {
        @Override
        public void introduce() {
            System.out.println("我是黑种男人");
        }
    }
    
    

    woman

    package com.gazikel.black;
    
    import com.gazikel.Woman;
    
    public class BlackWoman implements Woman {
        @Override
        public void introduce() {
            System.out.println("我是黑种女人");
        }
    }
    

    白人实现接口

    man

    package com.gazikel.white;
    
    import com.gazikel.Man;
    
    public class WhiteMan implements Man {
        @Override
        public void introduce() {
            System.out.println("我是白种男人");
        }
    }
    

    woman

    package com.gazikel.white;
    
    import com.gazikel.Man;
    
    public class WhiteMan implements Man {
        @Override
        public void introduce() {
            System.out.println("我是白种男人");
        }
    }
    

    黄人实现接口

    man

    package com.gazikel.yellow;
    
    import com.gazikel.Man;
    
    public class YellowMan implements Man {
        @Override
        public void introduce() {
            System.out.println("我是黄种男人");
        }
    }
    

    woman

    package com.gazikel.yellow;
    
    import com.gazikel.Woman;
    
    public class YellowWoman implements Woman {
        @Override
        public void introduce() {
            System.out.println("我是黄种女人");
        }
    }
    

    Client

    package com.gazikel;
    
    import com.gazikel.utils.XMLUtil;
    
    public class Client {
        private AbsFactory factory = null;
    
        public Client(String type){
            setFactory(type);
        }
    
        public void setFactory(String type) {
            if ("yellow".equals(type)) {
                factory = new YellowFactory();
            } else if ("black".equals(type)) {
                factory = new BlackFactory();
            } else if ("white".equals(type)) {
                factory = new WhiteFactory();
            }
        }
    
        public AbsFactory getFactory() {
            return factory;
        }
    }
    

    C++

    main.cpp

    #include <iostream>
    using namespace std;
    
    class Man {
    public:
    	virtual void introduce() = 0;
    };
    
    class WhiteMan : public Man {
    public:
    	virtual void introduce() {
    		cout << "我是白种男人!" << endl;
    	}
    };
    
    class YellowMan : public Man {
    public:
    	virtual void introduce() {
    		cout << "我是黄种男人!" << endl;
    	}
    };
    
    class BlackMan : public Man {
    public:
    	virtual void introduce() {
    		cout << "我是黑种男人!" << endl;
    	}
    };
    
    class Woman {
    public:
    	virtual void introduce() = 0;
    };
    
    class WhiteWoman : public Woman {
    public:
    	virtual void introduce() {
    		cout << "我是白种女人!" << endl;
    	}
    };
    
    class YellowWoman : public Woman {
    public:
    	virtual void introduce() {
    		cout << "我是黄种女人!" << endl;
    	}
    };
    
    class BlackWoman : public Woman {
    public:
    	virtual void introduce() {
    		cout << "我是黑种女人!" << endl;
    	}
    };
    
    class AbstractFactory {
    public:
    	virtual Man* ProduceMan() = 0;
    	virtual Woman* ProduceWoman() = 0;
    };
    
    class YellowFactory : public AbstractFactory {
    public:
    	virtual Man* ProduceMan() {
    		return new YellowMan;
    	}
    
    	virtual Woman* ProduceWoman() {
    		return new YellowWoman;
    	}
    };
    
    class WhiteFactory : public AbstractFactory {
    public:
    	virtual Man* ProduceMan() {
    		return new WhiteMan;
    	}
    
    	virtual Woman* ProduceWoman() {
    		return new WhiteWoman;
    	}
    };
    
    class BlackFactory : public AbstractFactory {
    public:
    	virtual Man* ProduceMan() {
    		return new BlackMan;
    	}
    	virtual Woman* ProduceWoman() {
    		return new BlackWoman;
    	}
    };
    
    int main(void) {
    	AbstractFactory * factory = NULL;
    	Man *man = NULL;
    	Woman *woman = NULL;
    
    	// 黄人
    	factory = new YellowFactory;
    	man = factory->ProduceMan();
    	woman = factory->ProduceWoman();
    	man->introduce();
    	woman->introduce();
    
    	delete woman;
    	delete man;
    	delete factory;
    
    	cout << "-----------------------" << endl;
    	// 白人
    	factory = new WhiteFactory;
    	man = factory->ProduceMan();
    	woman = factory->ProduceWoman();
    	man->introduce();
    	woman->introduce();
    	delete woman;
    	delete man;
    	delete factory;
    
    	cout << "-----------------------" << endl;
    	// 黑人
    	factory = new BlackFactory;
    	man = factory->ProduceMan();
    	woman = factory->ProduceWoman();
    	man->introduce();
    	woman->introduce();
    	delete woman;
    	delete man;
    	delete factory;
    
    }
    

  • 相关阅读:
    Node.js 学习笔记(二)
    微服务网关 zuul 替代者 gateway 网关路由
    flowable 6.6.0 绕过自带的登录限制(免登录)
    `flowable.common.app.idmurl` must be set (flowable 6.6.0)
    gateway 跨域问题解决方案
    ueditor 在springboot 打jar运行时 找不到图片附件路径问题
    springboot 打jar 包部署时 读取外部配置文件
    Navicat连接MySQL Server8.0版本时出现Client does not support authentication protocol requested by server;解决如下
    flowable 通过模型model ID部署流程
    springboot 配置日志输出
  • 原文地址:https://www.cnblogs.com/Gazikel/p/15596037.html
Copyright © 2011-2022 走看看