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

    下面用两个例子来说明:

    螺丝螺母的例子

    public interface Screw {
    
    	public void useScrew();
    }
    
    public class Screw6 implements Screw{
    
    	@Override
    	public void useScrew() {
    		System.out.println("使用6号螺丝");
    	}
    }
    
    public class Screw8 implements Screw{
    
    	@Override
    	public void useScrew() {
    		System.out.println("使用8号螺丝");
    	}
    }
    
    public interface Nut {
    
    	public void useNut();
    }
    
    public class Nut6 implements Nut{
    
    	@Override
    	public void useNut() {
    		System.out.println("使用6号螺母");	
    	}
    }
    
    public class Nut8 implements Nut{
    
    	@Override
    	public void useNut() {
    		System.out.println("使用8号螺母");	
    	}
    }
    
    public interface Product {
    
    	public Screw createScrew();
    	public Nut createNut();
    }
    
    public class Factory6 implements Product{
    
    	@Override
    	public Screw createScrew() {
    		
    		return new Screw6();
    	}
    
    	@Override
    	public Nut createNut() {
    		
    		return new Nut6();
    	}
    }
    
    public class Factory8 implements Product{
    
    	@Override
    	public Screw createScrew() {
    		
    		return new Screw8();
    	}
    
    	@Override
    	public Nut createNut() {
    		
    		return new Nut8();
    	}
    }
    
    public class Store {
    
    	public static void main(String[] args) {
    		useProduct(new Factory6());
    		useProduct(new Factory8());
    	}
    	
    	public static void useProduct(Product product){
    		Screw screw = product.createScrew();
    		screw.useScrew();
    		Nut nut = product.createNut();
    		nut.useNut();
    	}
    }
    

    image
    image

    家用电器的例子

    public interface BingXiang {
    
    	public void useBX();
    }
    
    public class HaierBX implements BingXiang{
    
    	@Override
    	public void useBX() {
    		// TODO Auto-generated method stub
    		System.out.println("我正在使用海尔冰箱。");
    	}
    }
    
    public class RongsBX implements BingXiang{
    
    	@Override
    	public void useBX() {
    		// TODO Auto-generated method stub
    		System.out.println("我正在使用容声冰箱。");
    	}
    
    }
    
    public class XiaomBX implements BingXiang{
    
    	@Override
    	public void useBX() {
    		
    		System.out.println("我正在使用小米冰箱。");
    	}
    
    }
    
    public interface TV {
    
    	public void watchTV();
    }
    
    public class HaierTV implements TV{
    
    	@Override
    	public void watchTV() {
    		// TODO Auto-generated method stub
    		System.out.println("正在用海尔电视观看节目");
    	}
    
    }
    
    public class RongsTV implements TV {
    
    	@Override
    	public void watchTV() {
    		// TODO Auto-generated method stub
    		System.out.println("正在用容声电视观看节目");
    	}
    
    }
    
    public class XiaomTV implements TV{
    
    	@Override
    	public void watchTV() {
    		// TODO Auto-generated method stub
    		System.out.println("正在用小米电视观看节目");
    	}
    
    }
    
    /**
     * TODO 家电接口,主要生产家电
     * 
     * @author ZhangPeng
     * @date 2021年6月2日 下午2:52:09
     */
    public interface JiaDianProduct {
    
    	public BingXiang createBX();
    	
    	public TV createTV();
    }
    
    public class HaierFactory implements JiaDianProduct{
    
    	@Override
    	public BingXiang createBX() {
    		// TODO Auto-generated method stub
    		return new HaierBX();
    	}
    
    	@Override
    	public TV createTV() {
    		// TODO Auto-generated method stub
    		return new HaierTV();
    	}
    
    }
    
    public class RongsFactory implements JiaDianProduct{
    
    	@Override
    	public BingXiang createBX() {
    		// TODO Auto-generated method stub
    		return new RongsBX();
    	}
    
    	@Override
    	public TV createTV() {
    		// TODO Auto-generated method stub
    		return new RongsTV();
    	}
    
    }
    
    public class XiaomFactory implements JiaDianProduct{
    
    	@Override
    	public BingXiang createBX() {
    		// TODO Auto-generated method stub
    		return new XiaomBX();
    	}
    
    	@Override
    	public TV createTV() {
    		// TODO Auto-generated method stub
    		return new XiaomTV();
    	}
    
    }
    
    public class Customer {
    
    	public static void main(String[] args) {
    //		buyJiaDian(new XiaomFactory());
    		buyJiaDian(new HaierFactory());
    	}
    	
    	public static void buyJiaDian(JiaDianProduct product){
    		BingXiang bx = product.createBX();
    		bx.useBX();
    		TV tv = product.createTV();
    		tv.watchTV();
    	}
    }
    

    image
    针对此图
    比如说当多一个产品等级的时候,例如京东,只需要建京东工厂以及创建对应的产品即可,但是当多一个产品簇的时候,例如烘干机,那么每个产品单位都需要新加方法。

  • 相关阅读:
    面试(转)
    Expression Blend实战开发技巧
    Twelve Principles of Agile Software
    Test Software Engineer
    Web开发工程师必读的15个设计博客
    麻省理工的C/C++的课程
    Orchard:处理1对多的关系
    DotNetNuke Switches to C# !!
    我的那个他
    2011 微软MVP全球大会
  • 原文地址:https://www.cnblogs.com/kaka-qiqi/p/14844196.html
Copyright © 2011-2022 走看看