07, the factory model - the abstract factory pattern

Under the first Tucao, learning factory mode, when the beginning is very sick, but repeated studies, Understand, and later write, only to find true value.
Now officially entered the study and understanding of the abstract factory pattern.
Examples of binding under the Factory Method Pattern cited, in order to better understand the abstract factory pattern, we have slightly adjusted example, Chinese people no longer foreigners can not, for example child, not to say that the Chinese people can not use the example of foreigners, and I found a better example.
We changed the car to make an example of, for example, first define an abstract class Car, and then derive sub-abstract class, SportCar, BusinessCar, are sports cars and commercial vehicles, derived from the abstract class or subclass, as well as the following: SportBenzCar ( Mercedes-Benz sports car ), BusinessBenzCar ( Mercedes-Benz commercial ), SportBuickCar, BusinessBuickCar ( here need to think about why a direct subclass of abstract class car is SportCar and BusnessCar, then the two subclasses is Benz and Buick, instead of car direct subclass BenzCar and BuickCar ), so come, first several abstract and concrete implementation class to get, concrete classes as follows:


Code implementation follows :( contents of several files are all put together)
   
  1. //定义车抽象产品,用途只体现在多态,客户端用此定义产品对象,子类或者子类工厂类实例化
  2. public abstract class Car {
  3. public abstract void desc();
  4. }
  5. //Car派生的子类,还是抽象产品,包含跑车和商务车
  6. public abstract class SportCar extends Car {}
  7. public abstract class BusinessCar extends Car {}
  8. //接下来不同品牌对两种车型的实现
  9. public class SportBenzCar extends SportCar {
  10. public void desc() {
  11. System.out.println("哥就是传说中的奔驰跑车!");
  12. }
  13. }
  14. public class BusinessBenzCar extends BusinessCar {
  15. public void desc() {
  16. System.out.println("奔驰商务车,成功人士的选择");
  17. }
  18. }
  19. public class SportBuickCar extends SportCar {
  20. public void desc() {
  21. System.out.println("本少即别克跑车!");
  22. }
  23. }
  24. public class BusinessBuickCar extends BusinessCar {
  25. public void desc() {
  26. System.out.println("商用别克,让您快乐");
  27. }
  28. }
These are prepared by the product of abstract and concrete classes, after which our simulation, part of the customer to buy a car steps:
1, into a 4S shop (or casually into one, or get into a designated)
2. Select a vehicle type (either his thoughts have a favorite model, or listen to 4S store sales presentation)
So the question is, how is reflected in the 4S shop our program? There are a lot of 4S stores began, BMW 4S, Ford 4S, 4S Mercedes-Benz, Buick 4S, etc., but is the same, each 4S shop is for sale of various types, so since we also need to 4S shops abstract, as follows:
   
  1. //抽象4S店
  2. public abstract class SaleCar4S {
  3. public abstract SportCar choiceSport();
  4. public abstract BusinessCar choiceBusiness();
  5. //只是举了两个例子,可能还会包含 房车,越野车,甲壳虫等等车型
  6. }
  7. //奔驰4S店
  8. public class Benz4S extends SaleCar4S {
  9. public SportCar choiceSport() {
  10. return new SportBenzCar();
  11. }
  12. public BusinessCar choiceBusiness() {
  13. return new BusinessBenzCar();
  14. }
  15. }
  16. //别克4S店
  17. public class Buick4S extends SaleCar4S {
  18. public SportCar choiceSport() {
  19. return new SportBuickCar();
  20. }
  21. public BusinessCar choiceBusiness() {
  22. return new BusinessBuickCar();
  23. }
  24. }
Well, now you can write client code, client code is in accordance with the steps we have just to buy a car of:
   
  1. public class Start {
  2. public static void main(String[] args) {
  3. //购车客户走进了一家卖车的4S店,一看是奔驰的4S店
  4. SaleCar4S _4S = new Benz4S();
  5. //然后 从这个4S店里面找了一款商用车
  6. Car c = _4S.choiceBusiness();
  7. //这款商务车的广告
  8. c.desc();
  9. //客户嫌广告词太老,不喜欢,于是重新选择了一辆跑车
  10. c = _4S.choiceSport();
  11. //这款跑车的广告
  12. c.desc();
  13. //客户不满意 奔驰 4S店的 服务态度,又进了一家4S店,一看是别克的4S店
  14. _4S = new Buick4S();
  15. //然后选择了 跑车
  16. c = _4S.choiceBusiness();
  17. //然后看了下这个车的简介
  18. c.desc();
  19. }
  20. }
Results are as follows:


to sum up:
Abstract factory pattern, is a product family abstracts. Here introduces a new concept, product family.
By Baidu Encyclopedia query, abstract interpretation, is not so easy to understand, you can define them at:
The first is a product of many ethnic groups together, each product between ethnic groups do not have the same properties, but due to other sub-clan rule is isolated, his focus is the product between the ethnic groups that there is similar place, and they can be distinguished, it is focusing on the relationships between ethnic groups, rather than the relationships within a family.
For example, the sports cars, commercial vehicles can produce many products, such as Mercedes-Benz and Buick, but the brand this rule, these products form two product families - a Mercedes-Benz product family, a Buick product family. But the two product family of products does exist one to one (Mercedes-Benz sports car, the Buick car), but also another example, Li Ning store, Anta stores, 361 specialty stores, special steps store, which is a few hundreds family of products, each product family of products can be found in similar products in another product family, for example, are selling shoes, pants, shirt and so on.
Indeed, there is an example of table tennis sporting goods stores, sporting goods stores badminton (assuming that only sell two rackets and balls), then the two families is quite clear, the racket can be abstracted to a class, the ball you can be abstracted to a class. That this is the two product family.

The difference between the factory method pattern, abstract factory method mode, only the abstract factory products, while the abstract factory product family is the abstraction of the plant. So, there is a saying: the factory method pattern is a special case of the abstract factory pattern (product family is only one product), which is a generalization of the abstract factory pattern factory method pattern.

Q & A:
For the process of abstraction in the product, it is so abstract:
Car -- SportCar、BusinessCar -- BenzSportCar、BuickSportCar……
First derived type of car, before the brand is derived out of the bus.
To look at the car, which is closer to the type of property than its brand. After all, if the models of the same class, as you how sticking labels.


This mode advantages and disadvantages:
Advantages: in fact, the advantages of using the product family, product isolation make a distinction, to ensure that the range of product line, to achieve a certain degree of encapsulation.
Cons: Once the product family has a new product, then all product family factory class, abstract factory class product family, we must adjust, no maintenance costs were far this increased







Reproduced in: https: //my.oschina.net/u/1182369/blog/406570

Guess you like

Origin blog.csdn.net/weixin_33736048/article/details/92083697