★ 对抽象工厂模式的一点理解

本文介绍了一种软件设计模式——抽象工厂模式,并通过具体代码示例展示了如何创建一系列相关或相互依赖的对象,而无需指定它们具体的类。此外,还提供了一些与软件开发相关的话题和技术讨论。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Aosu易博通,一分钱不花,实现网文自动摘抄, 博客写作方便又快捷,和您现在看到的一样 !自主嵌入Google广告,还能赚取美金! 现在就下载

src="http://aosustudio.com.cn//Upload/Articles/070325/fd048511-18fd-4b65-a776-a48ec09fc11d/2953034e-a94f-4133-bd82-ef9962acdcad.html" frameborder="0" width="100%" scrolling="no" height="270">

 
package abstractfactory;


// 接口  A
interface ProductA {
    void sayHello(String name);
}


//  类的具体的实现 一
class ProductA1 implements ProductA {
    public void sayHello(String name) {
        System.out.println("hello : " + name);
    }
}


//  类的实现 二
class ProductA2 implements ProductA {
    public void sayHello(String name) {
        System.out.println("hello : " + name);
    }
}


//接口 B
interface ProductB {
       ...........
}

// 类的具体的实现一
class ProductB1 implements ProductB {
       ..........
}


// 类的具体的实现二
class ProductB2 implements ProductB {
       ...........
}


//   返回对象实例 的接口
interface Creator {
    public ProductA factoryA();

    public ProductB factoryB();
}


//  抽象工厂的接口具体的实现 一
class Creator1 implements Creator {
    public ProductA factoryA() {
        return new ProductA1();
    }

    public ProductB factoryB() {
        return new ProductB1();
    }

}


//  抽象工厂的具体实现 二
class Creator2 implements Creator {
    public ProductA factoryA() {
        return new ProductA2();
    }

    public ProductB factoryB() {
        return new ProductB2();
    }

}


//   测试方法
public class Test {

    public static void main(String[] args) {

        ProductA pa;
        ProductB pb;

        Creator1 creator1 = new Creator1();
        pa = creator1.factoryA();
        pa.sayHello("张迪");

    }

}


 
今日热点:

src="http://aosustudio.com.cn/ad/GoogleSerch.htm" frameborder="0" width="100%" scrolling="no" height="30">

 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值