struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Gainty <mgai...@hotmail.com>
Subject Re: Java Singleton , Framework Design Patterns
Date Sat, 14 Dec 2019 12:19:38 GMT
MG>maybe.. see below

________________________________
From: Zahid Rahman <zahidr1000@gmail.com>
Sent: Friday, December 13, 2019 5:09 PM
To: Struts Users Mailing List <user@struts.apache.org>
Subject: Re: Java Singleton , Framework Design Patterns

Hi,

>?am interested to know which topic prompts your question ?
As you can see from the log file (below) the spring framework creates and
destroys a  singleton of a bean.
So the singleton pattern is used by the container.
I was also informed that the singleton is used to create a login screen.
That also appears to be the case from the page
http://www.blackwasp.co.uk/Singleton.aspx.

The login screen I will be using is the login.java and login.jsp in the
struts2  "blank app" in "struts-examples-master".
The struts framework also helps me with duplicate form submission.
So do I need to use the singleton pattern  ?

MG>here is ted husted struts-blank login.java (un-edited)
/*
 * $Id: Login.java 471756 2006-11-06 15:01:43Z husted $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package example;
public class Login extends ExampleSupport {
    public String execute() throws Exception {
        if (isInvalid(getUsername())) return INPUT;
        if (isInvalid(getPassword())) return INPUT;
        return SUCCESS;
    }
    private boolean isInvalid(String value) {
        return (value == null || value.length() == 0);
    }
    private String username;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    private String password;
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

//no singleton yet as there is no public static ClassName getInstance()
//with private constructor pointing to getInstance()

//BUT singleton is used to instantate and set default ObjectFactory for XmlConfigurationProvider
//as seen here in ConfigurationTestBase.java
package com.opensymphony.xwork2.config.providers;
public abstract class ConfigurationTestBase extends com.opensymphone.xwork2.XWorkTestCase
{
    protected com.opensymphony.xwork2.config.ConfigurationProvider buildConfigurationProvider(final
String filename) {
        configuration = new com.opensymphony.xwork2.config.providers.MockConfiguration();
        ((com.opensymphony.xwork2.config.providers.MockConfiguration)configuration).selfRegister();
        container = configuration.getContainer();
        XmlConfigurationProvider prov = new XmlConfigurationProvider(filename, true);
        prov.setObjectFactory(container.getInstance(com.opensymphony.xwork2.ObjectFactory.class));

//but the provider does not use Spring Object Factory which we can remedy here
        prov.setObjectFactory(container.getInstance(com.opensymphony.xwork2.spring.SpringObjectFactory.class));

//as you mention we must follow springs example of declaring singleton bean in ./WEB-INF/<application>context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
    <bean id="default" class="com.opensymphony.xwork2.spring.SpringObjectFactory" singleton="true"
/>

//where struts specifies default <Spring>ObjectFactory thru ./classes/org/apache/struts2/default.properties
### if specified, the default object factory can be overridden here
### Note: short-hand notation is supported in some cases, such as "spring"
###       Alternatively, you can provide a com.opensymphony.xwork2.ObjectFactory subclass
name here
struts.objectFactory=com.opensymphony.xwork2.spring.SpringObjectFactory.class

//the line struts.objectFactory=com.opensymphony.xwork2.spring.SpringObjectFactory.class in
default.properties should be sufficient
//to route all instances of ObjectFactory to SpringObjectFactory

does this help?

 2019-12-13 21:01:06,882 [main] [DEBUG]
o.s.b.f.s.DefaultListableBeanFactory - Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@6f1d30d:
defining beans [numberGenerator]; root of factory hierarchy
2019-12-13 21:01:06,882 [main] [DEBUG] o.s.b.f.s.DefaultListableBeanFactory
- Creating shared instance of singleton bean 'numberGenerator'
2019-12-13 21:01:06,882 [main] [DEBUG] o.s.b.f.s.DefaultListableBeanFactory
- Creating instance of bean 'numberGenerator'
2019-12-13 21:01:06,895 [main] [DEBUG] o.s.b.f.s.DefaultListableBeanFactory
- Eagerly caching bean 'numberGenerator' to allow for resolving potential
circular references
2019-12-13 21:01:06,896 [main] [DEBUG] o.s.b.f.s.DefaultListableBeanFactory
- Finished creating instance of bean 'numberGenerator'
2019-12-13 21:01:06,897 [main] [DEBUG]
o.s.c.s.ClassPathXmlApplicationContext - Unable to locate
LifecycleProcessor with name 'lifecycleProcessor': using default
[org.springframework.context.support.DefaultLifecycleProcessor@d967f908]
2019-12-13 21:01:06,897 [main] [DEBUG] o.s.b.f.s.DefaultListableBeanFactory
- Returning cached instance of singleton bean 'lifecycleProcessor'
2019-12-13 21:01:06,900 [main] [DEBUG]
o.s.c.e.PropertySourcesPropertyResolver - Could not find key
'spring.liveBeansView.mbeanDomain' in any property source
2019-12-13 21:01:06,910 [main] [DEBUG] o.s.b.f.s.DefaultListableBeanFactory
- Returning cached instance of singleton bean 'numberGenerator'
2019-12-13 21:01:06,910 [main] [INFO ] academy.learnprogramming.Main -
number = 0
2019-12-13 21:01:06,912 [main] [INFO ]
o.s.c.s.ClassPathXmlApplicationContext - Closing
org.springframework.context.support.ClassPathXmlApplicationContext@3e92e10:
startup date [Fri Dec 13 21:01:06 GMT 2019]; root of context hierarchy
2019-12-13 21:01:06,912 [main] [DEBUG] o.s.b.f.s.DefaultListableBeanFactory
- Returning cached instance of singleton bean 'lifecycleProcessor'
2019-12-13 21:01:06,912 [main] [DEBUG] o.s.b.f.s.DefaultListableBeanFactory
- Destroying singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@6f1d30d:
defining beans [numberGenerator]; root of factory hierarchy

On Fri, 13 Dec 2019 at 01:33, Martin Gainty <mgainty@hotmail.com> wrote:

> Singleton:
> i prefer thread-safe implementations and generally eschew singletons
> (unless extreme political pressure dictates otherwise)
> https://www.geeksforgeeks.org/singleton-design-pattern/
> [https://www.geeksforgeeks.org/wp-content/uploads/gfg_200X200.png]<
> https://www.geeksforgeeks.org/singleton-design-pattern/>
> Singleton Design Pattern | Implementation - GeeksforGeeks<
> https://www.geeksforgeeks.org/singleton-design-pattern/>
> The singleton pattern is one of the simplest design patterns. Sometimes we
> need to have only one instance of our class for example a single DB
> connection shared by multiple objects as creating a separate DB connection
> for every object may be costly. Similarly, there can be a single
> configuration ...
> www.geeksforgeeks.org<http://www.geeksforgeeks.org>
>
> Design Patterns:
> have a look at this discussion from stackoverflow on Visitor Pattern vs
> Command Pattern for lambda expresssions
> https://stackoverflow.com/questions/2186931/java-pass-method-as-parameter
> [
> https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded
> ]<
> https://stackoverflow.com/questions/2186931/java-pass-method-as-parameter>
> interface - Java Pass Method as Parameter - Stack Overflow<
> https://stackoverflow.com/questions/2186931/java-pass-method-as-parameter>
> In Java 8, you can now pass a method more easily using Lambda Expressions
> and Method References. First, some background: a functional interface is an
> interface that has one and only one abstract method, although it can
> contain any number of default methods (new in Java 8) and static methods. A
> lambda expression can quickly implement the abstract method, without all
> the unnecessary syntax ...
> stackoverflow.com
> although this discussion is more specific on when to implement Visitor
> Pattern vs when to use Command Pattern
>
> https://stackoverflow.com/questions/2857880/command-pattern-vs-visitor-pattern
> [
> https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded
> ]<
> https://stackoverflow.com/questions/2857880/command-pattern-vs-visitor-pattern
> >
> Command Pattern vs. Visitor Pattern - Stack Overflow<
> https://stackoverflow.com/questions/2857880/command-pattern-vs-visitor-pattern
> >
> Each pattern has it's own pros, cons and use cases. You can use Command
> pattern to . Decouple the invoker & receiver of command . Implement
> callback mechanism. Implement undo and redo functionality. Maintain a
> history of commands. Use Visitor pattern in below scenarios:. Similar
> operations have to be performed on objects of different types grouped in a
> structure ; You need to execute many ...
> stackoverflow.com
>
> ?am interested to know which topic prompts your question?
>
> /br/
> Martin
> ________________________________
> From: Lukasz Lenart <lukaszlenart@apache.org>
> Sent: Thursday, December 12, 2019 1:56 AM
> To: Struts Users Mailing List <user@struts.apache.org>
> Subject: Re: Java Singleton , Framework Design Patterns
>
> czw., 12 gru 2019 o 06:13 Zahid Rahman <zahidr1000@gmail.com> napisał(a):
> > So my point is I have not been able to find accurate information , if
> some
> > one could furnish me a Java language specification or recommend  a book
> > which accurately describes these I would be grateful.
>
> Start with Gang of Four
> http://www.blackwasp.co.uk/gofpatterns.aspx
>
>
> Regards
> --
> Łukasz
> + 48 606 323 122 http://www.lenart.org.pl/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message