设计模式—工厂模式
工厂模式是java最常用的设计模式之一,属于创建型模式,它提供了一种创建对象最佳方式,我们只需要定义一个创建对象的接口,让其子类决定实例化哪一个工厂类。
1.简单工厂模式
现在我们定义一个汽车工厂类,调用者只需要传一个汽车名字(Benz或Audi)就可以创建出相对应的汽车对象
-
创建一个汽车的接口
public interface Car { void getName(); }
- 1
- 2
- 3
-
创建实现接口的实现类
public class Benz implements Car{ @Override public void getName() { System.out.println("奔驰"); } }
- 1
- 2
- 3
- 4
- 5
- 6
public class Audi implements Car{ @Override public void getName() { System.out.println("奥迪"); } }
- 1
- 2
- 3
- 4
- 5
- 6
-
创建一个工厂类,用于生成给定信息的实体类对象
public class CarFactory { //根据传入的参数来决定实例化的对象 public static Car getCar(String name){ if (name=="Benz"){ return new Benz(); }else if (name=="Audi"){ return new Audi(); }else { return null; } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
-
使用该工厂,通过传递所需类型信息来获取实例对象
public static void main(String[] args) { Car audi = CarFactory.getCar("Audi"); Car benz = CarFactory.getCar("Benz"); audi.getName(); benz.getName(); } //---------------------------------------输出结果------------------------------------- 奥迪 奔驰
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
2.抽象工厂模式
抽象工厂模式是围绕一个超级工厂来创造其他工厂,与工厂方法模式不同的是,工厂方法模式中的工厂只生产单一的产品,而抽象工厂模式中的工厂生产多个产品
-
为手机创建一个接口
public interface PhoneProduct { void star(); void sendSMS(); }
- 1
- 2
- 3
- 4
-
创建实现手机接口的实现类
public class MiPhone implements PhoneProduct{ @Override public void star() { System.out.println("小米开机"); } @Override public void sendSMS() { System.out.println("小米发短信"); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
public class HUAWEIPhone implements PhoneProduct{ @Override public void star() { System.out.println("华为开机"); } @Override public void sendSMS() { System.out.println("华为发短信"); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
-
为路由器创建一个接口
public interface RouterProduct { void star(); void setting(); }
- 1
- 2
- 3
- 4
-
创建实现路由器接口的实现类
public class MiRouter implements RouterProduct{ @Override public void star() { System.out.println("小米路由器开机"); } @Override public void setting() { System.out.println("小米路由器设置"); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
public class HUAWEIRouter implements RouterProduct{
@Override
public void star() {
System.out.println("华为路由器开机");
}
@Override
public void setting() {
System.out.println("华为路由器设置");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
-
为手机和路由器创建一个抽象产品工厂接口(ProductFactory)
public interface ProductFactory { //生产手机 PhoneProduct phoneProduct(); //生产路由器 RouterProduct routerProduct(); }
- 1
- 2
- 3
- 4
- 5
- 6
-
创建手机和路由器抽象产品工厂实现类
public class MiFactory implements ProductFactory{ @Override public PhoneProduct phoneProduct() { return new MiPhone(); }
<span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> RouterProduct <span class="token function">routerProduct</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">MiRouter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
public class HUAWEIFactory implements ProductFactory{ @Override public PhoneProduct phoneProduct() { return new HUAWEIPhone(); }
<span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> RouterProduct <span class="token function">routerProduct</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">HUAWEIRouter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
-
使用ProductFactory接口来获取不同的实现类
public class Client { public static void main(String[] args) { PhoneProduct phoneProduct; RouterProduct routerProduct; System.out.println("===========================小米========================"); ProductFactory miFactory = new MiFactory(); //小米手机 phoneProduct = miFactory.phoneProduct(); phoneProduct.star(); phoneProduct.sendSMS(); //小米路由器 routerProduct = miFactory.routerProduct(); routerProduct.star(); routerProduct.setting(); System.out.println("===========================华为========================"); ProductFactory huaweiFactory = new HUAWEIFactory(); //华为手机 phoneProduct = huaweiFactory.phoneProduct(); phoneProduct.star(); phoneProduct.sendSMS(); //华为路由器 routerProduct = huaweiFactory.routerProduct(); routerProduct.star(); routerProduct.setting(); } } //-------------------------------------------输出结果----------------------------------------- ===========================小米======================== 小米开机 小米发短信 小米路由器开机 小米路由器设置 ===========================华为======================== 华为开机 华为发短信 华为路由器开机 华为路由器设置
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
</div>