Three factory pattern in Java

 

/ **
 * factory design pattern mode, user-created objects, the factory mode by the coupling to reduce the code
 * @author
 * /

 

// simple factory pattern


{class SimpleFactory public
    
    / **
     * returns the object according to different car but String parameter name name
     * @param name
     * @return
     * /
    public Car getCar (String name) {
        
        / *
         characteristic * using equals () methods: for any non-null reference values of the X-, x.equals (null)
         * should return false
         * "name" is a non-empty String object, that is, any non-null reference values of the X-
         * /
        IF (name.equals ( "Benz") ) {
            return new new Benz ();
        } the else IF (name.equals ( "QQ")) {
            return new new QQ ();
        } the else {
            System.out.println ( "no such plant models");
            return null;
        }
        
    }
}

 

interface Car {public
    // bus interface to implement
    String getName ();
}

 

Benz the implements Car {class public
    // implement bus interface
    @Override
    public String getName () {
        return "Benz";
    }
}

 

Car BWM the implements class {public    
    // implement bus interface
    @Override
    public String getName () {
        return "BWM";
    }
}

{class SimpleFactoryTest public
    public static void main (String [] args) {
        // simple test class factory
        SimpleFactory simpleFactory new new SimpleFactory = ();
        Car simpleFactory.getCar CAR = ( "Benz");
        System.out.println (CAR. getName ()); // Benz
                
    }
}

 

// factory method

Factory method using an abstract factory role as the core of concrete classes, instead of using the plant as a core in a simple mode. Although the factory method pattern to meet most business needs we might have. But when the product range is very long, there will be a large number of the corresponding factory class, it should not be what we want.

Preferably factory pattern is simple manner factory methods in combination to reduce factory classes: i.e. for products similar to tree species (typically leaves of the tree in each other brother) Using simple factory pattern is achieved.

Exist at some point in the system for different products tree, product and product family there is a tree, then in this case it may be possible to use the abstract factory pattern.

 

// define a class factory interface the interface is responsible for calling the production car other
public interface {Factory's
    Car getCar ();
}

 

interface Car {public
    // bus interface to implement
    String getName ();
}

 

Factory's class BWMFactory the implements {public    
    // implement bus interface
    @Override
    public Car getCar () {
        return new new BWM ();
    }
}

 

Factory's class BenZFactory the implements {public
    // implement the factory interface
    @Override
    public Car getCar () {
        return new new Benz ();
    }
}

Benz the implements Car {class public
  // bus interface achieved
    public String getName () {
        return "Benz";
    }
}

 

Car BWM the implements class {public
    // bus interface achieved
    public String getName () {
        return "BWM";
    }
}

 

public class FactoryTest {
    public static void main(String[] args){
        //工厂的测试类
        Factory bwmFactory = new BWMFactory();
        System.out.println(bwmFactory.getCar().getName());   //BWM
        Factory benzFactory = new BenZFactory();
        System.out.println(benzFactory.getCar().getName());     //BenZ                
    }
}

 

// Abstract Factory

 

// interface defines an abstract factory class
public abstract class AbstractFactory {
    protected abstract Car getCar ();    
    // dynamic configuration
    public Car getCar (String name) {
        IF ( "BWM" .equals (name)) {
            return new new BWMFactory () .getCar ();
        } the else IF ( "Benz" .equals (name)) {
            return new new BenZFactory () getCar ();.
        } the else {
            System.out.println ( "factory production of such models can not");
            return null ;
        }
    }    
}

 

interface Car {public
    // bus interface to implement
    String getName ();
}

 

Car BWM the implements class {public
    // bus interface achieved
    public String getName () {
        return "BWM";
    }
}

 

public  class BenZ implements Car{

    public String getName() {
        return "BenZ";
    }
}

 

the extends class BWMFactory AbstractFactory {public    
    // implement bus interface
    @Override
    public Car getCar () {
        return new new BWM ();
    }
}

 

the extends class BenZFactory AbstractFactory {public
    // implement the factory interface
    @Override
    public Car getCar () {
        return new new Benz ();
    }
}

 

public class DefaultFactory extends AbstractFactory {
    private AudiFactory defaultFactory = new AudiFactory();    
    public Car getCar(){
        return defaultFactory.getCar();
    }
}

 

 

public class AbstractFactoryTest {
    public static void main(String[] args){
        //工厂的测试类
        DefaultFactory factory = new DefaultFactory();
        System.out.println(factory.getCar("BWM").getName());   //BWM
    }
}

Guess you like

Origin www.cnblogs.com/zwjcom/p/11141097.html