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


      

      1 package com.tn.pattern;
      2 
      3 public class Client {
      4     public static void main(String[] args) {
      5         AbstractFactory factory=LinuxFactory.getInstance();
      6         factory.buttonFactory();
      7         factory.textFactory();
      8         
      9         factory=WindowsFactory.getInstance();
     10         factory.buttonFactory();
     11         factory.textFactory();
     12         
     13         //后期添加一个产品族OtherOs
     14         factory=OtherOsFactory.getInstance();
     15         factory.buttonFactory();
     16         factory.textFactory();
     17         
     18         //测试再后期添加的产品等级
     19         factory.radioFactory();
     20     }
     21 }
     22 /**
     23  * 
     24  * @author 
     25  *有多少产品等级结构,工厂角色就有多少个工厂方法;
     26  *有多少个产品族,就有多少个具体产品。
     27  *本例有Linux和Windows两个产品族,所有两个对应的工厂
     28  *每个产品族有button和text两个对象,所有每个工厂有创建这两个对象的工厂方法
     29  */
     30 interface AbstractFactory{
     31     Button buttonFactory();
     32     Text textFactory();
     33     
     34     //为再后期添加的产品等级Radio添加对应工厂角色的工厂方法
     35     Radio radioFactory();
     36 }
     37 
     38 class LinuxFactory implements AbstractFactory{
     39     private static LinuxFactory linuxFactory=null;
     40     public static LinuxFactory getInstance(){
     41         if(linuxFactory==null)
     42             linuxFactory=new LinuxFactory();
     43         return linuxFactory;
     44     }
     45     
     46     private LinuxFactory(){}
     47     
     48     @Override
     49     public Button buttonFactory(){
     50         return new LinuxButton();
     51     }
     52     
     53     @Override
     54     public Text textFactory(){
     55         return new LinuxText();
     56     }
     57 
     58     //为再后期添加的产品等级Radio添加对应具体工厂角色的工厂方法的实现
     59     @Override
     60     public Radio radioFactory() {
     61         return new LinuxRadio();
     62     }
     63 }
     64 
     65 class WindowsFactory implements AbstractFactory{
     66     private static WindowsFactory windowsFactory=null;
     67     
     68     public static WindowsFactory getInstance(){
     69         if(windowsFactory==null)
     70             windowsFactory=new WindowsFactory();
     71         return windowsFactory;
     72     }
     73     
     74     private WindowsFactory(){}
     75     
     76     @Override
     77     public Button buttonFactory() {
     78         return new WindowsButton();
     79     }
     80     
     81     @Override
     82     public Text textFactory() {
     83         return new WindowsText();
     84     }
     85 
     86     //为再后期添加的产品等级Radio添加对应具体工厂角色的工厂方法的实现
     87     @Override
     88     public Radio radioFactory() {
     89         return new WindowsRadio();
     90     }
     91 }
     92 
     93 //后期添加一个产品族:OtherOs
     94 class OtherOsFactory implements AbstractFactory{
     95     private static OtherOsFactory otherOsFactory=null;
     96     
     97     public static OtherOsFactory getInstance(){
     98         if(otherOsFactory==null)
     99             otherOsFactory=new OtherOsFactory();
    100         return otherOsFactory;
    101     }
    102     
    103     private OtherOsFactory(){}
    104     
    105     @Override
    106     public Button buttonFactory() {
    107         return new OtherOsButton();
    108     }
    109 
    110     @Override
    111     public Text textFactory() {
    112         return new OtherOsText();
    113     }
    114 
    115     //为再后期添加的产品等级Radio添加对应具体工厂角色的工厂方法的实现
    116     @Override
    117     public Radio radioFactory() {
    118         return new OtherOsRadio();
    119     }
    120 }
    121 
    122 interface Button{}
    123 
    124 class LinuxButton implements Button{
    125     public LinuxButton(){
    126         System.out.println("LinuxButton.LinuxButton()");
    127     }
    128 }
    129 
    130 class WindowsButton implements Button{
    131     public WindowsButton(){
    132         System.out.println("WindowsButton.WindowsButton()");
    133     }
    134 }
    135 
    136 //后期添加一个产品族OtherOs对应的产品Button
    137 class OtherOsButton implements Button{
    138     public OtherOsButton(){
    139         System.out.println("OtherOsButton.OtherOsButton()");
    140     }
    141 }
    142 
    143 interface Text{}
    144 
    145 class LinuxText implements Text{
    146     public LinuxText(){
    147         System.out.println("LinuxText.LinuxText()");
    148     }
    149 }
    150 
    151 class WindowsText implements Text{
    152     public WindowsText(){
    153         System.out.println("WindowsText.WindowsText()");
    154     }
    155 }
    156 
    157 //后期添加一个产品族OtherOs对应的产品Text
    158 class OtherOsText implements Text{
    159     public OtherOsText(){
    160         System.out.println("OtherOsText.OtherOsText()");
    161     }
    162 }
    163 
    164 //再后期添加一个产品等级:Radio
    165 interface Radio{}
    166 
    167 //为再后期添加的产品等级Radio添加对应产品族产品
    168 class LinuxRadio implements Radio{
    169     public LinuxRadio(){
    170         System.out.println("LinuxRadio.LinuxRadio()");
    171     }
    172 }
    173 
    174 //为再后期添加的产品等级Radio添加对应产品族产品
    175 class WindowsRadio implements Radio{
    176     public WindowsRadio(){
    177         System.out.println("WindowsRadio.WindowsRadio()");
    178     }
    179 }
    180 
    181 //为再后期添加的产品等级Radio添加对应产品族产品
    182 class OtherOsRadio implements Radio{
    183     public OtherOsRadio(){
    184         System.out.println("OtherOsRadio.OtherOsRadio()");
    185     }
    186 }
  • 相关阅读:
    SharePoint 技巧系列: 提权
    SharePoint 2010 开发多语言用户界面
    dos下各种常见命令索引
    chrome浏览器背景颜色修改方案
    C/C++内存泄漏及检测(vs2005平台)【转】
    一些debug常用的"魔法"数值
    学习OpenCV——HOG+SVM
    Jane Eyre
    文件夹名称为单字,右键单击出“Runtime Error”
    桌面右键菜单的“排列图标”和“新建”选项丢失问题
  • 原文地址:https://www.cnblogs.com/xiongjiawei/p/6904869.html
Copyright © 2020-2023  润新知