File tree Expand file tree Collapse file tree 2 files changed +44
-1
lines changed
src/com/anxpp/designpattern/factorymethod Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .factorymethod ;
2+ //工厂方法模式
3+ public class FactoryMethod {
4+ public static void main (String args []){
5+ IFactory bigfactory ;
6+ bigfactory = new SmallFactory ();
7+ bigfactory .produce ().run ();
8+ bigfactory = new BigFactory ();
9+ bigfactory .produce ().run ();
10+ }
11+ }
12+ //抽象产品
13+ interface MeizuPhone {
14+ void run ();
15+ }
16+ //具体产品*2
17+ class PRO5 implements MeizuPhone {
18+ @ Override
19+ public void run () {
20+ System .out .println ("我是一台PRO5" );
21+ }
22+ }
23+ class MX5 implements MeizuPhone {
24+ @ Override
25+ public void run () {
26+ System .out .println ("我是一台MX5" );
27+ }
28+ }
29+ interface IFactory {
30+ MeizuPhone produce ();
31+ }
32+ //工厂*2
33+ class BigFactory implements IFactory {
34+ @ Override
35+ public MeizuPhone produce () {
36+ return new PRO5 ();
37+ }
38+ }
39+ class SmallFactory implements IFactory {
40+ @ Override
41+ public MeizuPhone produce () {
42+ return new MX5 ();
43+ }
44+ }
Original file line number Diff line number Diff line change 11package com .anxpp .designpattern .factorymethod ;
2-
32//只是需要遍历一堆数据,那么只需要2个方法就可以了
43public interface Iterator <T > {
54 boolean hasNext (); //是否还有下一个元素
You can’t perform that action at this time.
0 commit comments