设计模式(10):JAVA(10):设计模式(3)工厂模式:提供了一种创建对象的最佳方式(3)抽象工厂

2021/9/4 20:08:50

本文主要是介绍设计模式(10):JAVA(10):设计模式(3)工厂模式:提供了一种创建对象的最佳方式(3)抽象工厂,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

例1 :

package com.woniuxy.j_abstractfactory.a;

// 抽象工厂: 
// 1.减少了工厂方法类,面对产品簇时,类爆炸的反面效果。
// 2.抽象工厂生成的产品簇,就是一系列产品,这些产品之间有逻辑(内在)关系!
// 3.现在想多增加一个产品簇, 表现的很好,很让人满意!

interface Food {
    public void eat();
}

interface Drink {
    public void drink();
}

interface Factory {
    Food getFood();
    Drink getDrink();
}

class Hamburger implements Food {
    @Override
    public void eat() {
        System.out.println("汉堡包");
    }
}
class LiangPi implements Food {
    @Override
    public void eat() {
        System.out.println("凉皮!!");
    }
}

class Cola implements Drink {
    @Override
    public void drink() {
        System.out.println("可乐,你值得拥有!");
    }
}
class IcePeek implements Drink {
    @Override
    public void drink() {
        System.out.println("冰峰,从小就喝它");
    }
}

class KFCFactory implements Factory {

    @Override
    public Food getFood() {
        return new Hamburger();
    }

    @Override
    public Drink getDrink() {
        return new Cola();
    }
}

class SanQinFactory implements Factory {

    @Override
    public Food getFood() {
        return new LiangPi();
    }

    @Override
    public Drink getDrink() {
        return new IcePeek();
    }
}


class Business {
    public void taste(Factory factory) {
        Food food = factory.getFood();
        Drink drink = factory.getDrink();
        
        System.out.println("评委1:开吃");
        food.eat();
        drink.drink();
        System.out.println("评委1:确实很难吃...");
        
        
        System.out.println("评委2:开吃");
        food.eat();
        drink.drink();
        System.out.println("评委2:难吃太太...");
        
        System.out.println("评委3:开吃");
        food.eat();
        drink.drink();
        System.out.println("评委3:难吃到家了...");
    }
}

// ========================================================


public class Test {
    public static void main(String[] args) {
        Business b = new Business();
        b.taste(new KFCFactory());
        b.taste(new SanQinFactory());
    }
}

// 缺点:
// 你试试,加一个产品等级, 就会“牵一发 而动全局”。 说明抽象工厂不适用于产品等级经常变化的情况。
// 如果产品簇中的产品等价是稳定的,则使用抽象工厂才比较何时。反之,还不如使用工厂方法来的更灵活。

// 换一个角度来分析以上代码:
// 我们都知道,产品就是new出来的对象, 抽象产品就是接口、抽象类。
// 那么看看现在main方法中,new了谁?? new KFCFactory() , new SanQinFactory()  这2个工厂也可以看做是产品
// 既然是产品,就应该和客户端解耦,以防产品类名变化,倒置客户端奔溃!

 

例2:

package com.woniuxy.j_abstractfactory.b;

// 针对于a包所提出的问题,重构代码如下:
interface Food {
    public void eat();
}

interface Drink {
    public void drink();
}

interface Factory {
    Food getFood();
    Drink getDrink();
}

class Hamburger implements Food {
    @Override
    public void eat() {
        System.out.println("汉堡包");
    }
}
class LiangPi implements Food {
    @Override
    public void eat() {
        System.out.println("凉皮!!");
    }
}

class Cola implements Drink {
    @Override
    public void drink() {
        System.out.println("可乐,你值得拥有!");
    }
}
class IcePeek implements Drink {
    @Override
    public void drink() {
        System.out.println("冰峰,从小就喝它");
    }
}

class KFCFactory implements Factory {

    @Override
    public Food getFood() {
        return new Hamburger();
    }

