Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 55caa45

Browse files
committed
Abstractfactory
1 parent e06f063 commit 55caa45

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

‎Abstractfactory/index.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
/**
4+
* Design pattern "Abstract factory" (Creational)
5+
* This is demo code
6+
* See for details: http://maxsite.org/page/php-patterns
7+
*/
8+
9+
/**
10+
* Base Interface for Factory's
11+
*/
12+
interface FactoryInterface
13+
{
14+
public function method();
15+
}
16+
17+
/**
18+
* Class for FactoryA
19+
*/
20+
class ConcreteFactoryA implements FactoryInterface
21+
{
22+
protected $class;
23+
24+
/**
25+
* Use Class1
26+
*/
27+
public function __construct()
28+
{
29+
$this->class = new Class1();
30+
}
31+
32+
/**
33+
* run method class1
34+
*/
35+
public function method()
36+
{
37+
$this->class->method();
38+
}
39+
}
40+
41+
/**
42+
* Class for FactoryB
43+
*/
44+
class ConcreteFactoryB implements FactoryInterface
45+
{
46+
protected $class;
47+
48+
/**
49+
* Use Class2
50+
*/
51+
public function __construct()
52+
{
53+
$this->class = new Class2();
54+
}
55+
56+
/**
57+
* run method class2
58+
*/
59+
public function method()
60+
{
61+
$this->class->method();
62+
}
63+
}
64+
65+
/**
66+
* implementation in Class1
67+
*/
68+
class Class1
69+
{
70+
public function method()
71+
{
72+
echo 'Class1 method<br>';
73+
}
74+
}
75+
76+
/**
77+
* implementation in Class2
78+
*/
79+
class Class2
80+
{
81+
public function method()
82+
{
83+
echo 'Class2 method<br>';
84+
}
85+
}
86+
87+
/**
88+
* demo
89+
*/
90+
91+
/**
92+
* create FactoryA
93+
*/
94+
$a = new ConcreteFactoryA();
95+
$a->method(); // Class1 method
96+
97+
/**
98+
* create FactoryB
99+
*/
100+
$b = new ConcreteFactoryB();
101+
$b->method(); // Class2 method
102+
103+
# end of file

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /