抽象工厂模式(Abstract Factory)

博客介绍了抽象工厂模式,即提供创建相关或依赖对象的接口,无需指定具体类。阐述了抽象工厂、具体工厂、抽象产品、产品和客户端等角色的定义与作用,并给出了Java代码示例,展示了如何使用该模式创建不同的产品对象。

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

提供一个创建一系列相关或相互依赖的对象的接口,而不需指定它们具体的类。

通常在run-time式创建一个ConcreateFactory类的单体实例。这个ConcreteFactory创建ConcreteProduct对象。为了创建不同的ConcreteProduct对象,clients需要使用不同的ConcreteFactory

AbstractFactoryContinentFactory

定义一个接口,用来创建抽象products

ConcreteFactoryAfrciaFactory,AmericaFactory

实现创建具体的Products

AbstractProductHerbivoreCarnivore

声明一个接口表示一种product类型

ProductWildebeestLionBisonWolf

定义被相关Concrete factory创建的product对象

实现AbstractProduct接口

ClientAnimalWorld

使用AbstractFactoryAbstractProduct

 

 

代码:

//AbstractFactory

public interface ContinentFactory{

            public Herbivore CreateHerbivore();

            public Carnivore CreateCarnivore();

}

 

 

//ConcreteFactory

public class AfricaFactory implements ContinentFactory{

            public Herbivore CreateHerbivore(){

                        return new Wildebeest();

            }

            public Carnivore CreateCarnivore(){

                        return new Lion();

            }

}

 

public class AmericaFactory implements ContinentFactory{

            public Herbivore CreateHerbivore(){

                        return new Bison();

            }

            public Carnivore CreateCarnivore(){

                        return new Wolf();

            }

}

 

//AbstractProduct

 

public interface Herbivore{

}

 

 

public interface Carnivore{

            public void Eat(Herbivore h);

}

 

 

//Product

public class Wildebeest implements Herbivore{

}

 

public class Lion implements Carnivore{

            public void Eat(Herbivore h){

                        System.out.println(“Lion eats “ +h.getName());

            }                     

}

 

public class Bison implements Herbivore{

}

 

public class Wolf implements Carnivore{

            public void Eat(Herbivore h){

                        System.out.println(“Wolf eats “ +h.getName());

            }

}

 

//Client

public class AnimalWorld{

            private Herbivore herbivore;

            private Carnivore carnivore;

            public AnimalWorld(ContinentFactory factory){

                        carnivore=factory. CreateCarnivore();

                        herbivore=factory. CreateHerbivore();

            }

            //Mehtods

            public void RunFoodChain(){

                        carnivore.Eat(herbivore);

            }

            public static void  main(String[] args){

                        AnimalWorld world=new AnimalWorld(new AfricaFactory());

                        world.RunFoodChain();

                       

                        world=new AnimalWorld(new AmericaFactory());

                        world.RunFoodChain();

}

 

Output

Lion eats Wildebeest
Wolf eats Bison

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值