File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
src/com/anxpp/designpattern/simplefactory Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .simplefactory ;
2+ //演示简单工厂
3+ public class SimpleFactory {
4+ public static void main (String args []) throws Exception {
5+ Factory factory = new Factory ();
6+ factory .produce ("PRO5" ).run ();
7+ factory .produce ("PRO6" ).run ();
8+ }
9+ }
10+ //抽象产品
11+ interface MeizuPhone {
12+ void run ();
13+ }
14+ //具体产品X2
15+ class PRO5 implements MeizuPhone {
16+ @ Override
17+ public void run (){
18+ System .out .println ("我是一台PRO5" );
19+ }
20+ }
21+ class PRO6 implements MeizuPhone {
22+ @ Override
23+ public void run (){
24+ System .out .println ("我是一台PRO6" );
25+ }
26+ }
27+ //工厂
28+ class Factory {
29+ MeizuPhone produce (String product ) throws Exception {
30+ if (product .equals ("PRO5" ))
31+ return new PRO5 ();
32+ else if (product .equals ("PRO6" ))
33+ return new PRO6 ();
34+ throw new Exception ("No Such Class" );
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * @author Administrator
3+ * 简单工厂模式
4+ */
5+ package com .anxpp .designpattern .simplefactory ;
You can’t perform that action at this time.
0 commit comments