Article Directory
Outline
Definition: the abstract factory pattern provides a create a series of related or dependent objects interface
Extended: without specifying their concrete classes
Type: Creating type
Abstract Factory can be set with the same theme and a separate factory encapsulated, in normal use, the client program to create a concrete implementation of the abstract factory, and then use the concrete implementation of the abstract factory to create the subject as an interface, we do not care specific types of internal object obtained in the process,
because our client object using a common interface, the abstract factory pattern implementation details of a set of objects and their use separated.
Applicable scene
The client (application layer) does not depend on how the product class instance is created, the realization details
Use create objects requires a lot of repetitive code with emphasis on a series of related product objects (belonging to the same product family)
A product class library, all of the products occurs at the same interface, so that the client does not depend on the specific implementation
advantage
Specific products in the application layer of code isolation, do not care about the details of creation
A series of unified product family to create together
Shortcoming
To provide for all possible product collection, product family was created in the expansion of new products difficult, need to modify the abstract factory interface
Increasing the abstraction of the system and the difficulty of understanding
Abstract Factory - Product grade structure and product family
On behalf of the same color of the same product registration
Different colors of the same shape is a product family
For example, Huawei production base, but also the production of mobile phones, also produces chips, Huawei products are of Huawei's brand, products belonging to the same level
Vertically on end to see this phone is there with a box of millet and other mobile phone brand which is the same product group
Up arrow is a concrete factory
Show
CakeFactory Interface
package softwareDesign.coding.factoryMethod.abstractFactory;
public interface CakeFactory {
Cake getCake();
GiftBox getGiftBox();
}
Cake abstract class
package softwareDesign.coding.factoryMethod.abstractFactory;
public abstract class Cake {
public abstract void produce();
}
GiftBox abstract class
package softwareDesign.coding.factoryMethod.abstractFactory;
public abstract class GiftBox {
public abstract void produce();
}
SnowCake class
package softwareDesign.coding.factoryMethod.abstractFactory;
public class SnowCake extends Cake{
@Override
public void produce() {
System.out.println("生产雪花蛋糕...");
}
}
SnowCakeGiftBox 类
package softwareDesign.coding.factoryMethod.abstractFactory;
public class SnowCakeGiftBox extends GiftBox{
@Override
public void produce() {
System.out.println("生产雪花蛋糕礼盒...");
}
}
SnowCakeFactory class
package softwareDesign.coding.factoryMethod.abstractFactory;
public class SnowCakeFactory implements CakeFactory{
@Override
public Cake getCake() {
return new SnowCake();
}
@Override
public GiftBox getGiftBox() {
return new SnowCakeGiftBox();
}
}
CCake class
package softwareDesign.coding.factoryMethod.abstractFactory;
public class CCake extends Cake{
@Override
public void produce() {
System.out.println("生产巧克力蛋糕...");
}
}
CCakeGiftBox class
package softwareDesign.coding.factoryMethod.abstractFactory;
public class CCakeGiftBox extends GiftBox{
@Override
public void produce() {
System.out.println("生产巧克力蛋糕礼盒");
}
}
CCakeFactory class
package softwareDesign.coding.factoryMethod.abstractFactory;
public class CCakeFactory implements CakeFactory {
@Override
public Cake getCake() {
return new CCake();
}
@Override
public GiftBox getGiftBox() {
return new CCakeGiftBox();
}
}
Test class
package softwareDesign.coding.factoryMethod.abstractFactory;
public class Test {
public static void main(String[] args) {
CakeFactory cakeFactory = new SnowCakeFactory();
Cake cake = cakeFactory.getCake();
GiftBox giftBox = cakeFactory.getGiftBox();
cake.produce();
giftBox.produce();
}
}
We look at its class diagram:
start with the class CakeFactory
If you increase the fruit cake and fruit cake gift box can easily expand
Join test test:
the Test only care about what means the plant from which the products. The application layer and the concrete boxes and other snow and cakes are decoupled.
Disadvantages are also obvious, if you want to expand the product level, it would be contrary to the principle of opening and closing, indeed affect the whole body.