zoukankan      html  css  js  c++  java
  • 抽象工厂模式

    抽象工厂模式:

    1、抽象工厂模式是工厂方法模式的扩展【工厂方法模式:仅生产一种产品、抽象工厂模式:生产同一系列的多种产品】

    //男孩接口
    package com.wch.project;
    
    public interface BoyInterface {
    
    	public void drawBoy();
    }
    
    
    //女孩接口
    package com.wch.project;
    
    public interface GirlInterface {
    
    	public void drawGirl();
    }
    
    
    //中国男孩类
    package com.wch.project;
    
    public class ChineseBoy implements BoyInterface {
    
    	@Override
    	public void drawBoy() {
    		// TODO Auto-generated method stub
             System.out.println("----------------chinese boy----------------");
    	}
    
    }
    
    //中国女孩类
    package com.wch.project;
    
    public class ChineseGirl implements GirlInterface {
    
    	@Override
    	public void drawGirl() {
    		// TODO Auto-generated method stub
    		 System.out.println("----------------chinese girl----------------");
    	}
    
    }
    
    
    
    //中国工厂
    package com.wch.project;
    
    public class ChineseFactory implements PersonFactory {
    
    	@Override
    	public BoyInterface getBoy() {
    		// TODO Auto-generated method stub
            return new ChineseBoy();
    	}
    
    	@Override
    	public GirlInterface getGirl() {
    		// TODO Auto-generated method stub
    		return new ChineseGirl();
    	}
    
    }
    
    
    //美国男孩类
    package com.wch.project;
    
    public class AmericanBoy implements BoyInterface {
    
    	@Override
    	public void drawBoy() {
    		// TODO Auto-generated method stub
    		  System.out.println("----------------American boy----------------");
    	}
    
    }
    
    
    //美国女孩类
    package com.wch.project;
    
    public class AmericanGirl implements GirlInterface {
    
    	@Override
    	public void drawGirl() {
    		// TODO Auto-generated method stub
    		  System.out.println("----------------American girl----------------");
    	}
    
    }
    
    
    //美国工厂
    package com.wch.project;
    
    public class AmericanFactory implements PersonFactory {
    
    	@Override
    	public BoyInterface getBoy() {
    		// TODO Auto-generated method stub
            return new AmericanBoy();
    	}
    
    	@Override
    	public GirlInterface getGirl() {
    		// TODO Auto-generated method stub
    		return new AmericanGirl();
    	}
    
    }
    
    
    //客户端调用
    package com.wch.project;
    
    public class WchTest {
    
    	public static void main(String[] args) throws Exception{
    		
    		/*PersonFactory personFactory = new ChineseFactory();
    		BoyInterface boy = personFactory.getBoy();
    		boy.drawBoy();*/
    		
    		
    		PersonFactory personFactory = new AmericanFactory();
    		GirlInterface girl = personFactory.getGirl();
    		girl.drawGirl();
    		
    	}
    	
    }
    

      

  • 相关阅读:
    玩机分享之群晖利用反代域名访问
    Clipboard.SetText()卡住问题
    KB4040973 KB3178034 补丁导致wpf无法启动异常
    WPF 启动缓慢问题
    Jetbrains系列产品2019.2.3最新激活方法
    .net 4.0 以下HttpWebRequest Header 修改hosts方法
    Crypto++ 无法解析的外部符号 CryptoPP::AssignIntToInteger
    关于WDK开发内核签名之WHQL签名认证流程简介
    ico制作工具
    VUE监听滚动条事件
  • 原文地址:https://www.cnblogs.com/chenweichu/p/6343705.html
Copyright © 2011-2022 走看看