定义一个用于创建对象的接口,让子类决定实例化哪个类(也可以叫做静态工厂模式)
产品
public abstract class Product{
public abstract void a();
public abstract void b();
}
产品实现类
/**
* 具体产品A
*/
public class ProductA extends Product{
@Override
public void a(){
System.out.println("PA a");
}
@Override
public void b(){
System.out.println("PA b");
}
}
/**
* 具体产品B
*/
public class ProductB extends Product{
@Override
public void a(){
System.out.println("PB a");
}
@Override
public void b(){
System.out.println("PB b");
}
}
工厂类
public class Factroy{
public static Product create(String type){
Product p = null;
switch(type){
case "A":
p = new ProductA();break;
case "B":
p = new ProductB();break;
default :
}
return p;
}
}
我们这里使用工厂类的create方法,根据传入的参数的不同,生成不同的产品。
public class test{
public void test(){
Factory.create("A").a();
Factory.create("B").b();
}
}
Output
PA a
PB b
定义一个接口用来创建对象,让他的子类去决定实例化哪个类。
定义抽象产品
public abstract class Product{
public abstract void deal();
}
具体产品实现
public class ProductA extends Product {
@Override
public void deal() {
System.out.println("product A");
}
}
public class ProductB extends Product {
@Override
public void deal() {
System.out.println("product B");
}
}
抽象工厂
public abstract class Factory {
public abstract Product create();
}
具体工厂实现
public class FactoryA extends Factory {
@Override
public Product create() {
return new ProductA();//创建ProductA
}
}
public class FactoryB extends Factory {
@Override
public Product create() {
return new ProductB();//创建ProductB
}
}
public class test{
public void test() {
Factory factoryA = new FactoryA();
Product productA = factoryA.create();
productA.deal();
Factory factoryB = new FactoryB();
Product productB = factoryB.create();
productB.deal();
}
}
Output
product A
product B
提供一个接口去创建一组相关或者相互依赖的对象,不用去制定他们的具体类。
抽象公共接口
public abstract class Guitar{
public abstract void getGuitar();
}
public abstract class Bass{
public abstract void getBass();
}
public abstract class Drum{
public abstract void getDrum();
}
具体产品实现
public class ESP extends Guitar{
@Override
public void getGuitar() {
System.out.println("Guitar:ESP");
}
}
public class Fender extends Guitar{
@Override
public void getGuitar() {
System.out.println("Guitar:Fender");
}
}
public class ESP extends Bass{
@Override
public void getBass() {
System.out.println("Bass:ESP");
}
}
public class Fender extends Bass{
@Override
public void getBass() {
System.out.println("Bass:Fender");
}
}
public class Roland extends Drum{
@Override
public void getDrum() {
System.out.println("Drum:Roland");
}
}
public class Gretsch extends Drum{
@Override
public void getDrum() {
System.out.println("Drum:Gretsch");
}
}
抽象工厂
public abstract class BankFactory{
public abstract Guitar createGuitar();
public abstract Bass createBass();
public abstract Drum createDrum();
}
具体工厂类
public class PopBankFactory extends BankFactory{
@Override
public Guitar createGuitar() {
return new Fender();
}
@Override
public Bass createBass(){
return new Fender();
}
@Override
public Drum createDrum(){
return new Gretsch();
}
}
public class RockBankFactory extends BankFactory{
@Override
public Guitar createGuitar() {
return new ESP();
}
@Override
public Bass createBass(){
return new ESP();
}
@Override
public Drum createDrum(){
return new Roland();
}
}
这里我们就可以根据不同的需要生产特定的组合产品了。
public class test{
public void test(){
System.out.println("--------------------组建一支摇滚乐队-----------------------");
RockBankFactory rockBand = new RockBankFactory();
rockBand.createGuitar().getGuitar();
rockBand.createBass().getBass();
rockBand.createDrum().getDrum();
System.out.println("--------------------组建一支流行乐队-----------------------");
PopBankFactory popBand = new PopBankFactory();
popBand.createGuitar().getGuitar();
popBand.createBass().getBass();
popBand.createDrum().getDrum();
}
}
Output
--------------------组建一支摇滚乐队-----------------------
Guitar:ESP
Bass:ESP
Drum:Gretsch
--------------------组建一支流行乐队-----------------------
Guitar:Fender
Bass:Fender
Drum:Roland