Abstract Factory Method
definition:
Abstract factory pattern provides a create a series of related or dependent objects interface. Without specifying their concrete classes.
Types of:
Creating type.
Applicable scene:
The client (application layer) does not depend on how the product class instance is created, the realization details.
He stressed the need to create an object using a lot of duplicate code with a series of related product objects (belonging to the same product family).
It provides a product class library, all of the products in the same interface to appear, 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.
The range of products to create a unified family together.
Disadvantages:
To provide for all possible sets are created products, expand into new product family of products is difficult, we need to modify the abstract factory interface.
Increasing the abstraction of the system and the difficulty of understanding.
Code Example:
For each course, we need it with video, notes, before if you use the factory method pattern, then we would create the various categories of Notes, Notes factories, notes abstract class and so on, will be a lot of new classes to implement, may lead to explosion class .
Abstract factory used method, this situation can be optimized.
- New courses Factory:
package com.design.pattern.creational.abstractFactory;
public interface CourseFactory {
Video getVideo();
Article getArticle();
}
- Create a video, notes abstract class:
package com.design.pattern.creational.abstractFactory;
public abstract class Video {
public abstract void produce();
}
package com.design.pattern.creational.abstractFactory;
public abstract class Article {
public abstract void produce();
}
- Create a java course the factory, the factory interface to achieve curriculum:
package com.design.pattern.creational.abstractFactory;
public class JavaCourseFactory implements CourseFactory{
@Override
public Video getVideo() {
return new JavaVideo();
}
@Override
public Article getArticle() {
return new JavaArticle();
}
}
- Create a java-video, article, inherited abstract class:
package com.design.pattern.creational.abstractFactory;
public class JavaVideo extends Video{
@Override
public void produce() {
System.out.println("录制Java课程视频");
}
}
package com.design.pattern.creational.abstractFactory;
public class JavaArticle extends Article{
@Override
public void produce() {
System.out.println("编写Java课程手记");
}
}
For other types of courses can be as Python, Web operations such as Java. New course, then need to be modified before the plant, can be created directly.
- Write test classes:
package com.design.pattern.creational.abstractFactory;
public class Test {
public static void main(String[] args) {
CourseFactory courseFactory = new JavaCourseFactory();
Video video = courseFactory.getVideo();
Article article = courseFactory.getArticle();
video.produce();
article.produce();
}
}
However, if we want to add the product level (for example, the need for programs like the plant source), would be more trouble, this scenario violates the principle of opening and closing. Thus, the product is suitable for family abstract factory relatively fixed scene.
JDK source code used in the abstract factory design pattern:
Java in sql.Connection:
Statement createStatement() throws SQLException;
PreparedStatement prepareStatement(String sql)
throws SQLException;
Various acquisition methods defined there, they belong to the same product family.
Enter Statement.java:
ResultSet executeQuery(String sql) throws SQLException;
int executeUpdate(String sql) throws SQLException;
......
They belong to various return Statement, all methods are required to implement the subclass. And Connection, Statement and corresponding to a different implementation class.