抽象工廠模式(重要):
抽象工廠模式提供了創建一系列相關或相互依賴對象的接口,而無須知道他們具體的類。工廠方法模式是一種特殊的抽象工廠模式,更加地抽象。兩者最大的區別在於工廠方法模式針對一個產品等級結構(繼承結構),而抽象工廠模式針對的是多個產品等級結構(繼承結構)
抽象工廠模式的優點:
1.包含前面簡單工廠模式和工廠方法模式的優點,即將對象的創建與使用分離,不關心具體創建的細節,也不需要知道具體的類名,實現高內聚低耦合
2.增加新的對象族,無須修改原有代碼,符合開閉原則
3.保證同一個產品(對象)族中的對象將在一起使用,這對根據當前環境來決定其行爲的軟件系統來說,是一種非常使用的設計模式
抽象工廠模式的缺點:
1.增加新的產品等級結構時,需要修改原有代碼,違背了開閉原則(開閉原則的傾斜性)
抽象工廠的適用環境:
1.和簡單工廠模式和工廠方法模式一樣,不關心對象創建的細節,不需要知道要實例化對象的類名,只需要知道其對應的具體工廠類
2.系統中有多個產品(對象)族,每次只使用其中同一個產品族,同一個產品(對象)族中的產品(對象)將在一起使用
抽象工廠模式的例子
Button產品等級結構(繼承結構)
public interface Button {
String getButtonType();
}
public class LinuxButton implements Button{
/**
* @Description: TODO
* @author doudou
* @date 2019年10月7日
* @return
* @throws
* @return
* @see creation.AbstractFactoryPattern.Button#getButtonType()
*/
@Override
public String getButtonType() {
System.out.println("The type of button is LinuxButton");
return "LinuxButton";
}
}
public class WindowsButton implements Button{
/**
* @Description: TODO
* @author doudou
* @date 2019年10月7日
* @return
* @throws
* @return
* @see creation.AbstractFactoryPattern.Button#getButtonType()
*/
@Override
public String getButtonType() {
System.out.println("The type of button is WindowsButton");
return "WindowsButton";
}
}
Text產品等級結構(繼承結構)
public interface Text {
String getTextType();
}
public class WindowsText implements Text{
/**
* @Description: TODO
* @author doudou
* @date 2019年10月7日
* @return
* @throws
* @return
* @see creation.AbstractFactoryPattern.Text#getTextType()
*/
@Override
public String getTextType() {
System.out.println("The type of type is WindowsText");
return "WindowsText";
}
}
public class LinuxText implements Text{
/**
* @Description: TODO
* @author doudou
* @date 2019年10月7日
* @return
* @throws
* @return
* @see creation.AbstractFactoryPattern.Text#getTextType()
*/
@Override
public String getTextType() {
System.out.println("The type of text is LinuxText");
return "LinuxText";
}
}
工廠等級結構
public interface SwingAbstractFactory {
Button createButton();
Text createText();
}
public class WindowsFactory implements SwingAbstractFactory{
/**
* @Description: TODO
* @author doudou
* @date 2019年10月7日
* @return
* @throws
* @return
* @see creation.AbstractFactoryPattern.SwingAbstractFactory#createButton()
*/
@Override
public Button createButton() {
return new WindowsButton();
}
/**
* @Description: TODO
* @author doudou
* @date 2019年10月7日
* @return
* @throws
* @return
* @see creation.AbstractFactoryPattern.SwingAbstractFactory#createText()
*/
@Override
public Text createText() {
return new WindowsText();
}
}
public class LinuxFactory implements SwingAbstractFactory{
/**
* @Description: TODO
* @author doudou
* @date 2019年10月7日
* @return
* @throws
* @return
* @see creation.AbstractFactoryPattern.SwingAbstractFactory#createButton()
*/
@Override
public Button createButton() {
return new LinuxButton();
}
/**
* @Description: TODO
* @author doudou
* @date 2019年10月7日
* @return
* @throws
* @return
* @see creation.AbstractFactoryPattern.SwingAbstractFactory#createText()
*/
@Override
public Text createText() {
return new LinuxText();
}
}