设计模式之抽象工厂模式
题目
[实验任务一]:人与肤色
使用抽象工厂模式,完成下述产品等级结构:
类图
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;
}