Abstract Factory Design Model
/**
Abstract Factory design patterns: they are the same as factory design patterns, but abstract factories are productive.
The factory model is for yield lines.
A factory that produces automobiles. It produces TopCar, TopBus, MidCar, MidBus, LowCar, and LowBus.
Abstract Design Patterns are used to extract common features.
When the fourth SuperCar () class is generated, you only need to add the SuperCar class implementation interface to the original code to create a factory.
You do not need to change the original code.
*/
Interface ICar {
// System. out. println (defines methods common to automobile production );
}
Class TopCar implements ICar {
Public TopCar (){
System. out. println (producing high-end cars );
}
}
Class MidCar implements ICar {
Public MidCar (){
System. out. println (producing out-of-stock vehicles );
}
}
Class LowCar implements ICar {
Public LowCar (){
System. out. println (producing low-end cars );
}
}
Interface IBus {
// System. out. println (defines some common methods for producing buses );
}
Class TopBus implements IBus {
Public TopBus (){
System. out. println (producing high-end buses );
}
}
Class MidBus implements IBus {
Public MidBus (){
System. out. println (producing mid-range buses );
}
}
Abstract class AbstractFactory {
Public abstract ICar creatCar ();
Public abstract IBus creatBus ();
}
Class TopCarFactory extends AbstractFactory {
Public ICar creatCar (){
Return new TopCar (); // after calling this method, it is equivalent to: ICar ic = new TopCar ();
}
Public IBus creatBus (){
Return new TopBus ();
}
}
Class MidCarFactory extends AbstractFactory {
Public ICar creatCar (){
Return new MidCar (); // after calling this method, it is equivalent to: ICar ic = new MidCar ();
}
Public IBus creatBus (){
Return new MidBus ();
}
}
Class LowCarFactory extends AbstractFactory {
Public ICar creatCar (){
Return new LowCar (); // after calling this method, it is equivalent to: ICar ic = new LowCar ();
}
Public IBus creatBus (){
Return null;
}
}
Public class AbstractFactoryDemos {
Public static void main (String [] args ){
LowCarFactory lcf = new LowCarFactory ();
Lcf. creatCar ();
Lcf. creatBus ();
}
}
// Abstract Factory ------ Example 2 (File Reading)
/**
Requirements:
Write the Read File function. The specific function is: Read text files, including (GBK, UTF-8, UNICoDe) encoded text files, the need to obtain the full text content;
To obtain an image file (BMP, GIF, and JPG), obtain the image width, length, and RGB color information at each point.
Defining a generic interface is a better way to solve different types of returned values. Shielding the difference in the number of method parameters is implemented in the form of "String... in.
*/
Import java. io .*;
Import javax. imageio .*;
Import java. awt. image .*;
Interface IRead {
T read (String... in );
}
Class FileRead implements IRead {
Public String read (String... in) {// multiple parameters can be input.
String result = null;
Try {
File file = new File (in [0]); // in [0] indicates the File name
Int len = 0;
FileInputStream input = new FileInputStream (file );
Byte buf [] = new byte [1024]; // the buffer size equals to the file length
// FileOutputStream output = new FileOutputStream ();
// While (len = input. read (buf ))! =-1 ){
// Output. write (buf, 0, len );
//}
Input. read (buf); // The length of the file to be read at a time
Result = new String (buf, in [1]); // convert the String into a Courseware String by encoding in [1]
Input. close ();
} Catch (Exception e ){
E. getMessage ();
}
Return result;
}
}
Class ImageInfo {
Private int width;
Private int height;
Private int r [] [];
Private int g [] [];
Private int B [] [];
Public void setWidth (int width ){
This. width = width;
}
Public void setHeight (int height ){
This. height = height;
}
Public int [] [] getR (){
Return r;
}
Public int [] [] getG (){
Return g;
}
Public int [] [] getB (){
Return B;
}
Public int getHeight (){
// TODO Auto-generated method stub
Return height;
}
Public int getWidth (){
// TODO Auto-generated method stub
Return width;
}
Public void setRGB (int rgb []) {
R = new int [height] [width];
G = new int [height] [width];
B = new int [height] [width];
Int pos = 0;
For (int I = 0; I
Pos = width * I;
For (int j = 0; j R [I] [j] = (rgb [pos + j] & 0xff0000)> 16;
G [I] [j] = (rgb [pos + j] & 0x00ff00)> 8;
B [I] [j] = rgb [pos + j] & 0x0000ff;
}
}
}
Public String toString (){
Return width: + this. getWidth () + height: + this. getHeight () + r: + this. getR () + g: + this. getG () + B: + this. getB ();
}
}
Class ImageRead implements IRead {// Read image files
BufferedImage bi = null;
Public ImageInfo read (String... in ){
File file = new File (in [0]); // in [0] indicates the image File name
Try {
/**
Static BufferedImage read (File input)
Returns a BufferedImage that is used to decode the provided File using ImageReader (which is automatically selected from the currently registered ImageReader.
*/
Bi = ImageIO. read (file );
} Catch (Exception e ){
E. printStackTrace ();
}
Int width = bi. getWidth ();
Int height = bi. getHeight ();
Int rgb [] = new int [width * height];
// Read the data image to the result Buffer
/**
Int [] getRGB (int startX, int startY, int w, int h, int [] rgbArray, int offset, int scansize)
Return the integer pixel array in the default RGB color model (TYPE_INT_ARGB) and default sRGB color space from a part of the image data. */
Bi. getRGB (0, 0, width, height, rgb, width, height );
ImageInfo obj = new ImageInfo (); // sets image information
Obj. setWidth (width );
Obj. setHeight (height );
Obj. setRGB (rgb );
Return obj;
}
}
// Automatically select the factory
Abstract class AbstractFactory {
Private static final String TEXT = text;
Private static final String IMAGE = image;
Abstract IRead creat ();
Static AbstractFactory creat (String mark ){
AbstractFactory factory = null;
If (mark. equals (TEXT) {// equivalent to: AbstractFactory factory = new TextFactory ();
Factory = new TextFactory ();
} Else if (mark. equals (IMAGE )){
Factory = new ImageFactory ();
}
Return factory;
}
}
Class TextFactory extends AbstractFactory {
Public IRead creat () {// IRead ifileread = new FileRead ();
Return new FileRead ();
}
}
Class ImageFactory extends AbstractFactory {
Public IRead creat (){
Return new ImageRead ();
}
}
Public class ReadInfoDemo {
Public static void main (String [] args ){
String result = (String) AbstractFactory. creat (text). creat (). read (F: // JavaOracle/OOP/AbstractDemos. java, GBK );
// TextFactory tf = AbstractFactory. creat (text );
// IRead ifread = tf. creat ();
// String result = ifread. read (F:/JavaOracle/OOP/AbstractDemos. java, GBK );
System. out. println (Read File Information: + result );
ImageInfo ir = (ImageInfo) AbstractFactory. creat (image). creat (). read (F: // JavaOracle/18.png );
System. out. println (read image information: + ir );
}
}