【java设计模式】【创建模式Creational Pattern】抽象工厂模式Abstract Factory Pattern

时间:2024-06-29 23:34:08

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAW0AAABvCAIAAACo3AbKAAALvUlEQVR4nO1dUa7cOA7U/c+zwJxkf4JkvvcQAbwf743jkcgSScm2xK6CENi0RFIlqlpuJJ1yHMePn7+OLMg0l5VBnr3IytjXvMqPn78KQRBEFD9+/irHcZRSfv/3PzlaKeVtgf4IkGcvsjL2NS/qCBEBefYiK2MmHZkuMZXDO/y/TexHgDx7kZUx63mku9XBi1PXIXVkU5BnL7IypupI92sVo9BYNMIoPdSR1UCevcjK2LTvR4w60urFHd/LZF2t1UCevcjKWEdHxs8d2MNNUpJ1tVYDefYiK2P984jxi4yRN6C5LzXUkcdAnr3IypiqI+irkbENr73XTDyVZF2t1UCevcjKGNKRiS8aouf7ImZdrdVAnr3IytgEHRHPKeDwUunIxJMOdeRhkGcvsjI2R0faW+CN55E0IM9eZGVsFR0RR1FHFgd59iIrY9QRIg7y7EVWxt7/fkRTGerI+iDPXmRlTNYR12Z2nUeuF9oZhN+z7gLy7EVWxtTzyNYt62qtBvLsRVbGvnWEv4dGEMQI+HtoRBDk2YusjBW+1xBhkGcvsjJGHSHiIM9eZGWMOkLEQZ69yMoYdYSIgzx7kZUxQUfOL2C1Lbq+4pyrdc5Fm/xN69p+m31HlNexIM+LU714emHI5xGgFHvpyAFX7tayqzwbA4GtOCGn2Xid56uQddNwObwJa67jONw6skUz1vcDzHrTSKkjD2eygp+3/L8Fn46IrzynpXpa9RQfiQ6f0RHxGHxaqqdVT/FRO6TNoeunzUqzY4da/7lYgWcQHfCphb6bt7tX5C2UwHmkffpFfduhNIrT7fPwecSyM8We3T5aUbpusR3E7TqcghV4NlrCfuaCOtLREfEW6EirPuvoiHjb3f+WwrX4uTXPuViBZ+AfHDHsfuaCOkIdieuIy8+tec7FCjwb/bdPqSMTQR2hjsSxAs9G/+1T6shEmHQE3xp1pFINTWvu1pHuNr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fJMxs55Hfw9NIIgBpHw99AIgngSJeV7DUEQT2JXHWmzFfO3nMeM/l3JvAVvJpiHymh3bukZcH4rz3NnNxJ6nXKyI7+OeP0YB3qjBLQsAOzKG9dIsjcNsQ8Ycj5aREcsnUc+t/baiV+gjsj/YxsuAlfnQFYxlOJYx4CI4M4uKkRLt/9NvFn8j08Q23Esz1TeQdlOR1wretaH1lOctYuKAG93UH312fUvsiRaugRaHLa3eBVGNmEALuoCPoHdWJDrYz8d+QIo/a4x4Mc43Dh2OtWuWVhytoiF3Se4baX/2kHTLzG9GEaoA53t9pbhvXbiF6gjjiLuDn9FR7St3hWI7j7UZtcl36Uj3bjAPsikizpvN6PwiZaJQvkMPkVHtOVsS9xe2W1n49YF5RXoDBIAhVicm/mmW3EhXEtwvb6POrtnLyoPIiHro3yOjnTt1UJaPJ9/uqrqPqotnsX0LJsKTNB7K/rBWwgs2RS8vmSYtMWxmY50PhHMpdnaQREDS2zt39IRF28amdc/A54PXQjAErh6xtClzu6nnbu9RLvsrYmyl454oRUcKESwnFpn7bab1XS4PHuTL5KCWFwZdQQY7csXxkQdaW8tDMRqaRHsqiPdUquMmiJY/ABmdtSRdu6xUeE0xnXEGMiFpXQESPaa2FJH7FVl/IwNO9lOR7Q9rO1nIDp2Wow5aMZwIBeoIyPYT0dc6/2AjgDYg87CCDlVHXcVZ0RHMEtdobFE8WKijrQTBLWBBXqvXbmNjliStFehWMftdppYyl2HgzB6dqUhUjRCC1ggoF+g2xTMWmVRnTXJvl6IPb3R38JmOkIQxIL41hH+HhpBECP4/j20Hz9/vS1q05BpLiuDPHuRlbGvef0fWlGFVA5dQCkAAAAASUVORK5CYII=" alt="" />

 package com.tn.pattern;

 public class Client {
public static void main(String[] args) {
AbstractFactory factory=LinuxFactory.getInstance();
factory.buttonFactory();
factory.textFactory(); factory=WindowsFactory.getInstance();
factory.buttonFactory();
factory.textFactory(); //后期添加一个产品族OtherOs
factory=OtherOsFactory.getInstance();
factory.buttonFactory();
factory.textFactory(); //测试再后期添加的产品等级
factory.radioFactory();
}
}
/**
*
* @author
*有多少产品等级结构,工厂角色就有多少个工厂方法;
*有多少个产品族,就有多少个具体产品。
*本例有Linux和Windows两个产品族,所有两个对应的工厂
*每个产品族有button和text两个对象,所有每个工厂有创建这两个对象的工厂方法
*/
interface AbstractFactory{
Button buttonFactory();
Text textFactory(); //为再后期添加的产品等级Radio添加对应工厂角色的工厂方法
Radio radioFactory();
} class LinuxFactory implements AbstractFactory{
private static LinuxFactory linuxFactory=null;
public static LinuxFactory getInstance(){
if(linuxFactory==null)
linuxFactory=new LinuxFactory();
return linuxFactory;
} private LinuxFactory(){} @Override
public Button buttonFactory(){
return new LinuxButton();
} @Override
public Text textFactory(){
return new LinuxText();
} //为再后期添加的产品等级Radio添加对应具体工厂角色的工厂方法的实现
@Override
public Radio radioFactory() {
return new LinuxRadio();
}
} class WindowsFactory implements AbstractFactory{
private static WindowsFactory windowsFactory=null; public static WindowsFactory getInstance(){
if(windowsFactory==null)
windowsFactory=new WindowsFactory();
return windowsFactory;
} private WindowsFactory(){} @Override
public Button buttonFactory() {
return new WindowsButton();
} @Override
public Text textFactory() {
return new WindowsText();
} //为再后期添加的产品等级Radio添加对应具体工厂角色的工厂方法的实现
@Override
public Radio radioFactory() {
return new WindowsRadio();
}
} //后期添加一个产品族:OtherOs
class OtherOsFactory implements AbstractFactory{
private static OtherOsFactory otherOsFactory=null; public static OtherOsFactory getInstance(){
if(otherOsFactory==null)
otherOsFactory=new OtherOsFactory();
return otherOsFactory;
} private OtherOsFactory(){} @Override
public Button buttonFactory() {
return new OtherOsButton();
} @Override
public Text textFactory() {
return new OtherOsText();
} //为再后期添加的产品等级Radio添加对应具体工厂角色的工厂方法的实现
@Override
public Radio radioFactory() {
return new OtherOsRadio();
}
} interface Button{} class LinuxButton implements Button{
public LinuxButton(){
System.out.println("LinuxButton.LinuxButton()");
}
} class WindowsButton implements Button{
public WindowsButton(){
System.out.println("WindowsButton.WindowsButton()");
}
} //后期添加一个产品族OtherOs对应的产品Button
class OtherOsButton implements Button{
public OtherOsButton(){
System.out.println("OtherOsButton.OtherOsButton()");
}
} interface Text{} class LinuxText implements Text{
public LinuxText(){
System.out.println("LinuxText.LinuxText()");
}
} class WindowsText implements Text{
public WindowsText(){
System.out.println("WindowsText.WindowsText()");
}
} //后期添加一个产品族OtherOs对应的产品Text
class OtherOsText implements Text{
public OtherOsText(){
System.out.println("OtherOsText.OtherOsText()");
}
} //再后期添加一个产品等级:Radio
interface Radio{} //为再后期添加的产品等级Radio添加对应产品族产品
class LinuxRadio implements Radio{
public LinuxRadio(){
System.out.println("LinuxRadio.LinuxRadio()");
}
} //为再后期添加的产品等级Radio添加对应产品族产品
class WindowsRadio implements Radio{
public WindowsRadio(){
System.out.println("WindowsRadio.WindowsRadio()");
}
} //为再后期添加的产品等级Radio添加对应产品族产品
class OtherOsRadio implements Radio{
public OtherOsRadio(){
System.out.println("OtherOsRadio.OtherOsRadio()");
}
}