工厂模式与代理工厂模式

一 ,工厂模式   

        工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

        由于工厂模式较为简单,就不进行详细的解释,文章中直接进行代码的演示举例,同学们有兴趣可以在这里看看:http://www.runoob.com/design-pattern/factory-pattern.html。

        首先创建一个接口,以便规范后续的编码

public interface Animal {
void draw();
}

        然后创建多个(本例创建两个)实现该接口规范的

public class Cat implements Animal {
public void draw() {
// TODO Auto-generated method stub
System.out.println("画一只猫");
}
}
public class Dog implements Animal {
public void draw() {
// TODO Auto-generated method stub
System.out.println("画一只狗");
}
}

然后创建一个工厂类,用于根据用户需求生成指定的类的对

public class AnimalFactory {
public Animal getAnimal(String animal){
if(animal.equalsIgnoreCase("Dog")){
return new Dog();
}
if(animal.equalsIgnoreCase("Cat")){
return new Cat();
}
return null;
}
}
接下来就可以进行测试了
public class Test1 {
@Test
public void test1(){
AnimalFactory aFactory=new AnimalFactory();
Animal cat=aFactory.getAnimal("Cat");
cat.draw();
Animal dog=aFactory.getAnimal("Dog");
dog.draw();
}
}
运行结果如下图所示,工厂能根据自身的需求,进行相关对象的创建。

二,抽象工厂模式

        抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

        先创建一个画衣服的接口

public interface Clothes {
void draw();
}

        再根据接口画出两种衣服

public class Shirt implements Clothes {
public void draw() {
// TODO Auto-generated method stub
System.out.println("衬衣");
}

}


public class Coat implements Clothes {
public void draw() {
// TODO Auto-generated method stub
System.out.println("大衣");
}
}

      创建裤子的接口
public interface Pants {
void draw();
}
     根据接口创建两个裤子的类型
public class Shorts implements Pants {
public void draw() {
// TODO Auto-generated method stub
System.out.println("短裤");
}

}


public class Trousers implements Pants {
public void draw() {
// TODO Auto-generated method stub
System.out.println("长裤");
}
}

     创建抽象工厂的接口,用于规范抽象工厂的规范。

public interface AbstractFactory {
public Clothes getClothes(String Clothes);
public Pants getpants(String Pants);
}
    创建抽象工厂类,用于生成工厂对象。
public class FactoryProducer {
public static AbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase("Pants")){
return new PantsFactory();
}
if(choice.equalsIgnoreCase("Clothes")){
return new ClothesFactory();
}
return null;
}
}
    创建相应的工厂类,用于生成裤子类型和衣服类型的对象。
public class ClothesFactory implements AbstractFactory {
public Clothes getClothes(String Clothes) {
if(Clothes.equalsIgnoreCase("Coat")){
return new Coat();
}
if(Clothes.equalsIgnoreCase("Shirt")){
return new Shirt();
}
return null;
}
public Pants getpants(String Pants) {
return null;
}

}


public class PantsFactory implements AbstractFactory {
public Clothes getClothes(String Clothes) {
return null;
}
public Pants getpants(String Pants) {
if(Pants.equalsIgnoreCase("Shorts")){
return new Shorts();
}
if(Pants.equalsIgnoreCase("Trousers")){
return new Trousers();
}
return null;
}
}

    测试部分代码

        @Test
public void test2(){
AbstractFactory aFactory= FactoryProducer.getFactory("Clothes");
Clothes c=aFactory.getClothes("Coat");
c.draw();
AbstractFactory bFactory=FactoryProducer.getFactory("Pants");
Pants pants=bFactory.getpants("Shorts");
pants.draw();
}
    测试结果:

总结:工厂模式在生产环境中主要承担了创建对象的作用,为类的使用者隐藏了创建细节,其目的是通过创建对象的接口让子类决定实例化哪一个工厂类,使对象的生成过程延迟到子类中进行。抽象工厂模式则是将工厂对象的生成也一并交给抽象工厂进行。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值