Re: How to I access a logging class from any other class.
- From: "H. S. Lahman" <h.lahman@xxxxxxxxxxx>
- Date: Wed, 26 Oct 2005 17:55:57 GMT
Responding to Ssg31415926...
I want to be able to write log data. How can I access my logging class from any class in the app, without passing a reference to it?
I'll assume you only have one instance of the logging class.
First let me point out that implementing relationships by passing objects is usually not a good idea. The problem is that conceptually one has:
R1 R2 [Client] --------- [Service1] ----------- [Service2]
where the Client is instantiating the R2 object when it passes a reference to Service2 to Service1. However, participation in the R2 relationship is a personal matter between [Service1] and [Service2]. But for Client to pass the /right/ instance of [Service2], the Client must understand the rules and policies related to defining participation in the R2 relationship. Typically those rules and policies are unrelated to the collaboration context between Client and Service1 via the R1 relationship. So one has effectively given [Client] responsibilities that probably belong elsewhere (e.g., in an object from the GoF factory patterns whose mission in life is to understand instantiation of Service1 and/or Service2 and the nature of their participation in the R2 relationship). IOW, one has created unnecessary implementation dependencies that violate OO encapsulation. So one should only pass object references ot constructors or setters for referential attributes.
I think what you are really asking is how you can access it without instantiating an explicit relationship to it from every object that needs logging facilities. The short answer is that you can't. However, there are ways to reduce the tedium.
One way is via relationship paths. In a class model it is highly unusual to have a model where all classes are not somehow connected through relationship paths. IOW, one has
[Client] ----- [A] ------ [B] ----- ... ----- [Z] ------ [Logger]
so rather than passing a reference to the Client, the Client can "walk" the relationships to get to the logger (assuming the relationships are all instantiated by reference attributes). If one judiciously relates [Logger] to a couple of classes in the middle of the model, then the path to navigate will probably be quite short from any given client.
Another possibility is to use the GoF Singleton pattern. Singleton essentially disguises the notion of a global instance as a local implementation instance. Each object needing logging creates its private [Logger] but Singleton takes care of the redundancy. While this Just Works, from an aesthetic viewpoint it really isn't what Singleton was designed to handle (i.e., enforcing a business rule that /requires/ only a single instantiation). However, one can rationalize this at the OOP level as an optimization to avoid the path walking of the first solution when there is only going to be one instance anyway.
[Note that the relationship between [Client] and [Logger] still exists and it is being implemented. But that is being done at the private object implementation level so, by convention, we don't worry about it. (E.g., we wouldn't dream of cluttering a UML Class Model with it).]
Yet another possibility is to use composition inheritance so that all objects needing such facilities inherit from the [Logger] class. That produces the same relationship clutter at the OOA/D level but it is very simple at the OOP level. Alas, there are a number of reasons why it is a Bad Idea, mostly related to cohesion, logical indivisibility, and maintainability. So don't go there.
************* There is nothing wrong with me that could not be cured by a capful of Drano.
H. S. Lahman hsl@xxxxxxxxxxxxxxxxx Pathfinder Solutions -- Put MDA to Work http://www.pathfindermda.com blog: http://pathfinderpeople.blogs.com/hslahman (888)OOA-PATH
.
- References:
- How to I access a logging class from any other class.
- From: ssg31415926
- How to I access a logging class from any other class.
- Prev by Date: Re: LSP and subtype
- Next by Date: Re: How to I access a logging class from any other class.
- Previous by thread: How to I access a logging class from any other class.
- Next by thread: Re: How to I access a logging class from any other class.
- Index(es):
Relevant Pages
|