    @Override
    public Drink getDrink() {
        return new Cola();
    }
}

class SanQinFactory implements Factory {

    @Override
    public Food getFood() {
        return new LiangPi();
    }

    @Override
    public Drink getDrink() {
        return new IcePeek();
    }
}


class Business {
    public void taste(Factory factory) {
        Food food = factory.getFood();
        Drink drink = factory.getDrink();
        
        System.out.println("评委1:开吃");
        food.eat();
        drink.drink();
        System.out.println("评委1:确实很难吃...");
        
        
        System.out.println("评委2:开吃");
        food.eat();
        drink.drink();
        System.out.println("评委2:难吃太太...");
        
        System.out.println("评委3:开吃");
        food.eat();
        drink.drink();
        System.out.println("评委3:难吃到家了...");
    }
}


class SimpleFactory {
    private SimpleFactory() {
    }
    
    public static Factory getFactory(int n) {
        Factory f = null;
        switch (n) {
            case 1:
                f = new KFCFactory();
                break;
            case 2:
                f = new SanQinFactory();
                break;
        }
        return f;
    }
}

// ========================================================


public class Test {
    public static void main(String[] args) {
        Business b = new Business();
        b.taste(SimpleFactory.getFactory(1));
        b.taste(SimpleFactory.getFactory(2));
    }
}

// 我们很容易发现,又回到了原点!!! 

例3:

package com.woniuxy.j_abstractfactory.c;

import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

// 针对于a包所提出的问题,重构代码如下:

interface Food {
    public void eat();
}

interface Drink {
    public void drink();
}

interface Factory {
    Food getFood();
    Drink getDrink();
}

class Hamburger implements Food {
    @Override
    public void eat() {
        System.out.println("汉堡包");
    }
}
class LiangPi implements Food {
    @Override
    public void eat() {
        System.out.println("凉皮!!");
    }
}

class Cola implements Drink {
    @Override
    public void drink() {
        System.out.println("可乐,你值得拥有!");
    }
}
class IcePeek implements Drink {
    @Override
    public void drink() {
        System.out.println("冰峰,从小就喝它");
    }
}

class KFCFactory implements Factory {

    @Override
    public Food getFood() {
        return new Hamburger();
    }

    @Override
    public Drink getDrink() {
        return new Cola();
    }
}

class SanQinFactory implements Factory {

    @Override
    public Food getFood() {
        return new LiangPi();
    }

    @Override
    public Drink getDrink() {
        return new IcePeek();
    }
}


class Business {
    public void taste(Factory factory) {
        Food food = factory.getFood();
        Drink drink = factory.getDrink();
        
        System.out.println("评委1:开吃");
        food.eat();
        drink.drink();
        System.out.println("评委1:确实很难吃...");
        
        
        System.out.println("评委2:开吃");
        food.eat();
        drink.drink();
        System.out.println("评委2:难吃太太...");
        
        System.out.println("评委3:开吃");
        food.eat();
        drink.drink();
        System.out.println("评委3:难吃到家了...");
    }
}


class SimpleFactory {
    public  Factory getFactory( ) {
        Factory f = null;
        try {
            InputStream in = SimpleFactory.class.getResourceAsStream("aa.properties");
            Properties prop = new Properties();
            prop.load(in);
            
            String className = prop.getProperty("factory");
            System.out.println(className + "!!!!");
            f = (Factory) Class.forName(className).newInstance();
            
            prop.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return f;
    }
}

// ========================================================

public class Test {
    public static void main(String[] args) {
        while(true) {
            Business b = new Business();
            b.taste(new SimpleFactory().getFactory());
            
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 

 

aa.properties

factory=com.woniuxy.j_abstractfactory.c.KFCFactory

 



这篇关于设计模式(10):JAVA(10):设计模式(3)工厂模式:提供了一种创建对象的最佳方式(3)抽象工厂的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程