Java Abstract Factory mode

Source: Internet
Author: User

Efactory:

Public abstract class Efactory {    abstract public television producetelevision ();    Abstract public airconditioner Produceairconditioner ();}

Television:

Public interface Television {    void play ();

Airconditioner:

Public interface Airconditioner {    void Changetemperature ();}

Haierfactory:

public class Haierfactory extends efactory{    @Override public    Television producetelevision () {        return new Haiertelevision ();    }    @Override public    airconditioner Produceairconditioner () {        return new Haierairconditioner ();    }    }

Tclfactory:

public class Tclfactory extends efactory{    @Override public    Television producetelevision () {        return new Tcltelevision ();    }    @Override public    airconditioner Produceairconditioner () {        return new Tclairconditioner ();}    }

Haiertelevision:

public class Haiertelevision implements television{    @Override public    Void Play () {        System.out.println ( "This is haiertelevision!");    }    }

Tcltelevision:

public class Tcltelevision implements television{    @Override public    Void Play () {        System.out.println ("This is tcltelevison! ");    }    }

Haierairconditioner:

public class Haierairconditioner implements airconditioner{    @Override public    void Changetemperature () {        System.out.println ("This is haierairconditioner!");    }    }

Tclairconditioner:

public class Tclairconditioner implements airconditioner{    @Override public    void Changetemperature () {        System.out.println ("This is tclairconditioner!");    }    }

Factory (call):

public class Factory {public    static void Main (string[] args) {        haierfactory HF = new Haierfactory ();        Tclfactory TCLF = new Tclfactory ();                Airconditioner HAC = Hf.produceairconditioner ();        Airconditioner TCLC = Tclf.produceairconditioner ();                Television hat = Hf.producetelevision ();        Television tclt = Tclf.producetelevision ();                Hac.changetemperature ();        Tclc.changetemperature ();                Hat.play ();        Tclt.play ();    }    }

Result:

This is haierairconditioner!
This is tclairconditioner!
This is haiertelevision!
This is tcltelevison!

Java Abstract Factory mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.