开发者

In PHP what means: factory, instance and observer?

In PHP, what means: factory, instance and observer?

Thank you!开发者_JS百科


observer and factory

observer and factory are two design patterns within object oriented programming. gang of four (or gof) are usually attributed the creation of these patterns. they have a book, i believe it's with implementations in c++. i haven' read it, i heard it was kind of heavy to read. however, i did read "head first design patterns", and that's a really good book! still high level stuff though, maybe for someone who has been programming for a year or so.

instance

an instance is something concrete. instance is to class what an employer is to its profession. the profession is a way of describing something that workers with that profession does. a job description so to speak. the worker then actually performs the work in question.

correlation between factory and instance

being a worker of a factory profession means that you have the responsibility of creating workers of some other, probably more complex, profession. or maybe the profession itself isn't very complex, but the process of creating a worker of that profession is complex.

important final note

all these terms are valid and interesting within all object oriented languages, like for example php, java, c# and c++, and many more.


It's not relative to PHP only. What those words mean are "design patterns" concept, that is, a method of organizing code that achieves a series of properties, such as loose dependency, ease of reuse and test.

I suggest you a book colloquially called Gang of four. It explain basic design patterns and it's a classic in its topic.


Factory and Observer are design patterns.

a class is a blueprint of an object. an instance is an created object of a specified class.


Blatantly copied from Wikipedia:

The factory pattern is a creational design pattern used in software development to encapsulate the processes involved in the creation of objects.

https://en.wikipedia.org/wiki/Factory_pattern

Example code for a factory can also be found in the PHP Manual on Patterns


The observer pattern (a subset of the publish/subscribe pattern) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.

https://en.wikipedia.org/wiki/Observer_pattern

PHP has a native interface to implement Subject/Observer patterns in SPL


An instance is an object derived from a class definition, e.g.

$instance = new StdClass;


It means the same as in any language support some flavor of OOP. A class with a very specific responsability.

Look here for more details:

  • http://en.wikipedia.org/wiki/Factory_pattern
  • http://en.wikipedia.org/wiki/Observer_pattern


Same thing it means everywhere else. Factory and Observer are design patterns. I'm not sure I can describe what instance means without using the word instance in the definition... Or Instantiate, which is just the word instance in disguise.


factory and observer are design patterns used to solve a few common OO problems. instance is an object of a class which will be used to access methods within that class ex.

$userObj = new User();

$userObj is an instance of the User class.

for more information on design patterns check this link out

http://www.fluffycat.com/PHP-Design-Patterns/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