工厂模式

Java83

工厂模式,是java中最常用的设计模式。这类设计模式,属于创建型设计模式,它提供了一种创建对象的最佳方式。

我们不需要公开创建逻辑来创建对象。我们只需要使用此接口来提供必要的信息,以便从工厂获得相应的对象。

[En]

We do not need to expose the creation logic to create objects. We only need to use this interface to provide the necessary information to obtain the corresponding objects from the factory.

实现案例:

女娲造人

女娲可以创造三种类型的人,黑色、白色和黄色。我们只需要知道我们想要创造的肤色,然后我们就可以直接打电话给工厂,获取具有相应肤色的人

[En]

Nuwa can create three types of people, black, white and yellow. We just need to know the skin color we want to create, and then we can directly call the factory to obtain the people with the corresponding skin color

第一步:创建一个人类接口

工厂模式

第二步:分别创建三类人,实现人类的接口

黑种人

工厂模式

输入验证码查看隐藏内容

扫描二维码关注本站微信公众号 Johngo学长
或者在微信里搜索 Johngo学长
回复 svip 获取验证码
wechat Johngo学长