Design Pattern 4 abstract factory pattern

Source: Internet
Author: User

Hello everyone, let's discuss the abstract factory model. I still use the example I gave earlier (For details, refer to the previous simple factory), but now I want to make a hypothesis:

Assume that Apple and Xiaomi both produce phones, MP3, etc. (purely for convenience)

Now let's talk about the product level structure and product family concept.

The so-called product hierarchy refers to the inheritance structure of a certain type of products, such as phones, including IPhone phones and XiaoMi phones. They belong to the same product hierarchy.

Product Family: A group of products with different product levels, which are generated by the same factory. For example, Apple can produce both IPhone and IPOD, while XiaoMi can produce both XiaoMi phones and XiaoMiPod (Suppose ...)

Now let's look at the UML diagram:


Now let's look at the code. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;"> package edu. fjnu. cs. hwb. abstractfactory; public interface Phone {void call ();}

package edu.fjnu.cs.hwb.abstractfactory;public interface Pod {void play();}

package edu.fjnu.cs.hwb.abstractfactory;public interface AbstractFactory{Phone getPhone(String name);Pod getPod(String name);}

Now let's look at the specific implementation class.

First, Apple products

package edu.fjnu.cs.hwb.abstractfactory;public class IPhone4 implements Phone {@Overridepublic void call(){System.out.println("this is IPhone4");}}
package edu.fjnu.cs.hwb.abstractfactory;public class IPhone4s implements Phone{@Overridepublic void call(){System.out.println("this is IPhone4s");}}
package edu.fjnu.cs.hwb.abstractfactory;public class IPod1 implements Pod {@Overridepublic void play() {System.out.println("this is IPod1");}}
package edu.fjnu.cs.hwb.abstractfactory;public class IPod2 implements Pod {@Overridepublic void play() {System.out.println("this is IPod2");}}

Now let's look at Xiaomi's:

package edu.fjnu.cs.hwb.abstractfactory; public class XiaoMi2 implements Phone{@Overridepublic void call(){System.out.println("this is XiaoMi2");}}
package edu.fjnu.cs.hwb.abstractfactory;public class XiaoMi2S implements Phone {@Overridepublic void call() {System.out.println("this is XiaoMi2S");}}
package edu.fjnu.cs.hwb.abstractfactory;public class XiaoMiPod1 implements Pod{@Overridepublic void play(){System.out.println("this is XiaoMiPod1");}}
package edu.fjnu.cs.hwb.abstractfactory;public class XiaoMiPod2 implements Pod {@Overridepublic void play() {System.out.println("this is XiaoMiPod2");}}


Okay. Let's take a look at the corresponding factory. The first is Apple's:

package edu.fjnu.cs.hwb.abstractfactory;public class AppleFactory implements AbstractFactory {@Overridepublic Phone getPhone(String name) {if ("IPhone4".equalsIgnoreCase(name))return new IPhone4();else if ("IPhone4s".equalsIgnoreCase(name))return new IPhone4s();return null;}@Overridepublic Pod getPod(String name) {if ("IPod1".equalsIgnoreCase(name))return new IPod1();else if ("IPod2".equalsIgnoreCase(name))return new IPod2();return null;}}

Followed by Xiaomi's:

package edu.fjnu.cs.hwb.abstractfactory;public class XiaoMiFactory implements AbstractFactory {@Overridepublic Phone getPhone(String name){if ("XiaoMi2".equalsIgnoreCase(name))return new XiaoMi2();else if ("XiaoMi2s".equalsIgnoreCase(name))return new XiaoMi2S();return null;}@Overridepublic Pod getPod(String name){if ("XiaoMiPod1".equalsIgnoreCase(name))return new XiaoMiPod1();else if ("XiaoMiPod2".equalsIgnoreCase(name))return new XiaoMiPod2();return null;}}
Let's see how the client programmer uses it:

package edu.fjnu.cs.hwb.abstractfactory;public class Client {public static void main(String[] args){AbstractFactory abstractFactory = new AppleFactory();Phone phone = abstractFactory.getPhone("Iphone4");phone.call();phone = abstractFactory.getPhone("Iphone4s");phone.call();Pod pod = abstractFactory.getPod("Ipod1");pod.play();pod = abstractFactory.getPod("Ipod2");pod.play();System.out.println();abstractFactory = new XiaoMiFactory();phone = abstractFactory.getPhone("XiaoMi2");phone.call();phone = abstractFactory.getPhone("XiaoMi2s");phone.call();pod = abstractFactory.getPod("XiaoMiPod1");pod.play();pod = abstractFactory.getPod("XiaoMiPod2");pod.play();}}
The result is as follows:





Okay... although there are a lot of code, it's very simple ..

If any errors occur, I hope you can correct them.


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.