设计模式:抽象工厂模式

抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态,类图如下:

设计模式:抽象工厂模式_第1张图片

代码实现:

Control接口:

package com.xs.pattern.abstractfactory.control;

public interface Control {
	public void getName();
}
Button类:
package com.xs.pattern.abstractfactory.control;


public abstract class Button implements Control {

	public abstract void getBackgroundColor();

}
Text类:
package com.xs.pattern.abstractfactory.control;

public abstract class Text implements Control {
	
	public abstract void getReadOnly();
}
WinButton类:
package com.xs.pattern.abstractfactory.control;

public class WinButton extends Button {

	@Override
	public void getBackgroundColor() {
		System.out.println("red");
	}

	public void getName() {
		System.out.println("WinButton");
	}

}
WinText类:
package com.xs.pattern.abstractfactory.control;

public class WinText extends Text {

	@Override
	public void getReadOnly() {
		System.out.println("Writable");
	}

	public void getName() {
		System.out.println("WinText");
	}

}
LinuxButton类:
package com.xs.pattern.abstractfactory.control;

public class LinuxButton extends Button {

	@Override
	public void getBackgroundColor() {
		System.out.println("yellow");
	}

	public void getName() {
		System.out.println("LinuxButton");
	}

}
LinuxText类:
package com.xs.pattern.abstractfactory.control;

public class LinuxText extends Text {

	@Override
	public void getReadOnly() {
		System.out.println("readOnly");
	}

	public void getName() {
		System.out.println("LinuxText");
	}

}
工厂接口类ControlFactory:
package com.xs.pattern.abstractfactory.factory;

import com.xs.pattern.abstractfactory.control.Control;

public interface ControlFactory {
	public Control createButton();
	public Control createText();
}
工厂类WinCtrFactory:
package com.xs.pattern.abstractfactory.factory;

import com.xs.pattern.abstractfactory.control.Button;
import com.xs.pattern.abstractfactory.control.Text;
import com.xs.pattern.abstractfactory.control.WinButton;
import com.xs.pattern.abstractfactory.control.WinText;

public class WinCtrFactory implements ControlFactory {
	public Button createButton(){
		return new WinButton();
	}

	public Text createText() {
		return new WinText();
	}
}
工厂类LinuxCtrFactory:
package com.xs.pattern.abstractfactory.factory;

import com.xs.pattern.abstractfactory.control.Button;
import com.xs.pattern.abstractfactory.control.LinuxButton;
import com.xs.pattern.abstractfactory.control.LinuxText;
import com.xs.pattern.abstractfactory.control.Text;

public class LinuxCtrFactory implements ControlFactory {

	public Button createButton() {
		return new LinuxButton();
	}

	public Text createText() {
		return new LinuxText();
	}

}
客户端:
package com.xs.pattern.abstractfactory;

import com.xs.pattern.abstractfactory.control.Button;
import com.xs.pattern.abstractfactory.control.Text;
import com.xs.pattern.abstractfactory.factory.ControlFactory;
import com.xs.pattern.abstractfactory.factory.LinuxCtrFactory;
import com.xs.pattern.abstractfactory.factory.WinCtrFactory;

public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ControlFactory factory = new WinCtrFactory();
		Button button = (Button) factory.createButton();
		button.getName();
		button.getBackgroundColor();
		Text text = (Text) factory.createText();
		text.getName();
		text.getReadOnly();
		
		System.out.println("---------");
		
		factory = new LinuxCtrFactory();
		button = (Button) factory.createButton();
		button.getName();
		button.getBackgroundColor();
		text = (Text) factory.createText();
		text.getName();
		text.getReadOnly();
	}

}
输出:
WinButton
red
WinText
Writable
---------
LinuxButton
yellow
LinuxText
readOnly

抽象工厂模式适用于同一体系的产品需在不同的环境下实现,形成多个产品族。例如上述例子中,同属UI控件,但是需要在不同平台实现,于是形成了windows家族UI控件和Linux家族UI控件。

抽象工厂模式对产品的体系结构进行了抽象,比工厂方法多了一个维度。

在抽象工厂模式中,如果要新增产品族(例如mac家族UI控件),可以新增一个实现了工厂接口的工厂类,然后新增相应的产品,符合“开—闭”原则;但是如果需要新增一种控件(例如Image控件),则需要修改所有的工厂类,违背了“开—闭”原则。

注:虽然在抽象工厂模式中可以有多个产品族,但是运行时的系统只会使用一个产品族。例如,在Windows操作系统下,只会实例化Windows家族的UI控件。


你可能感兴趣的:(设计模式:抽象工厂模式)