• 抽象工厂模式


    抽象工厂模式:

    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();
    		
    	}
    	
    }
    

      

  • 相关阅读:
    41.js延迟加载的方式有哪些?
    39、[“1”, “2”, “3”].map(parseInt) 答案是多少
    38.null,undefined 的区别?
    35.说几条写JavaScript的基本规范?
    34.介绍js有哪些内置对象?
    问题解决Android studio遇到 java.lang.OutOfMemoryError: GC app:transformClassesWithDexForDebug解决方法 以及gradle优化
    Multiple dex files define
    Retrofit2.0+RxJava2.0问题
    【转】Android Shape绘制虚线在手机端查看是实线的问题
    极光使用整理
  • 原文地址:https://www.cnblogs.com/chenweichu/p/6343705.html
Copyright © 2020-2023  润新知